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

read(16301, _) ->
    {ok, []};
read(16302, Bin0) ->
    <<PetId:32, Bin1/binary>> = Bin0, 
    <<FeedGoodId:32, Bin2/binary>> = Bin1, 
    <<GoodNum:16, _Bin3/binary>> = Bin2, 
    {ok, [PetId, FeedGoodId, GoodNum]};
read(16303, Bin0) ->
    <<PetId:32, Bin1/binary>> = Bin0, 
    <<ActionType:8, _Bin2/binary>> = Bin1, 
    {ok, [PetId, ActionType]};
read(16304, Bin0) ->
    <<PetId:32, _Bin1/binary>> = Bin0, 
    {ok, [PetId]};
read(16305, Bin0) ->
    <<PetId:32, Bin1/binary>> = Bin0, 
    <<DiamondSlot:8, Bin2/binary>> = Bin1, 
    <<DiamondId:32, Bin3/binary>> = Bin2, 
    <<GoodsId:64, _Bin4/binary>> = Bin3, 
    {ok, [PetId, DiamondSlot, DiamondId, GoodsId]};
read(16307, _) ->
    {ok, []};
read(16308, Bin0) ->
    <<PetId:32, Bin1/binary>> = Bin0, 
    <<SupportSlot:8, _Bin2/binary>> = Bin1, 
    {ok, [PetId, SupportSlot]};
read(16309, _) ->
    {ok, []};
read(16310, _) ->
    {ok, []};
read(16311, Bin0) ->
    <<RoleId:64, Bin1/binary>> = Bin0, 
    <<PetId:32, _Bin2/binary>> = Bin1, 
    {ok, [RoleId, PetId]};
read(16312, _) ->
    {ok, []};
read(16313, _) ->
    {ok, []};
read(16314, Bin0) ->
    <<PetId:32, _Bin1/binary>> = Bin0, 
    {ok, [PetId]};
read(16315, Bin0) ->
    <<PetId:32, _Bin1/binary>> = Bin0, 
    {ok, [PetId]};
read(16316, Bin0) ->
    <<PetId:32, _Bin1/binary>> = Bin0, 
    {ok, [PetId]};
read(16318, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<DiamondId:32, _Bin2/binary>> = Bin1, 
    {ok, [Type, DiamondId]};
read(_Cmd, _R) -> {error, no_match}.

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

write (16301,[
    FollowPet,
    PetList
] = Src_Data) ->
    BinList_PetList = [
        item_to_bin_0(PetList_Item) || PetList_Item <- PetList
    ], 

    PetList_Len = length(PetList), 
    Bin_PetList = list_to_binary(BinList_PetList),

    Data = <<
        FollowPet:32,
        PetList_Len:16, Bin_PetList/binary
    >>,
    {ok, pt:encode(16301, Data, Src_Data)};

write (16302,[
    PetId,
    PetLv,
    NowExp,
    PetPower
] = Src_Data) ->
    Data = <<
        PetId:32,
        PetLv:16,
        NowExp:32,
        PetPower:64
    >>,
    {ok, pt:encode(16302, Data, Src_Data)};

write (16303,[
    ActionType,
    PetId,
    PetStar,
    PetPower
] = Src_Data) ->
    Data = <<
        ActionType:8,
        PetId:32,
        PetStar:8,
        PetPower:64
    >>,
    {ok, pt:encode(16303, Data, Src_Data)};

write (16304,[
    PetId,
    SkillList,
    DiamondList
] = Src_Data) ->
    BinList_SkillList = [
        item_to_bin_2(SkillList_Item) || SkillList_Item <- SkillList
    ], 

    SkillList_Len = length(SkillList), 
    Bin_SkillList = list_to_binary(BinList_SkillList),

    BinList_DiamondList = [
        item_to_bin_3(DiamondList_Item) || DiamondList_Item <- DiamondList
    ], 

    DiamondList_Len = length(DiamondList), 
    Bin_DiamondList = list_to_binary(BinList_DiamondList),

    Data = <<
        PetId:32,
        SkillList_Len:16, Bin_SkillList/binary,
        DiamondList_Len:16, Bin_DiamondList/binary
    >>,
    {ok, pt:encode(16304, Data, Src_Data)};

write (16305,[
    PetId,
    DiamondSlot,
    DiamondId,
    GoodsId,
    PetPower
] = Src_Data) ->
    Data = <<
        PetId:32,
        DiamondSlot:8,
        DiamondId:32,
        GoodsId:64,
        PetPower:64
    >>,
    {ok, pt:encode(16305, Data, Src_Data)};

write (16306,[
    RoleId,
    PetId,
    PetStar,
    IsFollow
] = Src_Data) ->
    Data = <<
        RoleId:64,
        PetId:32,
        PetStar:8,
        IsFollow:8
    >>,
    {ok, pt:encode(16306, Data, Src_Data)};

write (16307,[
    PetSumStar,
    PetList
] = Src_Data) ->
    BinList_PetList = [
        item_to_bin_4(PetList_Item) || PetList_Item <- PetList
    ], 

    PetList_Len = length(PetList), 
    Bin_PetList = list_to_binary(BinList_PetList),

    Data = <<
        PetSumStar:16,
        PetList_Len:16, Bin_PetList/binary
    >>,
    {ok, pt:encode(16307, Data, Src_Data)};

write (16308,[
    PetId,
    SupportSlot,
    OldPetId,
    OldSupportSlot
] = Src_Data) ->
    Data = <<
        PetId:32,
        SupportSlot:8,
        OldPetId:32,
        OldSupportSlot:8
    >>,
    {ok, pt:encode(16308, Data, Src_Data)};

write (16309,[
    RankList
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_5(RankList_Item) || RankList_Item <- RankList
    ], 

    RankList_Len = length(RankList), 
    Bin_RankList = list_to_binary(BinList_RankList),

    Data = <<
        RankList_Len:16, Bin_RankList/binary
    >>,
    {ok, pt:encode(16309, Data, Src_Data)};

write (16310,[
    ReceiveList,
    PetNum
] = Src_Data) ->
    BinList_ReceiveList = [
        item_to_bin_6(ReceiveList_Item) || ReceiveList_Item <- ReceiveList
    ], 

    ReceiveList_Len = length(ReceiveList), 
    Bin_ReceiveList = list_to_binary(BinList_ReceiveList),

    Data = <<
        ReceiveList_Len:16, Bin_ReceiveList/binary,
        PetNum:8
    >>,
    {ok, pt:encode(16310, Data, Src_Data)};

write (16311,[
    PetId,
    PetStar,
    PetLv,
    AttrList,
    PetPower,
    Picture
] = Src_Data) ->
    BinList_AttrList = [
        item_to_bin_7(AttrList_Item) || AttrList_Item <- AttrList
    ], 

    AttrList_Len = length(AttrList), 
    Bin_AttrList = list_to_binary(BinList_AttrList),

    Bin_Picture = pt:write_picture(Picture), 

    Data = <<
        PetId:32,
        PetStar:8,
        PetLv:16,
        AttrList_Len:16, Bin_AttrList/binary,
        PetPower:64,
        Bin_Picture/binary
    >>,
    {ok, pt:encode(16311, Data, Src_Data)};

write (16312,[
    RankList
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_8(RankList_Item) || RankList_Item <- RankList
    ], 

    RankList_Len = length(RankList), 
    Bin_RankList = list_to_binary(BinList_RankList),

    Data = <<
        RankList_Len:16, Bin_RankList/binary
    >>,
    {ok, pt:encode(16312, Data, Src_Data)};

write (16313,[
    MyTicket,
    SupportTimes,
    PetNum
] = Src_Data) ->
    BinList_MyTicket = [
        item_to_bin_9(MyTicket_Item) || MyTicket_Item <- MyTicket
    ], 

    MyTicket_Len = length(MyTicket), 
    Bin_MyTicket = list_to_binary(BinList_MyTicket),

    Data = <<
        MyTicket_Len:16, Bin_MyTicket/binary,
        SupportTimes:8,
        PetNum:8
    >>,
    {ok, pt:encode(16313, Data, Src_Data)};

write (16314,[
    MyTicket,
    SupportTimes
] = Src_Data) ->
    BinList_MyTicket = [
        item_to_bin_10(MyTicket_Item) || MyTicket_Item <- MyTicket
    ], 

    MyTicket_Len = length(MyTicket), 
    Bin_MyTicket = list_to_binary(BinList_MyTicket),

    Data = <<
        MyTicket_Len:16, Bin_MyTicket/binary,
        SupportTimes:8
    >>,
    {ok, pt:encode(16314, Data, Src_Data)};

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

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

write (16317,[
    RoleId,
    PetId,
    PetStar,
    IsTop,
    SkillId
] = Src_Data) ->
    Data = <<
        RoleId:64,
        PetId:32,
        PetStar:16,
        IsTop:8,
        SkillId:32
    >>,
    {ok, pt:encode(16317, Data, Src_Data)};

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

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


item_to_bin_0 ({
    PetId,
    PetStar,
    PetLv,
    NowExp,
    AttrList,
    Power,
    CollectTime,
    IsAwaken
}) ->
    BinList_AttrList = [
        item_to_bin_1(AttrList_Item) || AttrList_Item <- AttrList
    ], 

    AttrList_Len = length(AttrList), 
    Bin_AttrList = list_to_binary(BinList_AttrList),

    Data = <<
        PetId:32,
        PetStar:8,
        PetLv:16,
        NowExp:32,
        AttrList_Len:16, Bin_AttrList/binary,
        Power:64,
        CollectTime:32,
        IsAwaken:8
    >>,
    Data.
item_to_bin_1 ({
    AttrId,
    AttrValue
}) ->
    Data = <<
        AttrId:8,
        AttrValue:32
    >>,
    Data.
item_to_bin_2 ({
    SkillSlotId,
    TalentType,
    TalentId
}) ->
    Data = <<
        SkillSlotId:8,
        TalentType:8,
        TalentId:32
    >>,
    Data.
item_to_bin_3 ({
    DiamondSlot,
    DiamondId
}) ->
    Data = <<
        DiamondSlot:8,
        DiamondId:32
    >>,
    Data.
item_to_bin_4 ({
    SupportSlot,
    PetId
}) ->
    Data = <<
        SupportSlot:8,
        PetId:32
    >>,
    Data.
item_to_bin_5 ({
    Rank,
    PetId,
    PlayerId,
    PlayerName,
    VipFlag,
    SupVipType,
    Sex,
    DressId,
    PhotoId,
    ServerNum,
    SumPower
}) ->
    Bin_PlayerName = pt:write_string(PlayerName), 

    Data = <<
        Rank:8,
        PetId:32,
        PlayerId:64,
        Bin_PlayerName/binary,
        VipFlag:8,
        SupVipType:8,
        Sex:8,
        DressId:32,
        PhotoId:32,
        ServerNum:32,
        SumPower:64
    >>,
    Data.
item_to_bin_6 ({
    PetId,
    IsReceived
}) ->
    Data = <<
        PetId:32,
        IsReceived:8
    >>,
    Data.
item_to_bin_7 ({
    AttrId,
    AttrValue
}) ->
    Data = <<
        AttrId:8,
        AttrValue:32
    >>,
    Data.
item_to_bin_8 ({
    Rank,
    PetId,
    SumPower
}) ->
    Data = <<
        Rank:8,
        PetId:32,
        SumPower:64
    >>,
    Data.
item_to_bin_9 (
    PetId
) ->
    Data = <<
        PetId:32
    >>,
    Data.
item_to_bin_10 (
    PetId
) ->
    Data = <<
        PetId:32
    >>,
    Data.
