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

read(21001, Bin0) ->
    <<SkillId:32, _Bin1/binary>> = Bin0, 
    {ok, [SkillId]};
read(21002, _) ->
    {ok, []};
read(21003, Bin0) ->
    <<SkillId:32, _Bin1/binary>> = Bin0, 
    {ok, [SkillId]};
read(21010, _) ->
    {ok, []};
read(21011, Bin0) ->
    <<SkillId:32, _Bin1/binary>> = Bin0, 
    {ok, [SkillId]};
read(21012, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

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

write (21002,[
    SkillList
] = Src_Data) ->
    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:encode(21002, Data, Src_Data)};

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

write (21010,[
    LessPoint,
    TalentSkills
] = Src_Data) ->
    BinList_TalentSkills = [
        item_to_bin_1(TalentSkills_Item) || TalentSkills_Item <- TalentSkills
    ], 

    TalentSkills_Len = length(TalentSkills), 
    Bin_TalentSkills = list_to_binary(BinList_TalentSkills),

    Data = <<
        LessPoint:16,
        TalentSkills_Len:16, Bin_TalentSkills/binary
    >>,
    {ok, pt:encode(21010, Data, Src_Data)};

write (21011,[
    Errcode,
    SkillId,
    SkillLv,
    LessPoint
] = Src_Data) ->
    Data = <<
        Errcode:32,
        SkillId:32,
        SkillLv:16,
        LessPoint:16
    >>,
    {ok, pt:encode(21011, Data, Src_Data)};

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

write (21015,[
    SkillId,
    NewSkillId,
    EndTime
] = Src_Data) ->
    Data = <<
        SkillId:32,
        NewSkillId:32,
        EndTime:64
    >>,
    {ok, pt:encode(21015, Data, Src_Data)};

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


item_to_bin_0 ({
    SkillId,
    SkillLv
}) ->
    Data = <<
        SkillId:32,
        SkillLv:16
    >>,
    Data.
item_to_bin_1 ({
    SkillType,
    Point,
    Skills
}) ->
    BinList_Skills = [
        item_to_bin_2(Skills_Item) || Skills_Item <- Skills
    ], 

    Skills_Len = length(Skills), 
    Bin_Skills = list_to_binary(BinList_Skills),

    Data = <<
        SkillType:8,
        Point:16,
        Skills_Len:16, Bin_Skills/binary
    >>,
    Data.
item_to_bin_2 ({
    SkillId,
    SkillLv
}) ->
    Data = <<
        SkillId:32,
        SkillLv:16
    >>,
    Data.
