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

read(14600, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(14601, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<AutoUp:8, Bin2/binary>> = Bin1, 
    <<AutoBuy:8, Bin3/binary>> = Bin2, 
    <<AutoBgold:8, _Bin4/binary>> = Bin3, 
    {ok, [Type, AutoUp, AutoBuy, AutoBgold]};
read(14602, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Opty:8, Bin2/binary>> = Bin1, 
    <<Num:8, _Bin3/binary>> = Bin2, 
    {ok, [Type, Opty, Num]};
read(14603, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Display:16, _Bin2/binary>> = Bin1, 
    {ok, [Type, Display]};
read(14604, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Pos:8, _Bin2/binary>> = Bin1, 
    {ok, [Type, Pos]};
read(14605, _) ->
    {ok, []};
read(14606, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Grade:16, Bin2/binary>> = Bin1, 
    <<Pos:8, Bin3/binary>> = Bin2, 
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<GoodsId:64, _Args01/binary>> = RestBin0, 
        {GoodsId,_Args01}
        end,
    {Eats, _Bin4} = pt:read_array(FunArray0, Bin3),

    {ok, [Type, Grade, Pos, Eats]};
read(14607, Bin0) ->
    <<Opty:8, Bin1/binary>> = Bin0, 
    <<Type:8, Bin2/binary>> = Bin1, 
    <<Grade:16, Bin3/binary>> = Bin2, 
    <<GoodsId:64, _Bin4/binary>> = Bin3, 
    {ok, [Opty, Type, Grade, GoodsId]};
read(14608, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Grade:16, _Bin2/binary>> = Bin1, 
    {ok, [Type, Grade]};
read(14609, Bin0) ->
    <<Opty:8, _Bin1/binary>> = Bin0, 
    {ok, [Opty]};
read(14610, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(14611, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Id:16, Bin2/binary>> = Bin1, 
    <<Opty:8, _Bin3/binary>> = Bin2, 
    {ok, [Type, Id, Opty]};
read(14612, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Skin:16, Bin2/binary>> = Bin1, 
    <<Star:16, _Bin3/binary>> = Bin2, 
    {ok, [Type, Skin, Star]};
read(14614, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(14615, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Grade:16, _Bin2/binary>> = Bin1, 
    {ok, [Type, Grade]};
read(14616, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Grade:16, _Bin2/binary>> = Bin1, 
    {ok, [Type, Grade]};
read(14617, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(14618, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Id:16, Bin2/binary>> = Bin1, 
    <<Option:8, _Bin3/binary>> = Bin2, 
    {ok, [Type, Id, Option]};
read(14619, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Id:16, Bin2/binary>> = Bin1, 
    <<Star:16, _Bin3/binary>> = Bin2, 
    {ok, [Type, Id, Star]};
read(14620, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<MaterialType:8, Bin2/binary>> = Bin1, 
    <<AutoUp:8, _Bin3/binary>> = Bin2, 
    {ok, [Type, MaterialType, AutoUp]};
read(_Cmd, _R) -> {error, no_match}.

write (14600,[
    Type,
    Grade,
    Star,
    Lv,
    Exp,
    Click,
    Isclear,
    Display,
    CoreList,
    Retime,
    Combat,
    Skills,
    GradeEquips,
    Attrs
] = Src_Data) ->
    BinList_CoreList = [
        item_to_bin_0(CoreList_Item) || CoreList_Item <- CoreList
    ], 

    CoreList_Len = length(CoreList), 
    Bin_CoreList = list_to_binary(BinList_CoreList),

    BinList_Skills = [
        item_to_bin_1(Skills_Item) || Skills_Item <- Skills
    ], 

    Skills_Len = length(Skills), 
    Bin_Skills = list_to_binary(BinList_Skills),

    BinList_GradeEquips = [
        item_to_bin_2(GradeEquips_Item) || GradeEquips_Item <- GradeEquips
    ], 

    GradeEquips_Len = length(GradeEquips), 
    Bin_GradeEquips = list_to_binary(BinList_GradeEquips),

    BinList_Attrs = [
        item_to_bin_4(Attrs_Item) || Attrs_Item <- Attrs
    ], 

    Attrs_Len = length(Attrs), 
    Bin_Attrs = list_to_binary(BinList_Attrs),

    Data = <<
        Type:8,
        Grade:16,
        Star:16,
        Lv:16,
        Exp:16,
        Click:16,
        Isclear:8,
        Display:16,
        CoreList_Len:16, Bin_CoreList/binary,
        Retime:32,
        Combat:64,
        Skills_Len:16, Bin_Skills/binary,
        GradeEquips_Len:16, Bin_GradeEquips/binary,
        Attrs_Len:16, Bin_Attrs/binary
    >>,
    {ok, pt:encode(14600, Data, Src_Data)};

write (14601,[
    Result,
    AutoUp,
    AutoBuy,
    AutoBgold,
    Type,
    Grade,
    Star,
    Islucky,
    Exp
] = Src_Data) ->
    Data = <<
        Result:32,
        AutoUp:8,
        AutoBuy:8,
        AutoBgold:8,
        Type:8,
        Grade:16,
        Star:16,
        Islucky:8,
        Exp:8
    >>,
    {ok, pt:encode(14601, Data, Src_Data)};

write (14602,[
    Result,
    Opty,
    Type,
    Total
] = Src_Data) ->
    Data = <<
        Result:32,
        Opty:8,
        Type:8,
        Total:16
    >>,
    {ok, pt:encode(14602, Data, Src_Data)};

write (14603,[
    Result,
    Type,
    Display
] = Src_Data) ->
    Data = <<
        Result:32,
        Type:8,
        Display:16
    >>,
    {ok, pt:encode(14603, Data, Src_Data)};

write (14604,[
    Result,
    Type,
    Pos,
    Lv,
    Skill
] = Src_Data) ->
    Data = <<
        Result:32,
        Type:8,
        Pos:8,
        Lv:8,
        Skill:32
    >>,
    {ok, pt:encode(14604, Data, Src_Data)};

write (14605,[
    Type,
    Pos,
    Lv,
    Skill
] = Src_Data) ->
    Data = <<
        Type:8,
        Pos:8,
        Lv:8,
        Skill:32
    >>,
    {ok, pt:encode(14605, Data, Src_Data)};

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

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

write (14608,[
    Type,
    Grade,
    Equips
] = Src_Data) ->
    BinList_Equips = [
        item_to_bin_5(Equips_Item) || Equips_Item <- Equips
    ], 

    Equips_Len = length(Equips), 
    Bin_Equips = list_to_binary(BinList_Equips),

    Data = <<
        Type:8,
        Grade:16,
        Equips_Len:16, Bin_Equips/binary
    >>,
    {ok, pt:encode(14608, Data, Src_Data)};

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

write (14610,[
    Type,
    Skins
] = Src_Data) ->
    BinList_Skins = [
        item_to_bin_6(Skins_Item) || Skins_Item <- Skins
    ], 

    Skins_Len = length(Skins), 
    Bin_Skins = list_to_binary(BinList_Skins),

    Data = <<
        Type:8,
        Skins_Len:16, Bin_Skins/binary
    >>,
    {ok, pt:encode(14610, Data, Src_Data)};

write (14611,[
    Type,
    Id,
    Star,
    Opty,
    Result
] = Src_Data) ->
    Data = <<
        Type:8,
        Id:16,
        Star:16,
        Opty:8,
        Result:32
    >>,
    {ok, pt:encode(14611, Data, Src_Data)};

write (14612,[
    Result,
    Type,
    Skin,
    Star
] = Src_Data) ->
    Data = <<
        Result:32,
        Type:8,
        Skin:16,
        Star:16
    >>,
    {ok, pt:encode(14612, Data, Src_Data)};

write (14613,[
    Type,
    Grade,
    Star,
    Awards
] = Src_Data) ->
    BinList_Awards = [
        item_to_bin_7(Awards_Item) || Awards_Item <- Awards
    ], 

    Awards_Len = length(Awards), 
    Bin_Awards = list_to_binary(BinList_Awards),

    Data = <<
        Type:8,
        Grade:16,
        Star:8,
        Awards_Len:16, Bin_Awards/binary
    >>,
    {ok, pt:encode(14613, Data, Src_Data)};

write (14614,[
    Type,
    GradeList
] = Src_Data) ->
    BinList_GradeList = [
        item_to_bin_8(GradeList_Item) || GradeList_Item <- GradeList
    ], 

    GradeList_Len = length(GradeList), 
    Bin_GradeList = list_to_binary(BinList_GradeList),

    Data = <<
        Type:8,
        GradeList_Len:16, Bin_GradeList/binary
    >>,
    {ok, pt:encode(14614, Data, Src_Data)};

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

write (14616,[
    Errcode,
    Type,
    Grade
] = Src_Data) ->
    Data = <<
        Errcode:32,
        Type:8,
        Grade:16
    >>,
    {ok, pt:encode(14616, Data, Src_Data)};

write (14617,[
    Type,
    Skins
] = Src_Data) ->
    BinList_Skins = [
        item_to_bin_9(Skins_Item) || Skins_Item <- Skins
    ], 

    Skins_Len = length(Skins), 
    Bin_Skins = list_to_binary(BinList_Skins),

    Data = <<
        Type:8,
        Skins_Len:16, Bin_Skins/binary
    >>,
    {ok, pt:encode(14617, Data, Src_Data)};

write (14618,[
    Result,
    Type,
    Id,
    Option,
    Star
] = Src_Data) ->
    Data = <<
        Result:32,
        Type:8,
        Id:16,
        Option:8,
        Star:16
    >>,
    {ok, pt:encode(14618, Data, Src_Data)};

write (14619,[
    Result,
    Type,
    Id,
    Star
] = Src_Data) ->
    Data = <<
        Result:32,
        Type:8,
        Id:16,
        Star:16
    >>,
    {ok, pt:encode(14619, Data, Src_Data)};

write (14620,[
    Result,
    Type,
    MaterialType,
    Lv,
    Exp
] = Src_Data) ->
    Data = <<
        Result:32,
        Type:8,
        MaterialType:8,
        Lv:16,
        Exp:32
    >>,
    {ok, pt:encode(14620, Data, Src_Data)};

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

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


item_to_bin_0 ({
    CoreType,
    CoreNum
}) ->
    Data = <<
        CoreType:8,
        CoreNum:16
    >>,
    Data.
item_to_bin_1 ({
    Pos,
    Lv,
    Skill
}) ->
    Data = <<
        Pos:8,
        Lv:8,
        Skill:32
    >>,
    Data.
item_to_bin_2 ({
    Grade,
    Equips
}) ->
    BinList_Equips = [
        item_to_bin_3(Equips_Item) || Equips_Item <- Equips
    ], 

    Equips_Len = length(Equips), 
    Bin_Equips = list_to_binary(BinList_Equips),

    Data = <<
        Grade:16,
        Equips_Len:16, Bin_Equips/binary
    >>,
    Data.
item_to_bin_3 ({
    Pos,
    GoodsId,
    TypeId,
    Color,
    Lv,
    Exp
}) ->
    Data = <<
        Pos:8,
        GoodsId:64,
        TypeId:32,
        Color:8,
        Lv:16,
        Exp:32
    >>,
    Data.
item_to_bin_4 ({
    AttType,
    AttValue
}) ->
    Data = <<
        AttType:8,
        AttValue:32
    >>,
    Data.
item_to_bin_5 ({
    Pos,
    GoodsId,
    TypeId,
    Color,
    Lv,
    Exp
}) ->
    Data = <<
        Pos:8,
        GoodsId:64,
        TypeId:32,
        Color:8,
        Lv:16,
        Exp:32
    >>,
    Data.
item_to_bin_6 ({
    Id,
    Star,
    Display
}) ->
    Data = <<
        Id:16,
        Star:16,
        Display:16
    >>,
    Data.
item_to_bin_7 ({
    Kdata,
    Vdata
}) ->
    Data = <<
        Kdata:32,
        Vdata:16
    >>,
    Data.
item_to_bin_8 ({
    Grade,
    State
}) ->
    Data = <<
        Grade:16,
        State:8
    >>,
    Data.
item_to_bin_9 ({
    Id,
    Star,
    Display
}) ->
    Data = <<
        Id:16,
        Star:16,
        Display:16
    >>,
    Data.
