%% ---------------------------------------------------------------------------
%% @doc 跨服帮战
%% @author zhaoyu
%% @since  2018-09-03
%% ---------------------------------------------------------------------------
-module(pp_cross_guild_war).

-include("server.hrl").
-include("guild.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("predefine.hrl").
-include("cross_guild_war.hrl").

-export([handle/3]).


%% 活动时间
handle(60100, Status, _) ->
    #player_status{sid = Sid} = Status,
    cross_guild_war_local:send_act_info(Sid),
    {ok,Status};

%% 上次活动中城池占领信息
handle(60101, Status, _) ->
    cross_guild_war_api:send_city_info(Status),
    {ok,Status};

%% 帮战参赛信息-报名阶段和活动进行中
handle(60102, Status, _) ->
    #player_status{
        sid = Sid,
        id = RoleId,
        guild = GuildStatus,
        cross_guild_war = RoleCgw
    } = Status,
    #status_guild{id = GuildId} = GuildStatus,
    ServerId = config:get_server_id(),
    Msg = {send_zone_guilds_info, RoleId, ServerId, GuildId, Sid, RoleCgw#role_cgw.last_sign},
    MFA = {cross_guild_war_mgr, cast, [Msg]},
    clusters_node:apply_cast(MFA),
    {ok,Status};

%% 应援列表
handle(60103, Status, []) ->
    cross_guild_war_api:send_support_list(Status),
    {ok,Status};


%% 应援帮派
handle(60104, Status = #player_status{sid = Sid}, [ServerId, GuildId]) ->
    case cross_guild_war_api:support_guild(Status, ServerId, GuildId) of
        {false, Reason} ->
            {ok, BinData} = pt_601:write(60104, [Reason, GuildId]),
            lib_server_send:send_to_sid(Sid, BinData),
            NStatus = Status;
        {ok,NStatus} ->
            ok
    end,
    {ok,NStatus};

%% 报名榜
handle(60105, Status, [OpTy]) ->
    #player_status{sid = Sid} = Status,
    ServerId = config:get_server_id(),
    Msg = {send_room_sign_up_info, ServerId, OpTy, Sid},
    MFA = {cross_guild_war_mgr, cast, [Msg]},
    clusters_node:apply_cast(MFA),
    {ok,Status};

%% 报名
handle(60106, Status = #player_status{sid = Sid}, [OpTy]) ->
    case cross_guild_war_api:sign_up(OpTy,Status) of
        {false, Reason} ->
            {ok, BinData} = pt_601:write(60106, [Reason,0,0]),
            lib_server_send:send_to_sid(Sid, BinData);
        ok ->
            ok
    end;

%% 领取占领城池奖励
handle(60107, Status = #player_status{sid = Sid}, [CityId]) ->
    case cross_guild_war_api:get_city_award(Status, CityId) of
        {false, Reason} ->
            {ok, BinData} = pt_601:write(60107, [CityId, Reason]),
            lib_server_send:send_to_sid(Sid, BinData);
        ok ->
            ok
    end;

%% 进入场景
handle(60108, Status = #player_status{sid = Sid}, _) ->
    case cross_guild_war_api:enter_scene(Status) of
        {false, Reason} ->
            {ok, BinData} = pt_601:write(60108, [Reason]),
            lib_server_send:send_to_sid(Sid, BinData);
        ok ->
            ok
    end;

%% 退出场景
handle(60109, Status, _) ->
    #player_status{sid = Sid} = Status,
    case cross_guild_war_api:leave_scene(Status) of
        {false, Reason} ->
            {ok, BinData} = pt_601:write(60109, [Reason]),
            lib_server_send:send_to_sid(Sid, BinData);
        ok ->
            ok
    end;

%% 战场信息
handle(60110, Status, _) ->
    cross_guild_war_api:send_battlefield_info(Status),
    {ok,Status};

%% 号令
handle(60112, Status, [CityId]) ->
    cross_guild_war_api:send_guild_direction(Status, CityId),
    {ok,Status};

handle(60115,Status,_)->
    #player_status{sid = Sid} = Status,
    ServerId = config:get_server_id(),
    Msg = {get_cgw_log, ServerId, Sid},
    MFA = {cross_guild_war_mgr, cast, [Msg]},
    clusters_node:apply_cast(MFA),
    {ok,Status};

%% 霸主特权-禁言
handle(60120, Status = #player_status{sid = Sid}, [OtherId]) ->
    case cross_guild_war_api:prohibit_talk(Status, OtherId) of
        {ok,NewStatus} -> Result = ?SUCCESS;
        {false, Result} -> NewStatus = Status
    end,
    {ok, BinData} = pt_601:write(60120, [Result]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok,NewStatus};

%% 霸主特权-国家改名
handle(60121, Status = #player_status{sid = Sid}, [FactId, FactionName]) ->
    case cross_guild_war_api:faction_rename(Status, FactId, FactionName) of
        {false, Result} ->
            {ok, BinData} = pt_601:write(60121, [Result, FactId, FactionName]),
            lib_server_send:send_to_sid(Sid, BinData);
        _ -> skip
    end,
    {ok,Status};

%% 霸主限购-物品信息
handle(60122, Status, _) ->
    cross_guild_war_api:get_goods_info(Status),
    {ok,Status};

%% 霸主限购-购买
handle(60123, Status = #player_status{sid = Sid}, _) ->
    case cross_guild_war_api:overlord_buy(Status) of
        {ok, NewStatus} -> ErrCode = ?SUCCESS;
        {false, ErrCode} -> NewStatus = Status
    end,
    {ok, BinData} = pt_601:write(60123, [ErrCode]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, NewStatus};


handle(_Cmd, _Status, _Data) ->
    {error, "pp_cross_guild_war no match"}.


