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

read(15701, Bin0) ->
    <<ActType:8, _Bin1/binary>> = Bin0, 
    {ok, [ActType]};
read(15702, _) ->
    {ok, []};
read(15703, _) ->
    {ok, []};
read(15705, Bin0) ->
    <<Id:32, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(15709, _) ->
    {ok, []};
read(15710, _) ->
    {ok, []};
read(15715, Bin0) ->
    <<Module:32, Bin1/binary>> = Bin0, 
    <<ModuleSub:32, _Bin2/binary>> = Bin1, 
    {ok, [Module, ModuleSub]};
read(15717, Bin0) ->
    <<Module:32, _Bin1/binary>> = Bin0, 
    {ok, [Module]};
read(15718, _) ->
    {ok, []};
read(15719, Bin0) ->
    <<Module:16, Bin1/binary>> = Bin0, 
    <<SubModule:8, Bin2/binary>> = Bin1, 
    <<AcSub:8, _Bin3/binary>> = Bin2, 
    {ok, [Module, SubModule, AcSub]};
read(15720, Bin0) ->
    <<Module:16, Bin1/binary>> = Bin0, 
    <<SubModule:8, Bin2/binary>> = Bin1, 
    <<AcSub:8, _Bin3/binary>> = Bin2, 
    {ok, [Module, SubModule, AcSub]};
read(_Cmd, _R) -> {error, no_match}.

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

write (15701,[
    ActType,
    Time,
    PowerLive,
    Coin,
    Honor,
    AcList
] = Src_Data) ->
    BinList_AcList = [
        item_to_bin_0(AcList_Item) || AcList_Item <- AcList
    ], 

    AcList_Len = length(AcList), 
    Bin_AcList = list_to_binary(BinList_AcList),

    Data = <<
        ActType:8,
        Time:64,
        PowerLive:32,
        Coin:16,
        Honor:16,
        AcList_Len:16, Bin_AcList/binary
    >>,
    {ok, pt:encode(15701, Data, Src_Data)};

write (15702,[
    AcList
] = Src_Data) ->
    BinList_AcList = [
        item_to_bin_1(AcList_Item) || AcList_Item <- AcList
    ], 

    AcList_Len = length(AcList), 
    Bin_AcList = list_to_binary(BinList_AcList),

    Data = <<
        AcList_Len:16, Bin_AcList/binary
    >>,
    {ok, pt:encode(15702, Data, Src_Data)};

write (15703,[
    Live,
    LiveMax,
    RewardList
] = Src_Data) ->
    BinList_RewardList = [
        item_to_bin_2(RewardList_Item) || RewardList_Item <- RewardList
    ], 

    RewardList_Len = length(RewardList), 
    Bin_RewardList = list_to_binary(BinList_RewardList),

    Data = <<
        Live:32,
        LiveMax:32,
        RewardList_Len:16, Bin_RewardList/binary
    >>,
    {ok, pt:encode(15703, Data, Src_Data)};

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

write (15706,[
    Module,
    ModuleSub,
    AcSub,
    Status,
    EndTime
] = Src_Data) ->
    Data = <<
        Module:32,
        ModuleSub:32,
        AcSub:32,
        Status:8,
        EndTime:32
    >>,
    {ok, pt:encode(15706, Data, Src_Data)};

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

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

write (15710,[
    Result,
    Lv,
    Exp
] = Src_Data) ->
    Data = <<
        Result:32,
        Lv:32,
        Exp:32
    >>,
    {ok, pt:encode(15710, Data, Src_Data)};

write (15714,[
    Time
] = Src_Data) ->
    Data = <<
        Time:64
    >>,
    {ok, pt:encode(15714, Data, Src_Data)};

write (15715,[
    Module,
    ModuleSub,
    AcSub,
    Status,
    EndTime
] = Src_Data) ->
    Data = <<
        Module:32,
        ModuleSub:32,
        AcSub:32,
        Status:8,
        EndTime:32
    >>,
    {ok, pt:encode(15715, Data, Src_Data)};

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



write (15718,[
    ReserveList
] = Src_Data) ->
    BinList_ReserveList = [
        item_to_bin_3(ReserveList_Item) || ReserveList_Item <- ReserveList
    ], 

    ReserveList_Len = length(ReserveList), 
    Bin_ReserveList = list_to_binary(BinList_ReserveList),

    Data = <<
        ReserveList_Len:16, Bin_ReserveList/binary
    >>,
    {ok, pt:encode(15718, Data, Src_Data)};

write (15719,[
    Errcode,
    Module,
    SubModule,
    AcSub,
    Status
] = Src_Data) ->
    Data = <<
        Errcode:32,
        Module:16,
        SubModule:8,
        AcSub:8,
        Status:8
    >>,
    {ok, pt:encode(15719, Data, Src_Data)};

write (15720,[
    Errcode,
    Module,
    SubModule,
    AcSub,
    Status
] = Src_Data) ->
    Data = <<
        Errcode:32,
        Module:16,
        SubModule:8,
        AcSub:8,
        Status:8
    >>,
    {ok, pt:encode(15720, Data, Src_Data)};

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


item_to_bin_0 ({
    Module,
    ModuleSub,
    AcSub,
    Num,
    MaxTimes,
    Live,
    State
}) ->
    Data = <<
        Module:32,
        ModuleSub:32,
        AcSub:32,
        Num:32,
        MaxTimes:32,
        Live:32,
        State:8
    >>,
    Data.
item_to_bin_1 ({
    Module,
    ModuleSub,
    AcSub,
    Status,
    EndTime
}) ->
    Data = <<
        Module:32,
        ModuleSub:32,
        AcSub:32,
        Status:8,
        EndTime:32
    >>,
    Data.
item_to_bin_2 ({
    Id,
    State
}) ->
    Data = <<
        Id:32,
        State:8
    >>,
    Data.
item_to_bin_3 ({
    Module,
    SubModule,
    AcSub,
    Status
}) ->
    Data = <<
        Module:16,
        SubModule:8,
        AcSub:8,
        Status:8
    >>,
    Data.
