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

read(41000, _) ->
    {ok, []};
read(41001, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<PageSize:8, Bin2/binary>> = Bin1, 
    <<PageNum:8, _Bin3/binary>> = Bin2, 
    {ok, [Type, PageSize, PageNum]};
read(41002, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(41003, _) ->
    {ok, []};
read(41004, _) ->
    {ok, []};
read(41005, Bin0) ->
    <<RankType:8, Bin1/binary>> = Bin0, 
    <<PageSize:8, Bin2/binary>> = Bin1, 
    <<PageNum:8, _Bin3/binary>> = Bin2, 
    {ok, [RankType, PageSize, PageNum]};
read(41006, _) ->
    {ok, []};
read(41007, _) ->
    {ok, []};
read(41008, Bin0) ->
    <<AttrId:32, _Bin1/binary>> = Bin0, 
    {ok, [AttrId]};
read(41009, _) ->
    {ok, []};
read(41010, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (41000,[
    Status,
    StartTime,
    NextTime,
    EndTime,
    AcSub
] = Src_Data) ->
    Data = <<
        Status:8,
        StartTime:32,
        NextTime:32,
        EndTime:32,
        AcSub:8
    >>,
    {ok, pt:encode(41000, Data, Src_Data)};

write (41001,[
    Errcode,
    Type,
    PageSize,
    PageNum,
    TotalPage,
    ArenaList
] = Src_Data) ->
    BinList_ArenaList = [
        item_to_bin_0(ArenaList_Item) || ArenaList_Item <- ArenaList
    ], 

    ArenaList_Len = length(ArenaList), 
    Bin_ArenaList = list_to_binary(BinList_ArenaList),

    Data = <<
        Errcode:32,
        Type:8,
        PageSize:16,
        PageNum:8,
        TotalPage:8,
        ArenaList_Len:16, Bin_ArenaList/binary
    >>,
    {ok, pt:encode(41001, Data, Src_Data)};

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

write (41003,[
    Errcode,
    BuffGoods
] = Src_Data) ->
    BinList_BuffGoods = [
        item_to_bin_2(BuffGoods_Item) || BuffGoods_Item <- BuffGoods
    ], 

    BuffGoods_Len = length(BuffGoods), 
    Bin_BuffGoods = list_to_binary(BinList_BuffGoods),

    Data = <<
        Errcode:32,
        BuffGoods_Len:16, Bin_BuffGoods/binary
    >>,
    {ok, pt:encode(41003, Data, Src_Data)};

write (41004,[
    Errcode,
    NextTime,
    Resources
] = Src_Data) ->
    BinList_Resources = [
        item_to_bin_3(Resources_Item) || Resources_Item <- Resources
    ], 

    Resources_Len = length(Resources), 
    Bin_Resources = list_to_binary(BinList_Resources),

    Data = <<
        Errcode:32,
        NextTime:32,
        Resources_Len:16, Bin_Resources/binary
    >>,
    {ok, pt:encode(41004, Data, Src_Data)};

write (41005,[
    RankType,
    PageSize,
    PageNum,
    TotalPage,
    HurtRank
] = Src_Data) ->
    BinList_HurtRank = [
        item_to_bin_4(HurtRank_Item) || HurtRank_Item <- HurtRank
    ], 

    HurtRank_Len = length(HurtRank), 
    Bin_HurtRank = list_to_binary(BinList_HurtRank),

    Data = <<
        RankType:8,
        PageSize:8,
        PageNum:8,
        TotalPage:8,
        HurtRank_Len:16, Bin_HurtRank/binary
    >>,
    {ok, pt:encode(41005, Data, Src_Data)};

write (41006,[
    MyGuildRank,
    HurtRank
] = Src_Data) ->
    BinList_HurtRank = [
        item_to_bin_5(HurtRank_Item) || HurtRank_Item <- HurtRank
    ], 

    HurtRank_Len = length(HurtRank), 
    Bin_HurtRank = list_to_binary(BinList_HurtRank),

    Data = <<
        MyGuildRank:16,
        HurtRank_Len:16, Bin_HurtRank/binary
    >>,
    {ok, pt:encode(41006, Data, Src_Data)};

write (41007,[
    Errcode,
    GuildRank,
    FirstArena,
    RoleRank,
    GuildName
] = Src_Data) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Data = <<
        Errcode:32,
        GuildRank:16,
        FirstArena:8,
        RoleRank:16,
        Bin_GuildName/binary
    >>,
    {ok, pt:encode(41007, Data, Src_Data)};

write (41008,[
    Errcode,
    AttrId,
    Coordinates
] = Src_Data) ->
    BinList_Coordinates = [
        item_to_bin_6(Coordinates_Item) || Coordinates_Item <- Coordinates
    ], 

    Coordinates_Len = length(Coordinates), 
    Bin_Coordinates = list_to_binary(BinList_Coordinates),

    Data = <<
        Errcode:32,
        AttrId:32,
        Coordinates_Len:16, Bin_Coordinates/binary
    >>,
    {ok, pt:encode(41008, Data, Src_Data)};

write (41009,[
    Role,
    AttrId,
    Num
] = Src_Data) ->
    Bin_Role = pt:write_picture(Role), 

    Data = <<
        Bin_Role/binary,
        AttrId:32,
        Num:8
    >>,
    {ok, pt:encode(41009, Data, Src_Data)};

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

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


item_to_bin_0 ({
    ArenaId,
    Guilds
}) ->
    BinList_Guilds = [
        item_to_bin_1(Guilds_Item) || Guilds_Item <- Guilds
    ], 

    Guilds_Len = length(Guilds), 
    Bin_Guilds = list_to_binary(BinList_Guilds),

    Data = <<
        ArenaId:32,
        Guilds_Len:16, Bin_Guilds/binary
    >>,
    Data.
item_to_bin_1 ({
    GuildId,
    GuildName,
    GuildFlag,
    ServerNum
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Data = <<
        GuildId:64,
        Bin_GuildName/binary,
        GuildFlag:32,
        ServerNum:32
    >>,
    Data.
item_to_bin_2 ({
    AttrId,
    Num
}) ->
    Data = <<
        AttrId:32,
        Num:16
    >>,
    Data.
item_to_bin_3 ({
    AttrId,
    Num
}) ->
    Data = <<
        AttrId:32,
        Num:16
    >>,
    Data.
item_to_bin_4 ({
    Rank,
    Id,
    FirstArena,
    Name,
    ServerNum,
    GuildFlag,
    Val,
    Lv,
    Vip,
    SvipType,
    Gender,
    PhotoProfileId,
    DressId
}) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        Rank:16,
        Id:64,
        FirstArena:8,
        Bin_Name/binary,
        ServerNum:32,
        GuildFlag:32,
        Val:64,
        Lv:16,
        Vip:8,
        SvipType:8,
        Gender:8,
        PhotoProfileId:32,
        DressId:32
    >>,
    Data.
item_to_bin_5 ({
    Rank,
    GuildId,
    GuildName,
    ServerNum,
    Val
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Data = <<
        Rank:16,
        GuildId:64,
        Bin_GuildName/binary,
        ServerNum:32,
        Val:64
    >>,
    Data.
item_to_bin_6 ({
    X,
    Y
}) ->
    Data = <<
        X:32,
        Y:32
    >>,
    Data.
