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

read(41600, _) ->
    {ok, []};
read(41601, _) ->
    {ok, []};
read(41602, _) ->
    {ok, []};
read(41603, _) ->
    {ok, []};
read(41605, _) ->
    {ok, []};
read(41606, _) ->
    {ok, []};
read(41607, Bin0) ->
    <<RoleId:64, _Bin1/binary>> = Bin0, 
    {ok, [RoleId]};
read(41608, _) ->
    {ok, []};
read(41609, _) ->
    {ok, []};
read(41610, Bin0) ->
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<Type:8, _Args01/binary>> = RestBin0, 
        <<Value1:8, _Args02/binary>> = _Args01, 
        <<Value2:32, _Args03/binary>> = _Args02, 
        <<Value3:8, _Args04/binary>> = _Args03, 
        {{Type, Value1, Value2, Value3},_Args04}
        end,
    {Cdata, _Bin1} = pt:read_array(FunArray0, Bin0),

    {ok, [Cdata]};
read(41611, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (41600,[
    Round,
    RoundBegin,
    RoundEnd,
    Stage,
    StageBegin,
    StageEnd
] = Src_Data) ->
    Data = <<
        Round:16,
        RoundBegin:32,
        RoundEnd:32,
        Stage:8,
        StageBegin:32,
        StageEnd:32
    >>,
    {ok, pt:encode(41600, Data, Src_Data)};

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

write (41602,[
    Cdata
] = Src_Data) ->
    BinList_Cdata = [
        item_to_bin_0(Cdata_Item) || Cdata_Item <- Cdata
    ], 

    Cdata_Len = length(Cdata), 
    Bin_Cdata = list_to_binary(BinList_Cdata),

    Data = <<
        Cdata_Len:16, Bin_Cdata/binary
    >>,
    {ok, pt:encode(41602, Data, Src_Data)};

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

write (41605,[
    Rank
] = Src_Data) ->
    BinList_Rank = [
        item_to_bin_1(Rank_Item) || Rank_Item <- Rank
    ], 

    Rank_Len = length(Rank), 
    Bin_Rank = list_to_binary(BinList_Rank),

    Data = <<
        Rank_Len:16, Bin_Rank/binary
    >>,
    {ok, pt:encode(41605, Data, Src_Data)};

write (41606,[
    Candidates
] = Src_Data) ->
    BinList_Candidates = [
        item_to_bin_3(Candidates_Item) || Candidates_Item <- Candidates
    ], 

    Candidates_Len = length(Candidates), 
    Bin_Candidates = list_to_binary(BinList_Candidates),

    Data = <<
        Candidates_Len:16, Bin_Candidates/binary
    >>,
    {ok, pt:encode(41606, Data, Src_Data)};

write (41607,[
    Res,
    Name,
    IsMore,
    RoleVote
] = Src_Data) ->
    Bin_Name = pt:write_string(Name), 

    BinList_RoleVote = [
        item_to_bin_5(RoleVote_Item) || RoleVote_Item <- RoleVote
    ], 

    RoleVote_Len = length(RoleVote), 
    Bin_RoleVote = list_to_binary(BinList_RoleVote),

    Data = <<
        Res:32,
        Bin_Name/binary,
        IsMore:8,
        RoleVote_Len:16, Bin_RoleVote/binary
    >>,
    {ok, pt:encode(41607, Data, Src_Data)};

write (41608,[
    RecordList
] = Src_Data) ->
    BinList_RecordList = [
        item_to_bin_6(RecordList_Item) || RecordList_Item <- RecordList
    ], 

    RecordList_Len = length(RecordList), 
    Bin_RecordList = list_to_binary(BinList_RecordList),

    Data = <<
        RecordList_Len:16, Bin_RecordList/binary
    >>,
    {ok, pt:encode(41608, Data, Src_Data)};

write (41609,[
    Rank
] = Src_Data) ->
    BinList_Rank = [
        item_to_bin_7(Rank_Item) || Rank_Item <- Rank
    ], 

    Rank_Len = length(Rank), 
    Bin_Rank = list_to_binary(BinList_Rank),

    Data = <<
        Rank_Len:16, Bin_Rank/binary
    >>,
    {ok, pt:encode(41609, Data, Src_Data)};

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

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

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


item_to_bin_0 ({
    Type,
    Value1,
    Value2,
    Value3
}) ->
    Data = <<
        Type:8,
        Value1:8,
        Value2:32,
        Value3:8
    >>,
    Data.
item_to_bin_1 ({
    ServerNum,
    RoleInfo,
    Cdata,
    Stage,
    OriginRank
}) ->
    Bin_RoleInfo = pt:write_picture(RoleInfo), 

    BinList_Cdata = [
        item_to_bin_2(Cdata_Item) || Cdata_Item <- Cdata
    ], 

    Cdata_Len = length(Cdata), 
    Bin_Cdata = list_to_binary(BinList_Cdata),

    Data = <<
        ServerNum:16,
        Bin_RoleInfo/binary,
        Cdata_Len:16, Bin_Cdata/binary,
        Stage:8,
        OriginRank:8
    >>,
    Data.
item_to_bin_2 ({
    Type,
    Value1,
    Value2,
    Value3
}) ->
    Data = <<
        Type:8,
        Value1:8,
        Value2:32,
        Value3:8
    >>,
    Data.
item_to_bin_3 ({
    RoleId,
    Name,
    ServerNum,
    Gender,
    Career,
    Turn,
    Vip,
    SvipType,
    Cdata
}) ->
    Bin_Name = pt:write_string(Name), 

    BinList_Cdata = [
        item_to_bin_4(Cdata_Item) || Cdata_Item <- Cdata
    ], 

    Cdata_Len = length(Cdata), 
    Bin_Cdata = list_to_binary(BinList_Cdata),

    Data = <<
        RoleId:64,
        Bin_Name/binary,
        ServerNum:16,
        Gender:8,
        Career:8,
        Turn:8,
        Vip:8,
        SvipType:8,
        Cdata_Len:16, Bin_Cdata/binary
    >>,
    Data.
item_to_bin_4 ({
    Type,
    Value1,
    Value2,
    Value3
}) ->
    Data = <<
        Type:8,
        Value1:8,
        Value2:32,
        Value3:8
    >>,
    Data.
item_to_bin_5 ({
    Id,
    Vote
}) ->
    Data = <<
        Id:64,
        Vote:32
    >>,
    Data.
item_to_bin_6 ({
    Name,
    Time
}) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        Bin_Name/binary,
        Time:32
    >>,
    Data.
item_to_bin_7 ({
    ServerNum,
    RoleInfo,
    Cdata,
    Stage,
    OriginRank,
    Rank
}) ->
    Bin_RoleInfo = pt:write_picture(RoleInfo), 

    BinList_Cdata = [
        item_to_bin_8(Cdata_Item) || Cdata_Item <- Cdata
    ], 

    Cdata_Len = length(Cdata), 
    Bin_Cdata = list_to_binary(BinList_Cdata),

    Data = <<
        ServerNum:32,
        Bin_RoleInfo/binary,
        Cdata_Len:16, Bin_Cdata/binary,
        Stage:8,
        OriginRank:8,
        Rank:8
    >>,
    Data.
item_to_bin_8 ({
    Type,
    Value1,
    Value2,
    Value3
}) ->
    Data = <<
        Type:8,
        Value1:8,
        Value2:32,
        Value3:8
    >>,
    Data.
