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

read(10300, _) ->
    {ok, []};
read(10301, _) ->
    {ok, []};
read(10302, _) ->
    {ok, []};
read(10303, _) ->
    {ok, []};
read(10304, _) ->
    {ok, []};
read(10305, _) ->
    {ok, []};
read(10310, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (10300,[
    Stage,
    FactionList
] = Src_Data) ->
    BinList_FactionList = [
        item_to_bin_0(FactionList_Item) || FactionList_Item <- FactionList
    ], 

    FactionList_Len = length(FactionList), 
    Bin_FactionList = list_to_binary(BinList_FactionList),

    Data = <<
        Stage:8,
        FactionList_Len:16, Bin_FactionList/binary
    >>,
    {ok, pt:encode(10300, Data, Src_Data)};

write (10301,[
    ScheduledTime,
    NextStage,
    FactionList
] = Src_Data) ->
    BinList_FactionList = [
        item_to_bin_2(FactionList_Item) || FactionList_Item <- FactionList
    ], 

    FactionList_Len = length(FactionList), 
    Bin_FactionList = list_to_binary(BinList_FactionList),

    Data = <<
        ScheduledTime:32,
        NextStage:8,
        FactionList_Len:16, Bin_FactionList/binary
    >>,
    {ok, pt:encode(10301, Data, Src_Data)};

write (10302,[
    NextStage,
    WLevelAvgZone,
    WLevelReqt,
    WLevelAvg,
    FactionList
] = Src_Data) ->
    BinList_FactionList = [
        item_to_bin_4(FactionList_Item) || FactionList_Item <- FactionList
    ], 

    FactionList_Len = length(FactionList), 
    Bin_FactionList = list_to_binary(BinList_FactionList),

    Data = <<
        NextStage:8,
        WLevelAvgZone:16,
        WLevelReqt:16,
        WLevelAvg:16,
        FactionList_Len:16, Bin_FactionList/binary
    >>,
    {ok, pt:encode(10302, Data, Src_Data)};

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

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

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

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

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


item_to_bin_0 ({
    Faction,
    FactionName,
    SvrList
}) ->
    Bin_FactionName = pt:write_string(FactionName), 

    BinList_SvrList = [
        item_to_bin_1(SvrList_Item) || SvrList_Item <- SvrList
    ], 

    SvrList_Len = length(SvrList), 
    Bin_SvrList = list_to_binary(BinList_SvrList),

    Data = <<
        Faction:8,
        Bin_FactionName/binary,
        SvrList_Len:16, Bin_SvrList/binary
    >>,
    Data.
item_to_bin_1 ({
    ServerId,
    ServerNum,
    SvrName
}) ->
    Bin_SvrName = pt:write_string(SvrName), 

    Data = <<
        ServerId:16,
        ServerNum:16,
        Bin_SvrName/binary
    >>,
    Data.
item_to_bin_2 ({
    Faction,
    FactionName,
    SvrList
}) ->
    Bin_FactionName = pt:write_string(FactionName), 

    BinList_SvrList = [
        item_to_bin_3(SvrList_Item) || SvrList_Item <- SvrList
    ], 

    SvrList_Len = length(SvrList), 
    Bin_SvrList = list_to_binary(BinList_SvrList),

    Data = <<
        Faction:8,
        Bin_FactionName/binary,
        SvrList_Len:16, Bin_SvrList/binary
    >>,
    Data.
item_to_bin_3 ({
    ServerId,
    ServerNum,
    SvrName
}) ->
    Bin_SvrName = pt:write_string(SvrName), 

    Data = <<
        ServerId:16,
        ServerNum:16,
        Bin_SvrName/binary
    >>,
    Data.
item_to_bin_4 ({
    Faction,
    FactionName,
    SvrList
}) ->
    Bin_FactionName = pt:write_string(FactionName), 

    BinList_SvrList = [
        item_to_bin_5(SvrList_Item) || SvrList_Item <- SvrList
    ], 

    SvrList_Len = length(SvrList), 
    Bin_SvrList = list_to_binary(BinList_SvrList),

    Data = <<
        Faction:8,
        Bin_FactionName/binary,
        SvrList_Len:16, Bin_SvrList/binary
    >>,
    Data.
item_to_bin_5 ({
    ServerId,
    ServerNum,
    SvrName,
    WLevel
}) ->
    Bin_SvrName = pt:write_string(SvrName), 

    Data = <<
        ServerId:16,
        ServerNum:16,
        Bin_SvrName/binary,
        WLevel:16
    >>,
    Data.
