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

read(41900, _) ->
    {ok, []};
read(41902, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(41903, Bin0) ->
    <<ActId:32, Bin1/binary>> = Bin0, 
    <<ActSub:16, Bin2/binary>> = Bin1, 
    <<Type:8, Bin3/binary>> = Bin2, 
    <<Times:16, _Bin4/binary>> = Bin3, 
    {ok, [ActId, ActSub, Type, Times]};
read(_Cmd, _R) -> {error, no_match}.

write (41900,[
    Errcode,
    ResAct
] = Src_Data) ->
    BinList_ResAct = [
        item_to_bin_0(ResAct_Item) || ResAct_Item <- ResAct
    ], 

    ResAct_Len = length(ResAct), 
    Bin_ResAct = list_to_binary(BinList_ResAct),

    Data = <<
        Errcode:32,
        ResAct_Len:16, Bin_ResAct/binary
    >>,
    {ok, pt:encode(41900, Data, Src_Data)};

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

write (41903,[
    Errcode,
    ActId,
    ActSub,
    Lefttimes,
    RewardLv
] = Src_Data) ->
    Data = <<
        Errcode:32,
        ActId:32,
        ActSub:16,
        Lefttimes:16,
        RewardLv:32
    >>,
    {ok, pt:encode(41903, Data, Src_Data)};

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


item_to_bin_0 ({
    ActId,
    ActSub,
    Lefttimes,
    RewardLv,
    Ratio
}) ->
    Data = <<
        ActId:32,
        ActSub:16,
        Lefttimes:16,
        RewardLv:32,
        Ratio:16
    >>,
    Data.
