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

read(60400, _) ->
    {ok, []};
read(60401, _) ->
    {ok, []};
read(60402, Bin0) ->
    <<PointId:16, _Bin1/binary>> = Bin0, 
    {ok, [PointId]};
read(60403, Bin0) ->
    <<PointId:16, _Bin1/binary>> = Bin0, 
    {ok, [PointId]};
read(60404, Bin0) ->
    <<PointId:16, Bin1/binary>> = Bin0, 
    <<Sweep:8, _Bin2/binary>> = Bin1, 
    {ok, [PointId, Sweep]};
read(60405, Bin0) ->
    <<PointId:16, Bin1/binary>> = Bin0, 
    <<Type:8, _Bin2/binary>> = Bin1, 
    {ok, [PointId, Type]};
read(60406, Bin0) ->
    <<Option:8, _Bin1/binary>> = Bin0, 
    {ok, [Option]};
read(60407, Bin0) ->
    <<PointId:16, _Bin1/binary>> = Bin0, 
    {ok, [PointId]};
read(60408, _) ->
    {ok, []};
read(60409, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

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

write (60401,[
    PointList,
    BuffInfo
] = Src_Data) ->
    BinList_PointList = [
        item_to_bin_0(PointList_Item) || PointList_Item <- PointList
    ], 

    PointList_Len = length(PointList), 
    Bin_PointList = list_to_binary(BinList_PointList),

    BinList_BuffInfo = [
        item_to_bin_2(BuffInfo_Item) || BuffInfo_Item <- BuffInfo
    ], 

    BuffInfo_Len = length(BuffInfo), 
    Bin_BuffInfo = list_to_binary(BinList_BuffInfo),

    Data = <<
        PointList_Len:16, Bin_PointList/binary,
        BuffInfo_Len:16, Bin_BuffInfo/binary
    >>,
    {ok, pt:encode(60401, Data, Src_Data)};

write (60402,[
    PointId,
    Faction,
    AttackInfo,
    AwardTime,
    IsMass,
    IsSweep
] = Src_Data) ->
    BinList_AttackInfo = [
        item_to_bin_3(AttackInfo_Item) || AttackInfo_Item <- AttackInfo
    ], 

    AttackInfo_Len = length(AttackInfo), 
    Bin_AttackInfo = list_to_binary(BinList_AttackInfo),

    Data = <<
        PointId:16,
        Faction:8,
        AttackInfo_Len:16, Bin_AttackInfo/binary,
        AwardTime:32,
        IsMass:8,
        IsSweep:8
    >>,
    {ok, pt:encode(60402, Data, Src_Data)};

write (60403,[
    PointId,
    AttackRank,
    MyScore,
    MyRank
] = Src_Data) ->
    BinList_AttackRank = [
        item_to_bin_4(AttackRank_Item) || AttackRank_Item <- AttackRank
    ], 

    AttackRank_Len = length(AttackRank), 
    Bin_AttackRank = list_to_binary(BinList_AttackRank),

    Data = <<
        PointId:16,
        AttackRank_Len:16, Bin_AttackRank/binary,
        MyScore:32,
        MyRank:8
    >>,
    {ok, pt:encode(60403, Data, Src_Data)};

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

write (60405,[
    Res,
    PointId,
    Type
] = Src_Data) ->
    Data = <<
        Res:32,
        PointId:16,
        Type:8
    >>,
    {ok, pt:encode(60405, Data, Src_Data)};



write (60407,[
    Res,
    PointId,
    AwardTime
] = Src_Data) ->
    Data = <<
        Res:32,
        PointId:16,
        AwardTime:32
    >>,
    {ok, pt:encode(60407, Data, Src_Data)};

write (60408,[
    SkillList
] = Src_Data) ->
    BinList_SkillList = [
        item_to_bin_5(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(60408, Data, Src_Data)};

write (60409,[
    PointId,
    OriginHpNumerator,
    OriginHpDenominator
] = Src_Data) ->
    Data = <<
        PointId:16,
        OriginHpNumerator:32,
        OriginHpDenominator:32
    >>,
    {ok, pt:encode(60409, Data, Src_Data)};

write (60410,[
    PointId,
    FactionScore,
    FactionAddScore,
    SelfAddScore,
    Value,
    Seize
] = Src_Data) ->
    Data = <<
        PointId:32,
        FactionScore:32,
        FactionAddScore:32,
        SelfAddScore:32,
        Value:32,
        Seize:8
    >>,
    {ok, pt:encode(60410, Data, Src_Data)};

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


item_to_bin_0 ({
    PointId,
    Faction,
    AttackInfo,
    AwardTime,
    IsMass,
    IsSweep
}) ->
    BinList_AttackInfo = [
        item_to_bin_1(AttackInfo_Item) || AttackInfo_Item <- AttackInfo
    ], 

    AttackInfo_Len = length(AttackInfo), 
    Bin_AttackInfo = list_to_binary(BinList_AttackInfo),

    Data = <<
        PointId:16,
        Faction:8,
        AttackInfo_Len:16, Bin_AttackInfo/binary,
        AwardTime:32,
        IsMass:8,
        IsSweep:8
    >>,
    Data.
item_to_bin_1 ({
    Faction,
    Score
}) ->
    Data = <<
        Faction:8,
        Score:32
    >>,
    Data.
item_to_bin_2 ({
    PointId,
    Faction
}) ->
    Data = <<
        PointId:16,
        Faction:8
    >>,
    Data.
item_to_bin_3 ({
    Faction,
    Score
}) ->
    Data = <<
        Faction:8,
        Score:32
    >>,
    Data.
item_to_bin_4 ({
    ServerNum,
    Faction,
    Name,
    Score,
    Hurt
}) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        ServerNum:32,
        Faction:8,
        Bin_Name/binary,
        Score:32,
        Hurt:16
    >>,
    Data.
item_to_bin_5 (
    SkillId
) ->
    Data = <<
        SkillId:32
    >>,
    Data.
