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

read(17301, _) ->
    {ok, []};
read(17302, Bin0) ->
    <<Id:16, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(17303, Bin0) ->
    <<Id:16, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(17304, Bin0) ->
    <<Id:16, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(17305, Bin0) ->
    <<Id:16, Bin1/binary>> = Bin0, 
    <<UpType:8, Bin2/binary>> = Bin1, 
    <<GoodsTypeId:32, Bin3/binary>> = Bin2, 
    <<GoodsNum:32, _Bin4/binary>> = Bin3, 
    {ok, [Id, UpType, GoodsTypeId, GoodsNum]};
read(17306, Bin0) ->
    <<Id:16, Bin1/binary>> = Bin0, 
    <<SkillId:32, _Bin2/binary>> = Bin1, 
    {ok, [Id, SkillId]};
read(17307, Bin0) ->
    <<Id:16, Bin1/binary>> = Bin0, 
    <<Pos:8, Bin2/binary>> = Bin1, 
    <<GoodsId:64, _Bin3/binary>> = Bin2, 
    {ok, [Id, Pos, GoodsId]};
read(17308, Bin0) ->
    <<Id:16, Bin1/binary>> = Bin0, 
    <<Pos:8, _Bin2/binary>> = Bin1, 
    {ok, [Id, Pos]};
read(17309, Bin0) ->
    <<GoodsId:64, Bin1/binary>> = Bin0, 
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<CostGoodsId:64, _Args01/binary>> = RestBin0, 
        <<CostGoodsNum:16, _Args02/binary>> = _Args01, 
        {{CostGoodsId, CostGoodsNum},_Args02}
        end,
    {CostList, _Bin2} = pt:read_array(FunArray0, Bin1),

    {ok, [GoodsId, CostList]};
read(17310, Bin0) ->
    <<Pos:16, _Bin1/binary>> = Bin0, 
    {ok, [Pos]};
read(17311, Bin0) ->
    <<Pos:16, Bin1/binary>> = Bin0, 
    <<Id:16, _Bin2/binary>> = Bin1, 
    {ok, [Pos, Id]};
read(17312, _) ->
    {ok, []};
read(17313, Bin0) ->
    <<GodId:16, _Bin1/binary>> = Bin0, 
    {ok, [GodId]};
read(17315, _) ->
    {ok, []};
read(17316, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<AutoBuy:8, _Bin2/binary>> = Bin1, 
    {ok, [Type, AutoBuy]};
read(17317, Bin0) ->
    <<AwardId:8, _Bin1/binary>> = Bin0, 
    {ok, [AwardId]};
read(17318, Bin0) ->
    <<Stage:8, _Bin1/binary>> = Bin0, 
    {ok, [Stage]};
read(17319, _) ->
    {ok, []};
read(17321, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (17301,[
    TotalPower,
    List,
    Unlock
] = Src_Data) ->
    BinList_List = [
        item_to_bin_0(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    BinList_Unlock = [
        item_to_bin_2(Unlock_Item) || Unlock_Item <- Unlock
    ], 

    Unlock_Len = length(Unlock), 
    Bin_Unlock = list_to_binary(BinList_Unlock),

    Data = <<
        TotalPower:64,
        List_Len:16, Bin_List/binary,
        Unlock_Len:16, Bin_Unlock/binary
    >>,
    {ok, pt:encode(17301, Data, Src_Data)};

write (17302,[
    Id,
    Star,
    Lv,
    Exp,
    Power,
    Skill,
    Pos
] = Src_Data) ->
    BinList_Skill = [
        item_to_bin_3(Skill_Item) || Skill_Item <- Skill
    ], 

    Skill_Len = length(Skill), 
    Bin_Skill = list_to_binary(BinList_Skill),

    Data = <<
        Id:16,
        Star:16,
        Lv:16,
        Exp:32,
        Power:32,
        Skill_Len:16, Bin_Skill/binary,
        Pos:16
    >>,
    {ok, pt:encode(17302, Data, Src_Data)};

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

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

write (17305,[
    Res,
    Id,
    Lv,
    Exp,
    IsUpLv
] = Src_Data) ->
    Data = <<
        Res:32,
        Id:16,
        Lv:16,
        Exp:16,
        IsUpLv:8
    >>,
    {ok, pt:encode(17305, Data, Src_Data)};

write (17306,[
    Res,
    Id,
    SkillId,
    SkillLv
] = Src_Data) ->
    Data = <<
        Res:32,
        Id:16,
        SkillId:32,
        SkillLv:8
    >>,
    {ok, pt:encode(17306, Data, Src_Data)};

write (17307,[
    Res,
    Id,
    Pos,
    GoodsId
] = Src_Data) ->
    Data = <<
        Res:32,
        Id:16,
        Pos:8,
        GoodsId:64
    >>,
    {ok, pt:encode(17307, Data, Src_Data)};

write (17308,[
    Res,
    Id,
    Pos
] = Src_Data) ->
    Data = <<
        Res:32,
        Id:16,
        Pos:8
    >>,
    {ok, pt:encode(17308, Data, Src_Data)};

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

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

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

write (17312,[
    SummonList
] = Src_Data) ->
    BinList_SummonList = [
        item_to_bin_4(SummonList_Item) || SummonList_Item <- SummonList
    ], 

    SummonList_Len = length(SummonList), 
    Bin_SummonList = list_to_binary(BinList_SummonList),

    Data = <<
        SummonList_Len:16, Bin_SummonList/binary
    >>,
    {ok, pt:encode(17312, Data, Src_Data)};

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

write (17315,[
    Phase,
    BeginTime,
    EndTime,
    Counts,
    Blessing,
    BlessAward,
    CountsAward
] = Src_Data) ->
    BinList_BlessAward = [
        item_to_bin_5(BlessAward_Item) || BlessAward_Item <- BlessAward
    ], 

    BlessAward_Len = length(BlessAward), 
    Bin_BlessAward = list_to_binary(BinList_BlessAward),

    BinList_CountsAward = [
        item_to_bin_6(CountsAward_Item) || CountsAward_Item <- CountsAward
    ], 

    CountsAward_Len = length(CountsAward), 
    Bin_CountsAward = list_to_binary(BinList_CountsAward),

    Data = <<
        Phase:8,
        BeginTime:32,
        EndTime:32,
        Counts:32,
        Blessing:32,
        BlessAward_Len:16, Bin_BlessAward/binary,
        CountsAward_Len:16, Bin_CountsAward/binary
    >>,
    {ok, pt:encode(17315, Data, Src_Data)};

write (17316,[
    Res,
    Type,
    AwardList,
    Blessing
] = Src_Data) ->
    BinList_AwardList = [
        item_to_bin_7(AwardList_Item) || AwardList_Item <- AwardList
    ], 

    AwardList_Len = length(AwardList), 
    Bin_AwardList = list_to_binary(BinList_AwardList),

    Data = <<
        Res:32,
        Type:8,
        AwardList_Len:16, Bin_AwardList/binary,
        Blessing:16
    >>,
    {ok, pt:encode(17316, Data, Src_Data)};

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

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

write (17319,[
    RoleId,
    GodId,
    Hp,
    HpLim
] = Src_Data) ->
    Data = <<
        RoleId:64,
        GodId:16,
        Hp:64,
        HpLim:64
    >>,
    {ok, pt:encode(17319, Data, Src_Data)};

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

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

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


item_to_bin_0 ({
    Id,
    Star,
    Lv,
    Exp,
    Power,
    Skill,
    Pos
}) ->
    BinList_Skill = [
        item_to_bin_1(Skill_Item) || Skill_Item <- Skill
    ], 

    Skill_Len = length(Skill), 
    Bin_Skill = list_to_binary(BinList_Skill),

    Data = <<
        Id:16,
        Star:16,
        Lv:16,
        Exp:32,
        Power:32,
        Skill_Len:16, Bin_Skill/binary,
        Pos:16
    >>,
    Data.
item_to_bin_1 ({
    SkillId,
    SkillLv
}) ->
    Data = <<
        SkillId:32,
        SkillLv:8
    >>,
    Data.
item_to_bin_2 (
    Pos
) ->
    Data = <<
        Pos:16
    >>,
    Data.
item_to_bin_3 ({
    SkillId,
    SkillLv
}) ->
    Data = <<
        SkillId:32,
        SkillLv:8
    >>,
    Data.
item_to_bin_4 ({
    Pos,
    Time
}) ->
    Data = <<
        Pos:16,
        Time:32
    >>,
    Data.
item_to_bin_5 ({
    Stage,
    State
}) ->
    Data = <<
        Stage:8,
        State:8
    >>,
    Data.
item_to_bin_6 (
    AwardId
) ->
    Data = <<
        AwardId:8
    >>,
    Data.
item_to_bin_7 ({
    GoodsId,
    GoodsNum
}) ->
    Data = <<
        GoodsId:32,
        GoodsNum:16
    >>,
    Data.
