%% ----------------------------------------------
%% desc     :跨服团战协议映射模块
%% module   :pp_camp_war.erl
%% author   :hh
%% time     :20/07/23
%% ----------------------------------------------

-module(pp_camp_war).

-export([handle/3]).

-include("server.hrl").
-include("camp_war.hrl").
-include("figure.hrl").
-include("def_module.hrl").
-include("def_fun.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("guild.hrl").
-include("rec_dress_up.hrl").


handle(Cmd, Player, Data) ->
    #player_status{figure = #figure{lv = RoleLv}} = Player,
    CfgLv = lib_module:get_open_lv(?MOD_CAMP_WAR),
    if
        RoleLv < CfgLv ->
            ok;
        true ->
            do_handle(Cmd, Player, Data)
    end.

do_handle(41000, Player, _) ->
    #player_status{sid = Sid} = Player,
    Res = camp_act_check(Player),
    if
        Res =/= true ->
            {false, _Errcode} = Res,
            lib_server_send:send_to_sid(Sid, pt_410, 41000, [0, 0, 0, 0, 0]);
        true ->
            ServId = config:get_server_id(),
            Msg = {query_camp_war_act_info, ServId, Sid},
            camp_war_mgr:cast_to_mgr(Msg)
    end,
    ok;

do_handle(41001, Player, [RankType, PageSize, PageNum]) ->
    #player_status{sid = Sid} = Player,
    Res = camp_act_check(Player),
    if 
        Res =/= true ->
            {false, Errcode} = Res,
            lib_server_send:send_to_sid(Sid, pt_410, 41001, [Errcode, RankType, 0, 0, 0, []]);
        true ->
            #player_status{guild = #status_guild{id = GuildId}, sid = Sid} = Player,
            ServId = config:get_server_id(),
            Msg = {get_camp_war_arena_info, ServId, GuildId, Sid, [RankType, PageSize, PageNum]},
            camp_war_mgr:cast_to_mgr(Msg)
    end,
    ok;

%% 进入和退出场景时人物锁和场景判断
%% 退出场景要将玩家所在分线信息发往跨服中心
do_handle(41002, Player, [Type]) ->
    #player_status{id = RoleId, guild = Guild, sid = Sid, scene = SceneId, scene_pool_id = ScenePool, 
        figure = Figure, action_lock = ActionLock} = Player,
    #figure{
        lv = RoleLv, vip = Vip, sup_vip = SupVip, sex = Sex, 
        name = Name, guild_name = GuildName, dress_list = DressList,
        profile_photo_id = PhotoId
    } = Figure,
    Res = camp_act_check(Player),
    if
        Res =/= true ->
            {false, Errcode} = Res,
            lib_server_send:send_to_sid(Sid, pt_410, 41002, [Errcode]);
        Type =/= 0 andalso Type =/= 1 ->
            lib_server_send:send_to_sid(Sid, pt_410, 41002, [?FAIL]);
        true ->
            ServId    = config:get_server_id(),
            ServerNum = config:get_server_num(),
            if Type == 0 ->
                    IsHosting = ?IF(offline_hosting_api:is_offline_hosting(Player), 1, 0),
                    if 
                        IsHosting == 1 ->
                            CheckList = [alive, action_free, is_transferable, dun_multi_match];
                        true ->
                            CheckList = [alive, action_free, safe_scene, is_transferable, dun_multi_match]
                    end,
                    CheckResult = lib_player_check:check_list(Player, CheckList),                 
                    case CheckResult of
                        true ->
                            #status_guild{id = GuildId, flag = GuildFlag} = Guild,
                            case lists:keyfind(?DRESS_UP_FRAME, 1, DressList) of
                                {_, DressId} -> ok;
                                _   -> DressId = 0
                            end,
                            Args = [ServId, ServerNum, GuildId, GuildFlag, RoleId, RoleLv, Vip, SupVip, Sex, PhotoId, DressId, Name, GuildName, IsHosting],
                            camp_war_api:enter_scene(Args);
                        {false, Errcode} ->
                            lib_server_send:send_to_sid(Sid, pt_410, 41002, [Errcode])
                    end;
                true ->
                    % LocRet = camp_war_mgr:is_loc_camp_war(),
                    {LocScene, ClsScene} = {data_camp_war:get_kv(scene), data_camp_war:get_kv(cls_scene)},
                    Errcode      =  ?ERRCODE(err410_on_camp_war),
                    % ?ERR("~p ~p ~p", [SceneId, TarScene, ActionLock]),
                    if 
                        (SceneId == LocScene orelse SceneId == ClsScene) andalso ActionLock == Errcode ->
                            camp_war_api:exit_scene([ServId, ServerNum, Guild#status_guild.id, RoleId, ScenePool]);
                        true ->
                            lib_server_send:send_to_sid(Sid, pt_410, 41002, [?ERRCODE(err410_not_on_camp_war)])
                    end
            end
    end,
    ok;

do_handle(41003, Player, _) ->
    Res = camp_act_check(Player),
    if
        Res =/= true ->
            #player_status{sid = Sid} = Player,
            {false, Errcode} = Res,
            lib_server_send:send_to_sid(Sid, pt_410, 41003, [Errcode, []]);
        true ->
            #player_status{guild = #status_guild{id = GuildId}, sid = Sid} = Player,
            ServId = config:get_server_id(),
            Msg = {get_guild_res_info, ServId, GuildId, Sid},
            camp_war_mgr:cast_to_mgr(Msg)
    end,
    ok;

do_handle(41004, Player, _) ->
    Res = camp_act_check(Player),
    if
        Res =/= true ->
            #player_status{sid = Sid} = Player,
            {false, Errcode} = Res,
            lib_server_send:send_to_sid(Sid, pt_410, 41004, [Errcode, 0, []]);
        true ->
            #player_status{sid = Sid, scene_pool_id = ScenePool, guild = #status_guild{id = GuildId}} = Player,
            ServId = config:get_server_id(),
            Msg = {get_arena_pool_res_info, ServId, ScenePool, GuildId, Sid},
            camp_war_mgr:cast_to_mgr(Msg)
    end,
    ok;

do_handle(41005, Player, [RankType, PageSize, PageNum]) ->
    Res = camp_act_check(Player),
    if
        Res =/= true ->
            #player_status{sid = Sid} = Player,
            % {false, _Errcode} = Res,
            lib_server_send:send_to_sid(Sid, pt_410, 41005, [RankType, PageSize, PageNum, 0, []]);
        true ->
            #player_status{sid = Sid, guild = #status_guild{id = GuildId}} = Player,
            ServId = config:get_server_id(),
            Msg = {get_hury_rank, ServId, GuildId, Sid, RankType, PageSize, PageNum},
            camp_war_mgr:cast_to_mgr(Msg)
    end,
    ok;

do_handle(41006, _Player, _) ->
    ok;

do_handle(41008, Player, [GoodsResId]) ->
    #player_status{} = Player,
    Res = camp_act_check(Player),
    if
        Res =/= true ->
            #player_status{sid = Sid} = Player,
            {false, Errcode} = Res,
            lib_server_send:send_to_sid(Sid, pt_410, 41008, [Errcode, GoodsResId, []]);
        true ->
            #player_status{sid = Sid, scene_pool_id = ScenePool, guild = #status_guild{id = GuildId}} = Player,
            ServId = config:get_server_id(),
            Msg = {get_scene_res_collect_coordinate, ServId, GuildId, GoodsResId, ScenePool, Sid},
            camp_war_mgr:cast_to_mgr(Msg)
    end,
    ok;

do_handle(_Cmd, _Player, Data) ->
    ?ERR("camp war protocol error ~p ~p", [_Cmd, Data]),
    ok.

camp_act_check(Player) ->
    #player_status{guild = Guild} = Player,
    OpenDay = util:get_open_day(),
    CfgOpenDay = data_camp_war:get_kv(open_day),
    if
        OpenDay < CfgOpenDay ->
            {false, ?ERRCODE(err410_open_day_limit)};
        not is_record(Guild, status_guild) ->
            {false, ?ERRCODE(err400_no_guild)};
        Guild#status_guild.id == 0 ->
            {false, ?ERRCODE(err400_no_guild)};
        true ->
            true
    end.