-module(pt_166).
-include("common.hrl").
-export([
        read/2,
        write/2
    ]).

read(16600, _) ->
    {ok, []};
read(16601, _) ->
    {ok, []};
read(16602, _) ->
    {ok, []};
read(16603, Bin0) ->
    <<ClassId:16, _Bin1/binary>> = Bin0, 
    {ok, [ClassId]};
read(16604, _) ->
    {ok, []};
read(16605, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (16600,[
    Degree,
    DegreeExp,
    DunList
] = Src_Data) ->
    BinList_DunList = [
        item_to_bin_0(DunList_Item) || DunList_Item <- DunList
    ], 

    DunList_Len = length(DunList), 
    Bin_DunList = list_to_binary(BinList_DunList),

    Data = <<
        Degree:8,
        DegreeExp:32,
        DunList_Len:16, Bin_DunList/binary
    >>,
    {ok, pt:encode(16600, Data, Src_Data)};

write (16601,[
    ClassList
] = Src_Data) ->
    BinList_ClassList = [
        item_to_bin_1(ClassList_Item) || ClassList_Item <- ClassList
    ], 

    ClassList_Len = length(ClassList), 
    Bin_ClassList = list_to_binary(BinList_ClassList),

    Data = <<
        ClassList_Len:16, Bin_ClassList/binary
    >>,
    {ok, pt:encode(16601, Data, Src_Data)};

write (16602,[
    Res,
    Degress,
    DegressExp
] = Src_Data) ->
    Data = <<
        Res:32,
        Degress:8,
        DegressExp:32
    >>,
    {ok, pt:encode(16602, Data, Src_Data)};

write (16603,[
    Res,
    ClassId,
    DegreeExp
] = Src_Data) ->
    Data = <<
        Res:32,
        ClassId:16,
        DegreeExp:32
    >>,
    {ok, pt:encode(16603, Data, Src_Data)};

write (16604,[
    Type,
    Id,
    Progress,
    State
] = Src_Data) ->
    Data = <<
        Type:8,
        Id:16,
        Progress:16,
        State:8
    >>,
    {ok, pt:encode(16604, Data, Src_Data)};

write (16605,[
    RefreshStatus
] = Src_Data) ->
    Data = <<
        RefreshStatus:8
    >>,
    {ok, pt:encode(16605, Data, Src_Data)};

write(_Cmd, _R) -> ?DEBUG("no_match:~p,~p~n", [_Cmd, _R]), {ok, pt:pack(0, <<>>)}.


item_to_bin_0 ({
    DunId,
    IsFinish
}) ->
    Data = <<
        DunId:32,
        IsFinish:8
    >>,
    Data.
item_to_bin_1 ({
    ClassId,
    TargetType,
    TargetId,
    Progress,
    Status
}) ->
    Data = <<
        ClassId:16,
        TargetType:8,
        TargetId:32,
        Progress:16,
        Status:8
    >>,
    Data.
