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

read(18100, _) ->
    {ok, []};
read(18102, Bin0) ->
    <<SealSoulId:64, Bin1/binary>> = Bin0,
    <<Pos:16, _Bin2/binary>> = Bin1,
    {ok, [SealSoulId, Pos]};
read(18104, Bin0) ->
    <<SealSoulId:64, _Bin1/binary>> = Bin0,
    {ok, [SealSoulId]};
read(18106, _) ->
    {ok, []};
read(18107, Bin0) ->
    <<Times:16, Bin1/binary>> = Bin0,
    <<BuyType:8, _Bin2/binary>> = Bin1,
    {ok, [Times, BuyType]};
read(18109, Bin0) ->
    <<AwardId:32, _Bin1/binary>> = Bin0,
    {ok, [AwardId]};
read(18110, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (18100,[
    Errcode
]) ->
    Data = <<
        Errcode:32
    >>,
    {ok, pt:pack(18100, Data)};

write (18102,[
    Recode,
    SealSoulId,
    GoodsTypeId,
    OldGoodsId,
    Pos
]) ->
    Data = <<
        Recode:32,
        SealSoulId:64,
        GoodsTypeId:32,
        OldGoodsId:64,
        Pos:16
    >>,
    {ok, pt:pack(18102, Data)};

write (18104,[
    Recode,
    SealSoulId,
    Stren,
    CurrExp
]) ->
    Data = <<
        Recode:32,
        SealSoulId:64,
        Stren:32,
        CurrExp:32
    >>,
    {ok, pt:pack(18104, Data)};

write (18106,[
    FreeTimes,
    PeriodTimes,
    Progress,
    AwardState
]) ->
    BinList_AwardState = [
        item_to_bin_0(AwardState_Item) || AwardState_Item <- AwardState
    ],

    AwardState_Len = length(AwardState),
    Bin_AwardState = list_to_binary(BinList_AwardState),

    Data = <<
        FreeTimes:32,
        PeriodTimes:32,
        Progress:32,
        AwardState_Len:16, Bin_AwardState/binary
    >>,
    {ok, pt:pack(18106, Data)};

write (18107,[
    Recode,
    Times,
    SealSoulChip,
    RewardList
]) ->
    BinList_RewardList = [
        item_to_bin_1(RewardList_Item) || RewardList_Item <- RewardList
    ],

    RewardList_Len = length(RewardList),
    Bin_RewardList = list_to_binary(BinList_RewardList),

    Data = <<
        Recode:32,
        Times:16,
        SealSoulChip:32,
        RewardList_Len:16, Bin_RewardList/binary
    >>,
    {ok, pt:pack(18107, Data)};

write (18109,[
    Recode,
    AwardId,
    State
]) ->
    Data = <<
        Recode:32,
        AwardId:32,
        State:8
    >>,
    {ok, pt:pack(18109, Data)};

write (18110,[
    MultipleSealSoul
]) ->
    BinList_MultipleSealSoul = [
        item_to_bin_2(MultipleSealSoul_Item) || MultipleSealSoul_Item <- MultipleSealSoul
    ],

    MultipleSealSoul_Len = length(MultipleSealSoul),
    Bin_MultipleSealSoul = list_to_binary(BinList_MultipleSealSoul),

    Data = <<
        MultipleSealSoul_Len:16, Bin_MultipleSealSoul/binary
    >>,
    {ok, pt:pack(18110, Data)};

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


item_to_bin_0 ({
    AwardId,
    State
}) ->
    Data = <<
        AwardId:32,
        State:8
    >>,
    Data.
item_to_bin_1 ({
    GoodsId,
    GoodsTypeId,
    GoodsNum
}) ->
    Data = <<
        GoodsId:64,
        GoodsTypeId:32,
        GoodsNum:32
    >>,
    Data.
item_to_bin_2 ({
    SealSoulId,
    DecomposeSealSoul,
    Exp
}) ->
    BinList_DecomposeSealSoul = [
        item_to_bin_3(DecomposeSealSoul_Item) || DecomposeSealSoul_Item <- DecomposeSealSoul
    ],

    DecomposeSealSoul_Len = length(DecomposeSealSoul),
    Bin_DecomposeSealSoul = list_to_binary(BinList_DecomposeSealSoul),

    Data = <<
        SealSoulId:64,
        DecomposeSealSoul_Len:16, Bin_DecomposeSealSoul/binary,
        Exp:32
    >>,
    Data.
item_to_bin_3 ({
    SealSoulTypeId,
    Stren
}) ->
    Data = <<
        SealSoulTypeId:32,
        Stren:32
    >>,
    Data.
