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

read(40901, _) ->
    {ok, []};
read(40902, Bin0) ->
    <<Type:16, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(40903, Bin0) ->
    <<Type:16, Bin1/binary>> = Bin0, 
    <<SubType:16, Bin2/binary>> = Bin1, 
    <<Id:16, _Bin3/binary>> = Bin2, 
    {ok, [Type, SubType, Id]};
read(40906, _) ->
    {ok, []};
read(40907, _) ->
    {ok, []};
read(40908, Bin0) ->
    <<Lv:16, _Bin1/binary>> = Bin0, 
    {ok, [Lv]};
read(40909, _) ->
    {ok, []};
read(40910, _) ->
    {ok, []};
read(40911, Bin0) ->
    <<Count:8, _Bin1/binary>> = Bin0, 
    {ok, [Count]};
read(40912, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(_Cmd, _R) -> {error, no_match}.

write (40901,[
    TypeList
] = Src_Data) ->
    BinList_TypeList = [
        item_to_bin_0(TypeList_Item) || TypeList_Item <- TypeList
    ], 

    TypeList_Len = length(TypeList), 
    Bin_TypeList = list_to_binary(BinList_TypeList),

    Data = <<
        TypeList_Len:16, Bin_TypeList/binary
    >>,
    {ok, pt:encode(40901, Data, Src_Data)};

write (40902,[
    Type,
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_1(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        Type:16,
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(40902, Data, Src_Data)};

write (40903,[
    Errcode,
    Type,
    SubType,
    Id
] = Src_Data) ->
    Data = <<
        Errcode:32,
        Type:16,
        SubType:16,
        Id:16
    >>,
    {ok, pt:encode(40903, Data, Src_Data)};

write (40904,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_2(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(40904, Data, Src_Data)};

write (40905,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_3(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(40905, Data, Src_Data)};

write (40906,[
    Lv,
    Exp
] = Src_Data) ->
    Data = <<
        Lv:16,
        Exp:32
    >>,
    {ok, pt:encode(40906, Data, Src_Data)};

write (40907,[
    DetialList
] = Src_Data) ->
    BinList_DetialList = [
        item_to_bin_4(DetialList_Item) || DetialList_Item <- DetialList
    ], 

    DetialList_Len = length(DetialList), 
    Bin_DetialList = list_to_binary(BinList_DetialList),

    Data = <<
        DetialList_Len:16, Bin_DetialList/binary
    >>,
    {ok, pt:encode(40907, Data, Src_Data)};

write (40908,[
    Lv,
    Status
] = Src_Data) ->
    Data = <<
        Lv:16,
        Status:8
    >>,
    {ok, pt:encode(40908, Data, Src_Data)};

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

write (40910,[
    NowCount,
    MaxCount
] = Src_Data) ->
    Data = <<
        NowCount:8,
        MaxCount:8
    >>,
    {ok, pt:encode(40910, Data, Src_Data)};

write (40911,[
    NowCount,
    MaxCount
] = Src_Data) ->
    Data = <<
        NowCount:8,
        MaxCount:8
    >>,
    {ok, pt:encode(40911, Data, Src_Data)};

write (40912,[
    Status
] = Src_Data) ->
    Data = <<
        Status:8
    >>,
    {ok, pt:encode(40912, Data, Src_Data)};

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


item_to_bin_0 ({
    Type,
    Count
}) ->
    Data = <<
        Type:16,
        Count:8
    >>,
    Data.
item_to_bin_1 ({
    SubType,
    Id,
    Progress,
    Status
}) ->
    Data = <<
        SubType:16,
        Id:16,
        Progress:32,
        Status:8
    >>,
    Data.
item_to_bin_2 ({
    Type,
    SubType,
    Id,
    Progress,
    Status
}) ->
    Data = <<
        Type:16,
        SubType:16,
        Id:16,
        Progress:32,
        Status:8
    >>,
    Data.
item_to_bin_3 ({
    Type,
    Count
}) ->
    Data = <<
        Type:16,
        Count:8
    >>,
    Data.
item_to_bin_4 ({
    Lv,
    Status
}) ->
    Data = <<
        Lv:16,
        Status:8
    >>,
    Data.
