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

read(13200, _) ->
    {ok, []};
read(13201, Bin0) ->
    <<SetType:8, Bin1/binary>> = Bin0,
    <<SetValue:8, _Bin2/binary>> = Bin1,
    {ok, [SetType, SetValue]};
read(13205, Bin0) ->
    <<SceneId:32, Bin1/binary>> = Bin0,
    FunArray0 = fun(<<RestBin0/binary>>) ->
        <<PlaceId:8, _Args1/binary>> = RestBin0,
        <<PosX:16, _Args2/binary>> = _Args1,
        <<PosY:16, _Args3/binary>> = _Args2,
        {{PlaceId, PosX, PosY},_Args3}
        end,
    {PosList, _Bin2} = pt:read_array(FunArray0, Bin1),

    {ok, [SceneId, PosList]};
read(13208, Bin0) ->
    <<SceneId:32, _Bin1/binary>> = Bin0,
    {ok, [SceneId]};
read(_Cmd, _R) -> {error, no_match}.

write (13200,[
    OnhookTime,
    OnhookSetting
]) ->
    BinList_OnhookSetting = [
        item_to_bin_0(OnhookSetting_Item) || OnhookSetting_Item <- OnhookSetting
    ],

    OnhookSetting_Len = length(OnhookSetting),
    Bin_OnhookSetting = list_to_binary(BinList_OnhookSetting),

    Data = <<
        OnhookTime:32,
        OnhookSetting_Len:16, Bin_OnhookSetting/binary
    >>,
    {ok, pt:pack(13200, Data)};

write (13201,[
    ErrorCode
]) ->
    Data = <<
        ErrorCode:8
    >>,
    {ok, pt:pack(13201, Data)};

write (13202,[
    OnhookTime,
    CostOnhookTime,
    Lv,
    NewLv,
    Exp,
    ExpRatio,
    DevourExp,
    DevourNum,
    Coin,
    SellNum,
    RewardGoods
]) ->
    Bin_RewardGoods = pt:write_object_list(RewardGoods),

    Data = <<
        OnhookTime:32,
        CostOnhookTime:32,
        Lv:16,
        NewLv:16,
        Exp:64,
        ExpRatio:8,
        DevourExp:32,
        DevourNum:32,
        Coin:32,
        SellNum:32,
        Bin_RewardGoods/binary
    >>,
    {ok, pt:pack(13202, Data)};

write (13205,[
    OnhookList
]) ->
    BinList_OnhookList = [
        item_to_bin_1(OnhookList_Item) || OnhookList_Item <- OnhookList
    ],

    OnhookList_Len = length(OnhookList),
    Bin_OnhookList = list_to_binary(BinList_OnhookList),

    Data = <<
        OnhookList_Len:16, Bin_OnhookList/binary
    >>,
    {ok, pt:pack(13205, Data)};

write (13206,[
    RoleId,
    Figure,
    HpLim,
    PlaceId,
    PosX,
    PosY,
    MoveSpeed,
    CombatPower
]) ->
    Bin_Figure = pt:write_figure(Figure),

    Data = <<
        RoleId:64,
        Bin_Figure/binary,
        HpLim:32,
        PlaceId:8,
        PosX:16,
        PosY:16,
        MoveSpeed:16,
        CombatPower:32
    >>,
    {ok, pt:pack(13206, Data)};

write (13207,[
    RoleId
]) ->
    Data = <<
        RoleId:64
    >>,
    {ok, pt:pack(13207, Data)};

write (13208,[
    PosList
]) ->
    BinList_PosList = [
        item_to_bin_2(PosList_Item) || PosList_Item <- PosList
    ],

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

    Data = <<
        PosList_Len:16, Bin_PosList/binary
    >>,
    {ok, pt:pack(13208, Data)};

write (13209,[
    PlaceId,
    PosX,
    PosY
]) ->
    Data = <<
        PlaceId:8,
        PosX:16,
        PosY:16
    >>,
    {ok, pt:pack(13209, Data)};

write (13210,[
    PlaceId,
    PosX,
    PosY
]) ->
    Data = <<
        PlaceId:8,
        PosX:16,
        PosY:16
    >>,
    {ok, pt:pack(13210, Data)};

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


item_to_bin_0 ({
    SetType,
    SetValue
}) ->
    Data = <<
        SetType:8,
        SetValue:8
    >>,
    Data.
item_to_bin_1 ({
    RoleId,
    Figure,
    HpLim,
    PlaceId,
    PosX,
    PosY,
    MoveSpeed,
    CombatPower
}) ->
    Bin_Figure = pt:write_figure(Figure),

    Data = <<
        RoleId:64,
        Bin_Figure/binary,
        HpLim:32,
        PlaceId:8,
        PosX:16,
        PosY:16,
        MoveSpeed:16,
        CombatPower:32
    >>,
    Data.
item_to_bin_2 ({
    PlaceId,
    PosX,
    PosY
}) ->
    Data = <<
        PlaceId:8,
        PosX:16,
        PosY:16
    >>,
    Data.
