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

read(41801, _) ->
    {ok, []};
read(41802, _) ->
    {ok, []};
read(41803, _) ->
    {ok, []};
read(41804, _) ->
    {ok, []};
read(41805, Bin0) ->
    <<RankLength:8, _Bin1/binary>> = Bin0, 
    {ok, [RankLength]};
read(41806, _) ->
    {ok, []};
read(41807, _) ->
    {ok, []};
read(41808, Bin0) ->
    <<VoteId:64, _Bin1/binary>> = Bin0, 
    {ok, [VoteId]};
read(41809, _) ->
    {ok, []};
read(41810, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(41811, Bin0) ->
    <<Num:8, _Bin1/binary>> = Bin0, 
    {ok, [Num]};
read(_Cmd, _R) -> {error, no_match}.

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

write (41801,[
    Status,
    StartTime,
    Duration
] = Src_Data) ->
    Data = <<
        Status:8,
        StartTime:64,
        Duration:32
    >>,
    {ok, pt:encode(41801, Data, Src_Data)};

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

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

write (41804,[
    Rank,
    AccExp,
    VoteOtherTime,
    VoteTimes,
    HasBuyTimes
] = Src_Data) ->
    Data = <<
        Rank:32,
        AccExp:32,
        VoteOtherTime:16,
        VoteTimes:16,
        HasBuyTimes:16
    >>,
    {ok, pt:encode(41804, Data, Src_Data)};

write (41805,[
    SelfRank,
    SelfBeVoted,
    RankList,
    LeftRank
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_0(RankList_Item) || RankList_Item <- RankList
    ], 

    RankList_Len = length(RankList), 
    Bin_RankList = list_to_binary(BinList_RankList),

    BinList_LeftRank = [
        item_to_bin_1(LeftRank_Item) || LeftRank_Item <- LeftRank
    ], 

    LeftRank_Len = length(LeftRank), 
    Bin_LeftRank = list_to_binary(BinList_LeftRank),

    Data = <<
        SelfRank:32,
        SelfBeVoted:32,
        RankList_Len:16, Bin_RankList/binary,
        LeftRank_Len:16, Bin_LeftRank/binary
    >>,
    {ok, pt:encode(41805, Data, Src_Data)};

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

write (41807,[
    EndTime,
    Rank,
    AccExp,
    FirstName,
    FirstServerId
] = Src_Data) ->
    Bin_FirstName = pt:write_string(FirstName), 

    Data = <<
        EndTime:32,
        Rank:32,
        AccExp:32,
        Bin_FirstName/binary,
        FirstServerId:16
    >>,
    {ok, pt:encode(41807, Data, Src_Data)};

write (41808,[
    Rcode,
    VoteOtherTime,
    VoteTimes
] = Src_Data) ->
    Data = <<
        Rcode:32,
        VoteOtherTime:16,
        VoteTimes:16
    >>,
    {ok, pt:encode(41808, Data, Src_Data)};

write (41809,[
    RoleList
] = Src_Data) ->
    BinList_RoleList = [
        item_to_bin_2(RoleList_Item) || RoleList_Item <- RoleList
    ], 

    RoleList_Len = length(RoleList), 
    Bin_RoleList = list_to_binary(BinList_RoleList),

    Data = <<
        RoleList_Len:16, Bin_RoleList/binary
    >>,
    {ok, pt:encode(41809, Data, Src_Data)};

write (41810,[
    Type,
    RoleList
] = Src_Data) ->
    BinList_RoleList = [
        item_to_bin_3(RoleList_Item) || RoleList_Item <- RoleList
    ], 

    RoleList_Len = length(RoleList), 
    Bin_RoleList = list_to_binary(BinList_RoleList),

    Data = <<
        Type:8,
        RoleList_Len:16, Bin_RoleList/binary
    >>,
    {ok, pt:encode(41810, Data, Src_Data)};

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

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

write (41813,[
    VotPicture,
    BeVotedName,
    ServerIdVoted,
    ServerIdBeVoted
] = Src_Data) ->
    Bin_VotPicture = pt:write_picture(VotPicture), 

    Bin_BeVotedName = pt:write_string(BeVotedName), 

    Data = <<
        Bin_VotPicture/binary,
        Bin_BeVotedName/binary,
        ServerIdVoted:16,
        ServerIdBeVoted:16
    >>,
    {ok, pt:encode(41813, Data, Src_Data)};

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


item_to_bin_0 ({
    Rank,
    BeVoted,
    Figure,
    ServerId,
    HaveVote
}) ->
    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        Rank:32,
        BeVoted:32,
        Bin_Figure/binary,
        ServerId:16,
        HaveVote:8
    >>,
    Data.
item_to_bin_1 ({
    Rank,
    BeVoted,
    Picture,
    ServerId,
    HaveVote
}) ->
    Bin_Picture = pt:write_picture(Picture), 

    Data = <<
        Rank:32,
        BeVoted:32,
        Bin_Picture/binary,
        ServerId:16,
        HaveVote:8
    >>,
    Data.
item_to_bin_2 ({
    Picture,
    Count,
    ServerId,
    HaveVote
}) ->
    Bin_Picture = pt:write_picture(Picture), 

    Data = <<
        Bin_Picture/binary,
        Count:8,
        ServerId:32,
        HaveVote:8
    >>,
    Data.
item_to_bin_3 ({
    RoleId,
    RoleName,
    VoetTimes
}) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Data = <<
        RoleId:64,
        Bin_RoleName/binary,
        VoetTimes:16
    >>,
    Data.
