%% ---------------------------------------------------------------------------
%% @doc lib_3v3_pai.erl

%% @author  hjh
%% @email  hejiahua@163.com
%% @since  2017-11-22
%% @deprecated 3v3接口
%% ---------------------------------------------------------------------------

-module(lib_3v3_api).

-compile(export_all).

-export([
        collect_mon/5
        , handle_event/2
        , collect_pre_check/2
        , is_3v3_scene/1
        , check_revive/2
        , check_collect/2
        , send_to_3v3_team/2
        , get_tier/1
    ]).

-include("3v3.hrl").
-include("rec_event.hrl").
-include("def_event.hrl").
-include("scene.hrl").
-include("server.hrl").
-include("battle.hrl").
-include("attr.hrl").
-include("errcode.hrl").

%% 采集完毕
collect_mon(SceneId, ScenePoolId, CopyId, CollectorId, Mid) ->
    Is3v3Scene = is_3v3_scene(SceneId),
    case Is3v3Scene of
        true ->
            case lib_3v3_center:get_unite_role_data(CollectorId) of
                {ok, #kf_3v3_role_data{pk_pid = PKPid}} when is_pid(PKPid) ->
                    PKPid ! {collect_complete, [SceneId, ScenePoolId, CopyId, CollectorId, Mid]};
                _ -> skip
            end;
        false ->
            skip
    end.

%% 死亡事件
handle_event(Player, #event_callback{type_id = ?EVENT_PLAYER_DIE, data = Data}) when is_record(Player, player_status) ->
    #player_status{id = DefRoleId, scene = SceneId, copy_id = RoomId} = Player,
    #{attersign := AtterSign, atter := Atter} = Data,
    #battle_return_atter{id = AtterId} = Atter,
    case is_3v3_scene(SceneId) of
        true ->
            % 助攻列表
%%            HitKeyList = lists:keydelete(AtterId, 1, HitList),
            clusters_node:apply_cast(mod_3v3_center, kill_enemy, [[SceneId, RoomId, DefRoleId, AtterSign, AtterId]]),
            ReviveTime = utime:unixtime()+?KF_3V3_REVIVE_TIME,
            ReviveStatus = #revive_status{is_revive = ?SUCCESS, revive_time = ReviveTime},
            % {ok, BinData} = pt_200:write(20009, [?SUCCESS, ReviveTime, 0]),
            % lib_server_send:send_to_sid(Player#player_status.sid, BinData),
            {ok, Player#player_status{revive_status = ReviveStatus} };
        false ->
            {ok, Player}
    end;

handle_event(Player, #event_callback{type_id = ?EVENT_LOGIN_CAST}) when is_record(Player, player_status) ->
    lib_3v3_local:repair_tier(Player),
    {ok, Player};

handle_event(Player, _EventCallback) ->
    {ok, Player}.

% %% 玩家死亡
% player_die(
%     #player_status{id = DefRoleId, scene = SceneID, copy_id = RoomID},
%     #battle_return_atter{
%         id = AttRoleId, sign = AttSign}, HitList) ->
%     Is3v3Scene = lib_3v3_api:is_3v3_scene(SceneID),
%     if
%         Is3v3Scene andalso AttSign == ?BATTLE_SIGN_PLAYER ->
%             % io:format("-------kill_enemy  000------~n"),
%             % TODO by hjh : 是否需要增加平台和平台数字
%             % by hjh 注释 KillerKey = [AttID, AttPlatform, AttServerNum],
%             AttPlatform = 0, AttServerNum = 0,
%             KillerKey = [AttRoleId, AttPlatform, AttServerNum],
%             HitKeyList = lists:keydelete(KillerKey, 1, HitList), %% 助攻列表
%             clusters_node:apply_cast(mod_3v3_center, kill_enemy, [[SceneID, RoomID, DefRoleId, AttRoleId, HitKeyList]]);
%         true -> skip
%     end.

%% 采集检测
collect_pre_check(_Player = #player_status{scene = SceneId}, _CheckArgs) ->
    is_3v3_scene(SceneId).

%% 是否是3v3场景
is_3v3_scene(SceneID) ->
    lists:member(SceneID, ?PK_3V3_SCENE).

%% 是否能复活
check_revive(Player, Type) ->
    #player_status{scene = SceneId, revive_status = ReviveStatus} = Player,
    Is3v3Scene = is_3v3_scene(SceneId),
    if
        Type =:= ?REVIVE_KF_3V3 ->
            if
                Is3v3Scene ->
                    NowTime = utime:unixtime(),
                    if
                        ReviveStatus#revive_status.revive_time =< NowTime + 1 ->
                            true;
                        true ->
                            {false, ?ERRCODE(err200_revive_time_cd)}
                    end ;
                true ->
                    {false, ?ERRCODE(err200_revive_time_cd)}
            end;
        Is3v3Scene ->
            {false, ?ERRCODE(err200_revive_time_cd)};
        true ->
            true
    end.

%% 能否采集
check_collect(User, Object) ->
    #ets_scene_user{scene = SceneId, battle_attr = #battle_attr{group = UserGroup}} = User,
    #scene_object{battle_attr = #battle_attr{group = ObjectGroup}} = Object,
    Is3v3Scene = is_3v3_scene(SceneId),
    if
        Is3v3Scene andalso UserGroup > 0 andalso UserGroup =:= ObjectGroup -> ?ERRCODE(err200_have_invaded);
        true -> true
    end.

%% 开启3v3
gm_start_3v3(LastTime, MemberLimit) ->
    clusters_node:apply_cast(mod_3v3_center, gm_start_3v3, [LastTime, MemberLimit]),
    ok.

send_to_3v3_team(RoleId, BinData) ->
    mod_3v3_local:send_to_3v3_team(RoleId, BinData).

get_tier(Player) ->
    #player_status{role_3v3 = #role_3v3{tier = V}} = Player,
    V.
