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

read(11300, _) ->
    {ok, []};
read(11301, _) ->
    {ok, []};
read(11302, _) ->
    {ok, []};
read(11303, Bin0) ->
    <<Module:16, Bin1/binary>> = Bin0, 
    <<SubId:8, Bin2/binary>> = Bin1, 
    <<Status:8, _Bin3/binary>> = Bin2, 
    {ok, [Module, SubId, Status]};
read(_Cmd, _R) -> {error, no_match}.

write (11300,[
    CloseList,
    NowValue,
    DailyValue
] = Src_Data) ->
    BinList_CloseList = [
        item_to_bin_0(CloseList_Item) || CloseList_Item <- CloseList
    ], 

    CloseList_Len = length(CloseList), 
    Bin_CloseList = list_to_binary(BinList_CloseList),

    Data = <<
        CloseList_Len:16, Bin_CloseList/binary,
        NowValue:32,
        DailyValue:16
    >>,
    {ok, pt:encode(11300, Data, Src_Data)};

write (11301,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_1(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(11301, Data, Src_Data)};

write (11302,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_2(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(11302, Data, Src_Data)};

write (11303,[
    Module,
    SubId,
    Status
] = Src_Data) ->
    Data = <<
        Module:16,
        SubId:8,
        Status:8
    >>,
    {ok, pt:encode(11303, Data, Src_Data)};

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


item_to_bin_0 ({
    Module,
    SubId
}) ->
    Data = <<
        Module:16,
        SubId:8
    >>,
    Data.
item_to_bin_1 ({
    Time,
    Duration,
    Module,
    SubId,
    CostValue
}) ->
    Data = <<
        Time:32,
        Duration:16,
        Module:16,
        SubId:8,
        CostValue:8
    >>,
    Data.
item_to_bin_2 ({
    Time,
    Duration,
    Module,
    SubId,
    CostValue,
    RewardList
}) ->
    Bin_RewardList = pt:write_object_list(RewardList), 

    Data = <<
        Time:32,
        Duration:16,
        Module:16,
        SubId:8,
        CostValue:8,
        Bin_RewardList/binary
    >>,
    Data.
