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

read(10400, _) ->
    {ok, []};
read(10401, Bin0) ->
    <<Count:8, _Bin1/binary>> = Bin0, 
    {ok, [Count]};
read(10402, Bin0) ->
    <<Count:16, _Bin1/binary>> = Bin0, 
    {ok, [Count]};
read(10403, _) ->
    {ok, []};
read(10404, Bin0) ->
    <<Id:8, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(10405, Bin0) ->
    <<Id:8, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(10406, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (10400,[
    CurrPeriod,
    CurrRound,
    RealRound,
    CurrCount,
    AwardRound,
    CountAwards
] = Src_Data) ->
    BinList_CountAwards = [
        item_to_bin_0(CountAwards_Item) || CountAwards_Item <- CountAwards
    ], 

    CountAwards_Len = length(CountAwards), 
    Bin_CountAwards = list_to_binary(BinList_CountAwards),

    Data = <<
        CurrPeriod:8,
        CurrRound:8,
        RealRound:8,
        CurrCount:16,
        AwardRound:8,
        CountAwards_Len:16, Bin_CountAwards/binary
    >>,
    {ok, pt:encode(10400, Data, Src_Data)};

write (10401,[
    Errcode,
    AwardList
] = Src_Data) ->
    BinList_AwardList = [
        item_to_bin_1(AwardList_Item) || AwardList_Item <- AwardList
    ], 

    AwardList_Len = length(AwardList), 
    Bin_AwardList = list_to_binary(BinList_AwardList),

    Data = <<
        Errcode:32,
        AwardList_Len:16, Bin_AwardList/binary
    >>,
    {ok, pt:encode(10401, Data, Src_Data)};

write (10402,[
    Errcode
] = Src_Data) ->
    Data = <<
        Errcode:32
    >>,
    {ok, pt:encode(10402, Data, Src_Data)};

write (10403,[
    TaskList
] = Src_Data) ->
    BinList_TaskList = [
        item_to_bin_2(TaskList_Item) || TaskList_Item <- TaskList
    ], 

    TaskList_Len = length(TaskList), 
    Bin_TaskList = list_to_binary(BinList_TaskList),

    Data = <<
        TaskList_Len:16, Bin_TaskList/binary
    >>,
    {ok, pt:encode(10403, Data, Src_Data)};

write (10404,[
    Errcode
] = Src_Data) ->
    Data = <<
        Errcode:32
    >>,
    {ok, pt:encode(10404, Data, Src_Data)};

write (10405,[
    Errcode
] = Src_Data) ->
    Data = <<
        Errcode:32
    >>,
    {ok, pt:encode(10405, Data, Src_Data)};

write (10406,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_3(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(10406, Data, Src_Data)};

write (10407,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_4(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(10407, Data, Src_Data)};

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


item_to_bin_0 ({
    Count,
    Stage
}) ->
    Data = <<
        Count:16,
        Stage:8
    >>,
    Data.
item_to_bin_1 (
    AwardId
) ->
    Data = <<
        AwardId:32
    >>,
    Data.
item_to_bin_2 ({
    Id,
    Times,
    State
}) ->
    Data = <<
        Id:8,
        Times:8,
        State:8
    >>,
    Data.
item_to_bin_3 ({
    Id,
    Name,
    GoodsTypeId
}) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        Id:64,
        Bin_Name/binary,
        GoodsTypeId:32
    >>,
    Data.
item_to_bin_4 ({
    Id,
    Name,
    GoodsTypeId
}) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        Id:64,
        Bin_Name/binary,
        GoodsTypeId:32
    >>,
    Data.
