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

read(14800, Bin0) ->
    <<Loc:8, Bin1/binary>> = Bin0, 
    <<Uid:64, _Bin2/binary>> = Bin1, 
    {ok, [Loc, Uid]};
read(14801, Bin0) ->
    <<Opty:8, _Bin1/binary>> = Bin0, 
    {ok, [Opty]};
read(14802, Bin0) ->
    <<Id:8, Bin1/binary>> = Bin0, 
    <<Auto:8, _Bin2/binary>> = Bin1, 
    {ok, [Id, Auto]};
read(14803, Bin0) ->
    <<Pos:8, Bin1/binary>> = Bin0, 
    <<Uid:64, _Bin2/binary>> = Bin1, 
    {ok, [Pos, Uid]};
read(14804, Bin0) ->
    <<Uid:64, Bin1/binary>> = Bin0, 
    <<Pos:8, Bin2/binary>> = Bin1, 
    <<CostUid:64, _Bin3/binary>> = Bin2, 
    {ok, [Uid, Pos, CostUid]};
read(14805, Bin0) ->
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<Uid:64, _Args1/binary>> = RestBin0, 
        {Uid,_Args1}
        end,
    {Uids, _Bin1} = pt:read_array(FunArray0, Bin0),

    {ok, [Uids]};
read(14806, Bin0) ->
    <<Uid:64, _Bin1/binary>> = Bin0, 
    {ok, [Uid]};
read(14807, _) ->
    {ok, []};
read(14808, Bin0) ->
    <<Opty:8, Bin1/binary>> = Bin0, 
    <<Skin:16, _Bin2/binary>> = Bin1, 
    {ok, [Opty, Skin]};
read(14809, Bin0) ->
    <<Skin:16, _Bin1/binary>> = Bin0, 
    {ok, [Skin]};
read(14810, _) ->
    {ok, []};
read(14811, _) ->
    {ok, []};
read(14812, Bin0) ->
    <<Pos:8, _Bin1/binary>> = Bin0, 
    {ok, [Pos]};
read(14813, _) ->
    {ok, []};
read(14815, Bin0) ->
    <<Type:16, Bin1/binary>> = Bin0, 
    <<Subtype:16, _Bin2/binary>> = Bin1, 
    {ok, [Type, Subtype]};
read(14816, _) ->
    {ok, []};
read(14817, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (14800,[
    Res,
    Loc,
    Uid,
    Lev
] = Src_Data) ->
    Data = <<
        Res:32,
        Loc:8,
        Uid:64,
        Lev:16
    >>,
    {ok, pt:encode(14800, Data, Src_Data)};

write (14801,[
    Opty,
    Power,
    GalaxyUseList,
    AttrList,
    SkillList
] = Src_Data) ->
    BinList_GalaxyUseList = [
        item_to_bin_0(GalaxyUseList_Item) || GalaxyUseList_Item <- GalaxyUseList
    ], 

    GalaxyUseList_Len = length(GalaxyUseList), 
    Bin_GalaxyUseList = list_to_binary(BinList_GalaxyUseList),

    BinList_AttrList = [
        item_to_bin_1(AttrList_Item) || AttrList_Item <- AttrList
    ], 

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

    BinList_SkillList = [
        item_to_bin_2(SkillList_Item) || SkillList_Item <- SkillList
    ], 

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

    Data = <<
        Opty:8,
        Power:32,
        GalaxyUseList_Len:16, Bin_GalaxyUseList/binary,
        AttrList_Len:16, Bin_AttrList/binary,
        SkillList_Len:16, Bin_SkillList/binary
    >>,
    {ok, pt:encode(14801, Data, Src_Data)};

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

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

write (14804,[
    Res,
    Uid,
    Pos,
    SkillId,
    Lev,
    CostUid
] = Src_Data) ->
    Data = <<
        Res:32,
        Uid:64,
        Pos:8,
        SkillId:32,
        Lev:16,
        CostUid:64
    >>,
    {ok, pt:encode(14804, Data, Src_Data)};

write (14805,[
    Res,
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_3(List_Item) || List_Item <- List
    ], 

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

    Data = <<
        Res:32,
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(14805, Data, Src_Data)};

write (14806,[
    Res,
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_4(List_Item) || List_Item <- List
    ], 

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

    Data = <<
        Res:32,
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(14806, Data, Src_Data)};

write (14807,[
    IsShow,
    UsingSkin,
    SkinList
] = Src_Data) ->
    BinList_SkinList = [
        item_to_bin_5(SkinList_Item) || SkinList_Item <- SkinList
    ], 

    SkinList_Len = length(SkinList), 
    Bin_SkinList = list_to_binary(BinList_SkinList),

    Data = <<
        IsShow:8,
        UsingSkin:16,
        SkinList_Len:16, Bin_SkinList/binary
    >>,
    {ok, pt:encode(14807, Data, Src_Data)};

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

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

write (14810,[
    RefreshTimes,
    RestTimes,
    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 = <<
        RefreshTimes:16,
        RestTimes:16,
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(14810, Data, Src_Data)};

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

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

write (14813,[
    Collects
] = Src_Data) ->
    BinList_Collects = [
        item_to_bin_7(Collects_Item) || Collects_Item <- Collects
    ], 

    Collects_Len = length(Collects), 
    Bin_Collects = list_to_binary(BinList_Collects),

    Data = <<
        Collects_Len:16, Bin_Collects/binary
    >>,
    {ok, pt:encode(14813, Data, Src_Data)};

write (14814,[
    Type,
    Subtype,
    Items,
    State
] = Src_Data) ->
    BinList_Items = [
        item_to_bin_9(Items_Item) || Items_Item <- Items
    ], 

    Items_Len = length(Items), 
    Bin_Items = list_to_binary(BinList_Items),

    Data = <<
        Type:16,
        Subtype:16,
        Items_Len:16, Bin_Items/binary,
        State:8
    >>,
    {ok, pt:encode(14814, Data, Src_Data)};

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

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

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

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


item_to_bin_0 ({
    Id,
    IsOpen,
    Uid,
    TypeId
}) ->
    Data = <<
        Id:8,
        IsOpen:8,
        Uid:64,
        TypeId:32
    >>,
    Data.
item_to_bin_1 ({
    AttrId,
    Value
}) ->
    Data = <<
        AttrId:16,
        Value:32
    >>,
    Data.
item_to_bin_2 ({
    SkillId,
    Lev
}) ->
    Data = <<
        SkillId:32,
        Lev:16
    >>,
    Data.
item_to_bin_3 ({
    Type,
    GoodsId,
    GoodsNum
}) ->
    Data = <<
        Type:8,
        GoodsId:16,
        GoodsNum:8
    >>,
    Data.
item_to_bin_4 ({
    Type,
    GoodsId,
    GoodsNum
}) ->
    Data = <<
        Type:8,
        GoodsId:16,
        GoodsNum:8
    >>,
    Data.
item_to_bin_5 ({
    Skin,
    IsActive,
    Star
}) ->
    Data = <<
        Skin:16,
        IsActive:8,
        Star:16
    >>,
    Data.
item_to_bin_6 ({
    Pos,
    Id,
    Status
}) ->
    Data = <<
        Pos:8,
        Id:16,
        Status:8
    >>,
    Data.
item_to_bin_7 ({
    Type,
    Subtype,
    Items,
    State
}) ->
    BinList_Items = [
        item_to_bin_8(Items_Item) || Items_Item <- Items
    ], 

    Items_Len = length(Items), 
    Bin_Items = list_to_binary(BinList_Items),

    Data = <<
        Type:16,
        Subtype:16,
        Items_Len:16, Bin_Items/binary,
        State:8
    >>,
    Data.
item_to_bin_8 (
    Gtype
) ->
    Data = <<
        Gtype:32
    >>,
    Data.
item_to_bin_9 (
    Gtype
) ->
    Data = <<
        Gtype:32
    >>,
    Data.
