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

read(15200, _) ->
    {ok, []};
read(15201, Bin0) ->
    <<Series:8, Bin1/binary>> = Bin0, 
    <<Pos:8, Bin2/binary>> = Bin1, 
    <<GoodsId:64, _Bin3/binary>> = Bin2, 
    {ok, [Series, Pos, GoodsId]};
read(15202, Bin0) ->
    <<GoodsId:64, _Bin1/binary>> = Bin0, 
    {ok, [GoodsId]};
read(15203, Bin0) ->
    <<GoodsId:64, Bin1/binary>> = Bin0, 
    <<Location:8, Bin2/binary>> = Bin1, 
    <<AutoBuy:8, _Bin3/binary>> = Bin2, 
    {ok, [GoodsId, Location, AutoBuy]};
read(15204, Bin0) ->
    <<GoodsId:64, Bin1/binary>> = Bin0, 
    <<Location:8, Bin2/binary>> = Bin1, 
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<CostGoodsId:64, _Args01/binary>> = RestBin0, 
        {CostGoodsId,_Args01}
        end,
    {ExtraCost, Bin3} = pt:read_array(FunArray0, Bin2),

    FunArray1 = fun(<<RestBin1/binary>>) -> 
        <<Id:64, _Args101/binary>> = RestBin1, 
        {Id,_Args101}
        end,
    {MarketBuy, Bin4} = pt:read_array(FunArray1, Bin3),

    <<Auto:8, _Bin5/binary>> = Bin4, 
    {ok, [GoodsId, Location, ExtraCost, MarketBuy, Auto]};
read(15205, Bin0) ->
    <<FromGoodsId:64, Bin1/binary>> = Bin0, 
    <<FromLocation:8, Bin2/binary>> = Bin1, 
    <<ToGoodsId:64, Bin3/binary>> = Bin2, 
    <<ToLocation:8, _Bin4/binary>> = Bin3, 
    {ok, [FromGoodsId, FromLocation, ToGoodsId, ToLocation]};
read(15206, Bin0) ->
    <<Choice:8, _Bin1/binary>> = Bin0, 
    {ok, [Choice]};
read(15207, _) ->
    {ok, []};
read(15208, _) ->
    {ok, []};
read(15210, Bin0) ->
    <<Series:8, Bin1/binary>> = Bin0, 
    <<Pos:8, _Bin2/binary>> = Bin1, 
    {ok, [Series, Pos]};
read(15212, Bin0) ->
    <<GoodsId:32, Bin1/binary>> = Bin0, 
    <<Lv:8, Bin2/binary>> = Bin1, 
    <<NowRatio:16, _Bin3/binary>> = Bin2, 
    {ok, [GoodsId, Lv, NowRatio]};
read(15213, _) ->
    {ok, []};
read(15214, Bin0) ->
    <<Series:8, Bin1/binary>> = Bin0, 
    <<Pos:8, _Bin2/binary>> = Bin1, 
    {ok, [Series, Pos]};
read(15215, Bin0) ->
    <<Series:8, Bin1/binary>> = Bin0, 
    <<Pos:8, Bin2/binary>> = Bin1, 
    <<GoodsTypeId:32, _Bin3/binary>> = Bin2, 
    {ok, [Series, Pos, GoodsTypeId]};
read(15216, Bin0) ->
    <<Series:8, Bin1/binary>> = Bin0, 
    <<Pos:8, Bin2/binary>> = Bin1, 
    <<GoodsTypeId:32, _Bin3/binary>> = Bin2, 
    {ok, [Series, Pos, GoodsTypeId]};
read(15217, Bin0) ->
    <<Series:8, Bin1/binary>> = Bin0, 
    <<Pos:8, Bin2/binary>> = Bin1, 
    <<GoodsTypeId:32, _Bin3/binary>> = Bin2, 
    {ok, [Series, Pos, GoodsTypeId]};
read(15218, _) ->
    {ok, []};
read(15220, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<SubType:8, _Bin2/binary>> = Bin1, 
    {ok, [Type, SubType]};
read(15221, Bin0) ->
    <<Id:64, Bin1/binary>> = Bin0, 
    <<Status:8, _Bin2/binary>> = Bin1, 
    {ok, [Id, Status]};
read(15222, Bin0) ->
    <<Series:8, _Bin1/binary>> = Bin0, 
    {ok, [Series]};
read(15223, _) ->
    {ok, []};
read(15224, Bin0) ->
    <<Series:8, _Bin1/binary>> = Bin0, 
    {ok, [Series]};
read(15225, Bin0) ->
    <<Series:8, Bin1/binary>> = Bin0, 
    <<Pos:8, _Bin2/binary>> = Bin1, 
    {ok, [Series, Pos]};
read(15226, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (15200,[
    Stars,
    Series,
    StarsInfo
] = Src_Data) ->
    BinList_StarsInfo = [
        item_to_bin_0(StarsInfo_Item) || StarsInfo_Item <- StarsInfo
    ], 

    StarsInfo_Len = length(StarsInfo), 
    Bin_StarsInfo = list_to_binary(BinList_StarsInfo),

    Data = <<
        Stars:16,
        Series:8,
        StarsInfo_Len:16, Bin_StarsInfo/binary
    >>,
    {ok, pt:encode(15200, Data, Src_Data)};

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

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

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

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

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

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

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

write (15208,[
    SeriesList
] = Src_Data) ->
    BinList_SeriesList = [
        item_to_bin_1(SeriesList_Item) || SeriesList_Item <- SeriesList
    ], 

    SeriesList_Len = length(SeriesList), 
    Bin_SeriesList = list_to_binary(BinList_SeriesList),

    Data = <<
        SeriesList_Len:16, Bin_SeriesList/binary
    >>,
    {ok, pt:encode(15208, Data, Src_Data)};

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

write (15212,[
    Res,
    Lists
] = Src_Data) ->
    BinList_Lists = [
        item_to_bin_2(Lists_Item) || Lists_Item <- Lists
    ], 

    Lists_Len = length(Lists), 
    Bin_Lists = list_to_binary(BinList_Lists),

    Data = <<
        Res:32,
        Lists_Len:16, Bin_Lists/binary
    >>,
    {ok, pt:encode(15212, Data, Src_Data)};

write (15213,[
    SeriesList
] = Src_Data) ->
    BinList_SeriesList = [
        item_to_bin_3(SeriesList_Item) || SeriesList_Item <- SeriesList
    ], 

    SeriesList_Len = length(SeriesList), 
    Bin_SeriesList = list_to_binary(BinList_SeriesList),

    Data = <<
        SeriesList_Len:16, Bin_SeriesList/binary
    >>,
    {ok, pt:encode(15213, Data, Src_Data)};

write (15214,[
    Series,
    Pos,
    Gems
] = Src_Data) ->
    BinList_Gems = [
        item_to_bin_5(Gems_Item) || Gems_Item <- Gems
    ], 

    Gems_Len = length(Gems), 
    Bin_Gems = list_to_binary(BinList_Gems),

    Data = <<
        Series:8,
        Pos:8,
        Gems_Len:16, Bin_Gems/binary
    >>,
    {ok, pt:encode(15214, Data, Src_Data)};

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

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

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

write (15218,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_6(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(15218, Data, Src_Data)};

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

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

write (15221,[
    Id,
    Status
] = Src_Data) ->
    Data = <<
        Id:64,
        Status:8
    >>,
    {ok, pt:encode(15221, Data, Src_Data)};

write (15222,[
    Res,
    PosList
] = Src_Data) ->
    BinList_PosList = [
        item_to_bin_8(PosList_Item) || PosList_Item <- PosList
    ], 

    PosList_Len = length(PosList), 
    Bin_PosList = list_to_binary(BinList_PosList),

    Data = <<
        Res:32,
        PosList_Len:16, Bin_PosList/binary
    >>,
    {ok, pt:encode(15222, Data, Src_Data)};

write (15223,[
    GemsLvList
] = Src_Data) ->
    BinList_GemsLvList = [
        item_to_bin_9(GemsLvList_Item) || GemsLvList_Item <- GemsLvList
    ], 

    GemsLvList_Len = length(GemsLvList), 
    Bin_GemsLvList = list_to_binary(BinList_GemsLvList),

    Data = <<
        GemsLvList_Len:16, Bin_GemsLvList/binary
    >>,
    {ok, pt:encode(15223, Data, Src_Data)};

write (15224,[
    Res,
    Series,
    GemsLv
] = Src_Data) ->
    Data = <<
        Res:32,
        Series:8,
        GemsLv:16
    >>,
    {ok, pt:encode(15224, Data, Src_Data)};

write (15225,[
    Series,
    Pos,
    Lv,
    Exp
] = Src_Data) ->
    Data = <<
        Series:8,
        Pos:8,
        Lv:16,
        Exp:32
    >>,
    {ok, pt:encode(15225, Data, Src_Data)};

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

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


item_to_bin_0 ({
    Id,
    Choice
}) ->
    Data = <<
        Id:16,
        Choice:8
    >>,
    Data.
item_to_bin_1 ({
    Series,
    Pos,
    Lv
}) ->
    Data = <<
        Series:8,
        Pos:8,
        Lv:8
    >>,
    Data.
item_to_bin_2 ({
    Id,
    GoodsId,
    Price
}) ->
    Data = <<
        Id:64,
        GoodsId:32,
        Price:32
    >>,
    Data.
item_to_bin_3 ({
    Series,
    Pos,
    Gems
}) ->
    BinList_Gems = [
        item_to_bin_4(Gems_Item) || Gems_Item <- Gems
    ], 

    Gems_Len = length(Gems), 
    Bin_Gems = list_to_binary(BinList_Gems),

    Data = <<
        Series:8,
        Pos:8,
        Gems_Len:16, Bin_Gems/binary
    >>,
    Data.
item_to_bin_4 (
    GoodsId
) ->
    Data = <<
        GoodsId:32
    >>,
    Data.
item_to_bin_5 (
    GoodsId
) ->
    Data = <<
        GoodsId:32
    >>,
    Data.
item_to_bin_6 ({
    Type,
    SubType,
    EquipmentList,
    ActiveProgress
}) ->
    BinList_EquipmentList = [
        item_to_bin_7(EquipmentList_Item) || EquipmentList_Item <- EquipmentList
    ], 

    EquipmentList_Len = length(EquipmentList), 
    Bin_EquipmentList = list_to_binary(BinList_EquipmentList),

    Data = <<
        Type:8,
        SubType:8,
        EquipmentList_Len:16, Bin_EquipmentList/binary,
        ActiveProgress:8
    >>,
    Data.
item_to_bin_7 (
    GoodsId
) ->
    Data = <<
        GoodsId:32
    >>,
    Data.
item_to_bin_8 (
    Pos
) ->
    Data = <<
        Pos:8
    >>,
    Data.
item_to_bin_9 ({
    Series,
    GemsLv
}) ->
    Data = <<
        Series:8,
        GemsLv:16
    >>,
    Data.
