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

read(45001, _) ->
    {ok, []};
read(45002, Bin0) ->
    <<CardType:32, _Bin1/binary>> = Bin0, 
    {ok, [CardType]};
read(45003, _) ->
    {ok, []};
read(45004, _) ->
    {ok, []};
read(45005, _) ->
    {ok, []};
read(45006, Bin0) ->
    <<VipLv:16, _Bin1/binary>> = Bin0, 
    {ok, [VipLv]};
read(45007, _) ->
    {ok, []};
read(45008, Bin0) ->
    <<VipLv:8, _Bin1/binary>> = Bin0, 
    {ok, [VipLv]};
read(45009, Bin0) ->
    <<VipLv:8, _Bin1/binary>> = Bin0, 
    {ok, [VipLv]};
read(45010, _) ->
    {ok, []};
read(45011, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

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

write (45001,[
    VipLv,
    ExperienceVipLv,
    VipExp,
    VipEndtime,
    VipAwardList
] = Src_Data) ->
    BinList_VipAwardList = [
        item_to_bin_0(VipAwardList_Item) || VipAwardList_Item <- VipAwardList
    ], 

    VipAwardList_Len = length(VipAwardList), 
    Bin_VipAwardList = list_to_binary(BinList_VipAwardList),

    Data = <<
        VipLv:16,
        ExperienceVipLv:16,
        VipExp:32,
        VipEndtime:32,
        VipAwardList_Len:16, Bin_VipAwardList/binary
    >>,
    {ok, pt:encode(45001, Data, Src_Data)};

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

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

write (45004,[
    Display,
    DisplayCode,
    Medium,
    LinkInfo1,
    LinkInfo2,
    CodePath,
    Remark,
    WorkTime
] = Src_Data) ->
    Bin_Medium = pt:write_string(Medium), 

    Bin_LinkInfo1 = pt:write_string(LinkInfo1), 

    Bin_LinkInfo2 = pt:write_string(LinkInfo2), 

    Bin_CodePath = pt:write_string(CodePath), 

    Bin_Remark = pt:write_string(Remark), 

    Bin_WorkTime = pt:write_string(WorkTime), 

    Data = <<
        Display:8,
        DisplayCode:8,
        Bin_Medium/binary,
        Bin_LinkInfo1/binary,
        Bin_LinkInfo2/binary,
        Bin_CodePath/binary,
        Bin_Remark/binary,
        Bin_WorkTime/binary
    >>,
    {ok, pt:encode(45004, Data, Src_Data)};

write (45005,[
    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(45005, Data, Src_Data)};

write (45006,[
    Res,
    RewardList
] = Src_Data) ->
    Bin_RewardList = pt:write_object_list(RewardList), 

    Data = <<
        Res:32,
        Bin_RewardList/binary
    >>,
    {ok, pt:encode(45006, Data, Src_Data)};

write (45007,[
    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(45007, Data, Src_Data)};

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

write (45009,[
    Res,
    VipLv,
    Rewards
] = Src_Data) ->
    BinList_Rewards = [
        item_to_bin_3(Rewards_Item) || Rewards_Item <- Rewards
    ], 

    Rewards_Len = length(Rewards), 
    Bin_Rewards = list_to_binary(BinList_Rewards),

    Data = <<
        Res:32,
        VipLv:8,
        Rewards_Len:16, Bin_Rewards/binary
    >>,
    {ok, pt:encode(45009, Data, Src_Data)};

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

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

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


item_to_bin_0 (
    NotGot
) ->
    Data = <<
        NotGot:8
    >>,
    Data.
item_to_bin_1 ({
    VipLv,
    State
}) ->
    Data = <<
        VipLv:8,
        State:8
    >>,
    Data.
item_to_bin_2 (
    VipLv
) ->
    Data = <<
        VipLv:8
    >>,
    Data.
item_to_bin_3 ({
    GoodsType,
    GoodsId,
    Num
}) ->
    Data = <<
        GoodsType:16,
        GoodsId:32,
        Num:32
    >>,
    Data.
