%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     BOSS首杀
%%% @end
%%%-------------------------------------------------------------------
-module(boss_first_blood_logic).
-author("liushl").

-include("server.hrl").
-include("equipment_check.hrl").
-include("errcode.hrl").
-include("predefine.hrl").
-include("rec_event.hrl").
-include("def_event.hrl").
%% API
-export([
    login/1,
    boss_die/2,
    handle_event/2,
    get_award/3,
    
    get_server_award_callback/3
]).

%% 登陆初始化
login(RoleId) ->
    SQL = io_lib:format(?SQL_GET_ROLE_BOSS_INFO, [RoleId]),
    BossInfo = db:get_all(SQL),
    [
        #role_boss_first_blood{
            boss_id         = BossId,    %% BOSS
            time            = Time,    %% 首杀时间
            self_award      = SelfAward,    %% 自己首杀领取状态
            server_award    = ServerAward     %% 全服首杀领取状态
        }
        || [BossId, SelfAward, ServerAward, Time] <- BossInfo
    ].

%% 完成击杀
boss_die(Player, BossId) ->
    case data_equipment_check:get_boss_first_blood(BossId) of
        #base_boss_first_blood{} ->
            #player_status{boss_first_blood = BossInfo} = Player,
            Res =
            case lists:keyfind(BossId, #role_boss_first_blood.boss_id, BossInfo) of
                #role_boss_first_blood{self_award = ?BOSS_FIRST_KILL_NOT} = OneBoss ->
                    {true, OneBoss};
                false ->
                    {true, #role_boss_first_blood{boss_id = BossId}};
                _ ->
                    false
            end,
            case Res of
                {true, Boss} ->
                    NewOneBoss = Boss#role_boss_first_blood{self_award = ?BOSS_FIRST_KILL_YES},
                    db_save_role_boss_info(Player#player_status.id, NewOneBoss),
                    NewBossInfo = lists:keystore(BossId, #role_boss_first_blood.boss_id, BossInfo, NewOneBoss),
                    {ok, Bin} = pt_145:write(14503, [1, BossId]),
                    lib_server_send:send_to_sid(Player#player_status.sid, Bin),
                    Player1 = Player#player_status{boss_first_blood = NewBossInfo},
                    Player1;
                false ->
                    Player
            end;
        _ ->
            Player
    end.

%% 领奖-全服首杀奖励
get_award(Player, BossId, 1) ->
    #player_status{boss_first_blood = BossInfo} = Player,
    case lists:keyfind(BossId, #role_boss_first_blood.boss_id, BossInfo) of
        #role_boss_first_blood{
            server_award    = 1     %% 全服首杀领取状态
        } ->
            {false, ?ERRCODE(err145_already_get)};
        _ ->
            boss_first_blood:get_server_award(Player#player_status.pid, BossId)
    end;
%% 领奖-个人首杀奖励
get_award(Player, BossId, 2) ->
    #player_status{boss_first_blood = BossInfo} = Player,
    OneBoss = lists:keyfind(BossId, #role_boss_first_blood.boss_id, BossInfo),
    BossCfg = data_equipment_check:get_boss_first_blood(BossId),
    if
        not is_record(OneBoss, role_boss_first_blood) -> {false, ?ERRCODE(err145_not_kill_yet)};
        not is_record(BossCfg, base_boss_first_blood) -> {false, ?MISSING_CONFIG};
        OneBoss#role_boss_first_blood.self_award =:= ?BOSS_FIRST_KILL_NOT -> {false, ?ERRCODE(err145_not_kill_yet)};
        OneBoss#role_boss_first_blood.self_award =:= ?BOSS_FIRST_KILL_AWARD -> {false, ?ERRCODE(err145_already_get)};
        true ->
            NewOneBoss = OneBoss#role_boss_first_blood{self_award = ?BOSS_FIRST_KILL_AWARD, time = utime:unixtime()},
            db_save_role_boss_info(Player#player_status.id, NewOneBoss),
            NewBossInfo = lists:keyreplace(BossId, #role_boss_first_blood.boss_id, BossInfo, NewOneBoss),
            Player1 = Player#player_status{boss_first_blood = NewBossInfo},
            Player2 = lib_goods_api:send_reward(Player1, BossCfg#base_boss_first_blood.self_first_award, boss_first_blood_self),
            {ok, Player2}
    end.

%% 全服首杀奖励领取回调
get_server_award_callback(Player, BossId, false) ->
    {ok, Bin} = pt_145:write(14502, [BossId, 1, ?ERRCODE(err145_not_kill_yet)]),
    lib_server_send:send_to_sid(Player#player_status.sid, Bin),
    Player;
get_server_award_callback(Player, BossId, true) ->
    case data_equipment_check:get_boss_first_blood(BossId) of
        #base_boss_first_blood{server_award = Award} ->
            #player_status{boss_first_blood = BossInfo, id = RoleId} = Player,
            OneBoss =
                case lists:keyfind(BossId, #role_boss_first_blood.boss_id, BossInfo) of
                    #role_boss_first_blood{} = _OneBoss ->
                        _OneBoss;
                    _ ->
                        #role_boss_first_blood{boss_id = BossId}
                end,
            NewOneBoss = OneBoss#role_boss_first_blood{server_award = 1, time = utime:unixtime()},
            db_save_role_boss_info(RoleId, NewOneBoss),
            NewBossInfo = lists:keystore(BossId, #role_boss_first_blood.boss_id, BossInfo, NewOneBoss),
            Player1 = Player#player_status{boss_first_blood = NewBossInfo},
            Player2 = lib_goods_api:send_reward(Player1, Award, boss_first_blood),
            {ok, Bin} = pt_145:write(14502, [BossId, 1, 1]),
            lib_server_send:send_to_sid(Player#player_status.sid, Bin),
            Player2;
        _ ->
            Player
    end.

%% 角色改名
handle_event(Player, #event_callback{type_id = ?EVENT_RENAME, data = Name}) ->
    boss_first_blood:rename(Player#player_status.id, Name),
    {ok, Player};
handle_event(Player, _) -> {ok, Player}.

db_save_role_boss_info(RoleId, BossInfo) ->
    #role_boss_first_blood{
        boss_id         = BossId,       %% BOSS
        time            = Time,         %% 首杀时间
        self_award      = SelfAward,    %% 自己首杀领取状态
        server_award    = ServerAward   %% 全服首杀领取状态
    } = BossInfo,
    SQL = io_lib:format(?SQL_SAVE_ROLE_BOSS_INFO, [RoleId, BossId, SelfAward, ServerAward, Time]),
    db:execute(SQL).
    