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

read(14700, _) ->
    {ok, []};
read(14701, Bin0) ->
    <<Id:16, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(14702, Bin0) ->
    <<Id:16, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(14703, Bin0) ->
    <<Id:16, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(14705, _) ->
    {ok, []};
read(14706, Bin0) ->
    <<SkillId:32, _Bin1/binary>> = Bin0, 
    {ok, [SkillId]};
read(_Cmd, _R) -> {error, no_match}.

write (14700,[
    Days,
    Visus
] = Src_Data) ->
    BinList_Visus = [
        item_to_bin_0(Visus_Item) || Visus_Item <- Visus
    ], 

    Visus_Len = length(Visus), 
    Bin_Visus = list_to_binary(BinList_Visus),

    Data = <<
        Days:16,
        Visus_Len:16, Bin_Visus/binary
    >>,
    {ok, pt:encode(14700, Data, Src_Data)};

write (14701,[
    Result,
    Id,
    Grade,
    Lv
] = Src_Data) ->
    Data = <<
        Result:32,
        Id:16,
        Grade:16,
        Lv:16
    >>,
    {ok, pt:encode(14701, Data, Src_Data)};

write (14702,[
    Result,
    Opty,
    Id
] = Src_Data) ->
    Data = <<
        Result:32,
        Opty:8,
        Id:16
    >>,
    {ok, pt:encode(14702, Data, Src_Data)};

write (14703,[
    Result,
    Id
] = Src_Data) ->
    Data = <<
        Result:32,
        Id:16
    >>,
    {ok, pt:encode(14703, Data, Src_Data)};

write (14704,[
    Id
] = Src_Data) ->
    Data = <<
        Id:16
    >>,
    {ok, pt:encode(14704, Data, Src_Data)};

write (14705,[
    UseSkillId,
    SkillList
] = Src_Data) ->
    BinList_SkillList = [
        item_to_bin_1(SkillList_Item) || SkillList_Item <- SkillList
    ], 

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

    Data = <<
        UseSkillId:32,
        SkillList_Len:16, Bin_SkillList/binary
    >>,
    {ok, pt:encode(14705, Data, Src_Data)};

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

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

write (14708,[
    Days
] = Src_Data) ->
    Data = <<
        Days:16
    >>,
    {ok, pt:encode(14708, Data, Src_Data)};

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


item_to_bin_0 ({
    Id,
    Grade,
    Lv,
    Combat,
    State,
    Display
}) ->
    Data = <<
        Id:16,
        Grade:16,
        Lv:16,
        Combat:64,
        State:8,
        Display:8
    >>,
    Data.
item_to_bin_1 ({
    SkillId,
    Lv
}) ->
    Data = <<
        SkillId:32,
        Lv:16
    >>,
    Data.
