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

read(17600, _) ->
    {ok, []};
read(17601, Bin0) ->
    <<LoginDays:8, Bin1/binary>> = Bin0, 
    <<ModId:16, Bin2/binary>> = Bin1, 
    <<SubId:16, _Bin3/binary>> = Bin2, 
    {ok, [LoginDays, ModId, SubId]};
read(17602, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<RewardId:16, _Bin2/binary>> = Bin1, 
    {ok, [Type, RewardId]};
read(17603, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (17600,[
    LoginDays,
    Score,
    LeftTime,
    RealTime,
    TaskState,
    ScoreState,
    SkillState
] = Src_Data) ->
    BinList_TaskState = [
        item_to_bin_0(TaskState_Item) || TaskState_Item <- TaskState
    ], 

    TaskState_Len = length(TaskState), 
    Bin_TaskState = list_to_binary(BinList_TaskState),

    BinList_ScoreState = [
        item_to_bin_1(ScoreState_Item) || ScoreState_Item <- ScoreState
    ], 

    ScoreState_Len = length(ScoreState), 
    Bin_ScoreState = list_to_binary(BinList_ScoreState),

    BinList_SkillState = [
        item_to_bin_2(SkillState_Item) || SkillState_Item <- SkillState
    ], 

    SkillState_Len = length(SkillState), 
    Bin_SkillState = list_to_binary(BinList_SkillState),

    Data = <<
        LoginDays:8,
        Score:16,
        LeftTime:32,
        RealTime:32,
        TaskState_Len:16, Bin_TaskState/binary,
        ScoreState_Len:16, Bin_ScoreState/binary,
        SkillState_Len:16, Bin_SkillState/binary
    >>,
    {ok, pt:encode(17600, Data, Src_Data)};

write (17601,[
    Errcode,
    LoginDays,
    ModId,
    SubId
] = Src_Data) ->
    Data = <<
        Errcode:32,
        LoginDays:8,
        ModId:16,
        SubId:16
    >>,
    {ok, pt:encode(17601, Data, Src_Data)};

write (17602,[
    Errcode,
    Type,
    RewardId
] = Src_Data) ->
    Data = <<
        Errcode:32,
        Type:8,
        RewardId:16
    >>,
    {ok, pt:encode(17602, Data, Src_Data)};

write (17603,[
    Score,
    Days,
    ModId,
    SubId,
    Progress
] = Src_Data) ->
    Data = <<
        Score:16,
        Days:8,
        ModId:16,
        SubId:16,
        Progress:16
    >>,
    {ok, pt:encode(17603, Data, Src_Data)};

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


item_to_bin_0 ({
    Days,
    ModId,
    SubId,
    Progress,
    IsReceive
}) ->
    Data = <<
        Days:8,
        ModId:16,
        SubId:16,
        Progress:16,
        IsReceive:8
    >>,
    Data.
item_to_bin_1 (
    ScoreId
) ->
    Data = <<
        ScoreId:16
    >>,
    Data.
item_to_bin_2 (
    Days
) ->
    Data = <<
        Days:8
    >>,
    Data.
