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

read(41300, _) ->
    {ok, []};
read(41302, Bin0) ->
    <<PosId:8, Bin1/binary>> = Bin0, 
    <<FashionId:32, Bin2/binary>> = Bin1, 
    <<ColorId:8, _Bin3/binary>> = Bin2, 
    {ok, [PosId, FashionId, ColorId]};
read(41303, Bin0) ->
    <<PosId:8, Bin1/binary>> = Bin0, 
    <<FashionId:32, _Bin2/binary>> = Bin1, 
    {ok, [PosId, FashionId]};
read(41306, Bin0) ->
    <<PosId:8, Bin1/binary>> = Bin0, 
    <<FashionId:32, Bin2/binary>> = Bin1, 
    <<ColorId:16, _Bin3/binary>> = Bin2, 
    {ok, [PosId, FashionId, ColorId]};
read(41310, Bin0) ->
    <<PosId:8, Bin1/binary>> = Bin0, 
    <<FashionId:32, _Bin2/binary>> = Bin1, 
    {ok, [PosId, FashionId]};
read(41312, Bin0) ->
    <<PosId:8, Bin1/binary>> = Bin0, 
    <<FashionId:32, Bin2/binary>> = Bin1, 
    <<ColorId:16, _Bin3/binary>> = Bin2, 
    {ok, [PosId, FashionId, ColorId]};
read(41313, _) ->
    {ok, []};
read(41314, Bin0) ->
    <<SuitId:16, _Bin1/binary>> = Bin0, 
    {ok, [SuitId]};
read(_Cmd, _R) -> {error, no_match}.

write (41300,[
    Code,
    PosList
] = Src_Data) ->
    BinList_PosList = [
        item_to_bin_0(PosList_Item) || PosList_Item <- PosList
    ], 

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

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

write (41302,[
    Code,
    PosId,
    FashionId,
    ColorId
] = Src_Data) ->
    Data = <<
        Code:32,
        PosId:8,
        FashionId:32,
        ColorId:8
    >>,
    {ok, pt:encode(41302, Data, Src_Data)};

write (41303,[
    Code,
    PosId,
    FashionId
] = Src_Data) ->
    Data = <<
        Code:32,
        PosId:8,
        FashionId:32
    >>,
    {ok, pt:encode(41303, Data, Src_Data)};

write (41306,[
    Code,
    PosId,
    FashionId,
    NewStar,
    Combat
] = Src_Data) ->
    Data = <<
        Code:32,
        PosId:8,
        FashionId:32,
        NewStar:16,
        Combat:32
    >>,
    {ok, pt:encode(41306, Data, Src_Data)};

write (41310,[
    PosId,
    FashionId,
    WearFashionId,
    NowColorId,
    Power,
    ColorList
] = Src_Data) ->
    BinList_ColorList = [
        item_to_bin_3(ColorList_Item) || ColorList_Item <- ColorList
    ], 

    ColorList_Len = length(ColorList), 
    Bin_ColorList = list_to_binary(BinList_ColorList),

    Data = <<
        PosId:8,
        FashionId:32,
        WearFashionId:32,
        NowColorId:8,
        Power:32,
        ColorList_Len:16, Bin_ColorList/binary
    >>,
    {ok, pt:encode(41310, Data, Src_Data)};

write (41311,[
    RoleId,
    FashionEquip,
    Pos,
    FashionId,
    ColorId
] = Src_Data) ->
    BinList_FashionEquip = [
        item_to_bin_4(FashionEquip_Item) || FashionEquip_Item <- FashionEquip
    ], 

    FashionEquip_Len = length(FashionEquip), 
    Bin_FashionEquip = list_to_binary(BinList_FashionEquip),

    Data = <<
        RoleId:64,
        FashionEquip_Len:16, Bin_FashionEquip/binary,
        Pos:8,
        FashionId:32,
        ColorId:32
    >>,
    {ok, pt:encode(41311, Data, Src_Data)};



write (41313,[
    SuitList
] = Src_Data) ->
    BinList_SuitList = [
        item_to_bin_5(SuitList_Item) || SuitList_Item <- SuitList
    ], 

    SuitList_Len = length(SuitList), 
    Bin_SuitList = list_to_binary(BinList_SuitList),

    Data = <<
        SuitList_Len:16, Bin_SuitList/binary
    >>,
    {ok, pt:encode(41313, Data, Src_Data)};

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

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


item_to_bin_0 ({
    PosId,
    WearFashionId,
    NowColorId,
    FashionList
}) ->
    BinList_FashionList = [
        item_to_bin_1(FashionList_Item) || FashionList_Item <- FashionList
    ], 

    FashionList_Len = length(FashionList), 
    Bin_FashionList = list_to_binary(BinList_FashionList),

    Data = <<
        PosId:8,
        WearFashionId:32,
        NowColorId:8,
        FashionList_Len:16, Bin_FashionList/binary
    >>,
    Data.
item_to_bin_1 ({
    FashionId,
    ColorList,
    Combat
}) ->
    BinList_ColorList = [
        item_to_bin_2(ColorList_Item) || ColorList_Item <- ColorList
    ], 

    ColorList_Len = length(ColorList), 
    Bin_ColorList = list_to_binary(BinList_ColorList),

    Data = <<
        FashionId:32,
        ColorList_Len:16, Bin_ColorList/binary,
        Combat:32
    >>,
    Data.
item_to_bin_2 ({
    ColorId,
    Star,
    ExpiryTime
}) ->
    Data = <<
        ColorId:8,
        Star:16,
        ExpiryTime:32
    >>,
    Data.
item_to_bin_3 ({
    ColorId,
    Star,
    ExpiryTime
}) ->
    Data = <<
        ColorId:8,
        Star:16,
        ExpiryTime:32
    >>,
    Data.
item_to_bin_4 ({
    PartPos,
    FashionModelId,
    FashionChartletId
}) ->
    Data = <<
        PartPos:8,
        FashionModelId:32,
        FashionChartletId:32
    >>,
    Data.
item_to_bin_5 ({
    SuitId,
    FashionList,
    Active
}) ->
    BinList_FashionList = [
        item_to_bin_6(FashionList_Item) || FashionList_Item <- FashionList
    ], 

    FashionList_Len = length(FashionList), 
    Bin_FashionList = list_to_binary(BinList_FashionList),

    Data = <<
        SuitId:16,
        FashionList_Len:16, Bin_FashionList/binary,
        Active:8
    >>,
    Data.
item_to_bin_6 (
    FashionId
) ->
    Data = <<
        FashionId:32
    >>,
    Data.
