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

read(43501, _) ->
    {ok, []};
read(43502, Bin0) ->
    <<SkillType:8, _Bin1/binary>> = Bin0,
    {ok, [SkillType]};
read(43503, Bin0) ->
    <<SkillType:8, Bin1/binary>> = Bin0,
    <<Type:8, _Bin2/binary>> = Bin1,
    {ok, [SkillType, Type]};
read(_Cmd, _R) -> {error, no_match}.

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

write (43501,[
    SkillList
]) ->
    BinList_SkillList = [
        item_to_bin_0(SkillList_Item) || SkillList_Item <- SkillList
    ],

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

    Data = <<
        SkillList_Len:16, Bin_SkillList/binary
    >>,
    {ok, pt:pack(43501, Data)};

write (43502,[
    SkillType,
    SkillLv,
    SkillExp,
    BuildLv,
    BookList
]) ->
    BinList_BookList = [
        item_to_bin_1(BookList_Item) || BookList_Item <- BookList
    ],

    BookList_Len = length(BookList),
    Bin_BookList = list_to_binary(BinList_BookList),

    Data = <<
        SkillType:8,
        SkillLv:16,
        SkillExp:32,
        BuildLv:16,
        BookList_Len:16, Bin_BookList/binary
    >>,
    {ok, pt:pack(43502, Data)};

write (43503,[
    Errcode,
    SkillType,
    SkillLv,
    SkillExp,
    Type,
    Reward
]) ->
    BinList_Reward = [
        item_to_bin_2(Reward_Item) || Reward_Item <- Reward
    ],

    Reward_Len = length(Reward),
    Bin_Reward = list_to_binary(BinList_Reward),

    Data = <<
        Errcode:32,
        SkillType:8,
        SkillLv:16,
        SkillExp:32,
        Type:8,
        Reward_Len:16, Bin_Reward/binary
    >>,
    {ok, pt:pack(43503, Data)};

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


item_to_bin_0 ({
    SkillType,
    SkillLv,
    SkillExp
}) ->
    Data = <<
        SkillType:8,
        SkillLv:16,
        SkillExp:32
    >>,
    Data.
item_to_bin_1 (
    TypeId
) ->
    Data = <<
        TypeId:16
    >>,
    Data.
item_to_bin_2 ({
    TypeId,
    GoodTypeId,
    Num
}) ->
    Data = <<
        TypeId:8,
        GoodTypeId:32,
        Num:32
    >>,
    Data.
