%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     秒杀活动接口
%%% @end
%%%-------------------------------------------------------------------
-module(custom_act_seckill).
-author("liushl").

-include("custom_act.hrl").
-include("def_goods.hrl").
-include("server.hrl").
-include("errcode.hrl").
-include("predefine.hrl").
-include("figure.hrl").
%% API
-export([
    kf_notify_other_module/4            %% 活动派发接口：开启与结束
]).

-export([
    logout/1,
    buy_seckill/3,                      %% 秒杀-抢购
    buy_seckill_success/5,              %% 跨服购买成功回调
    buy_seckill_success_in_role/4,      %% 玩家进程处理扣钱+发奖
    
    get_reward_config/3,                %% 秒杀商品配置
    keyfind_reward_condition/3,
    keyfind_reward_condition/5
]).

%% @doc 活动派发接口：开启与结束
kf_notify_other_module(?CUSTOM_ACT_STATUS_OPEN, ActInfo, _Stime, _Etime) ->
    seckill_center:seckill_begin(ActInfo);
kf_notify_other_module(_, ActInfo, _Stime, _Etime) ->
    seckill_center:seckill_end(ActInfo).

%% 玩家离线-取消订阅
logout(Player) ->
    seckill_local:subscribe_seckill(Player#player_status.id, 0).

%% 秒杀-抢购
buy_seckill(Player, SubType, GradeId) ->
    Type = ?CUSTOM_ACT_TYPE_SECKILL,
    case lib_custom_act_api:is_open_act(Type, SubType) of
        true  ->
            case lib_custom_act_util:keyfind_reward_condition(Type, SubType, GradeId, limit_cost, 0) of
                0 ->
                    {false, ?MISSING_CONFIG};
                Cost ->
                    case lib_goods_api:check_object_list(Player, Cost) of
                        true  ->
                            seckill_center:buy_seckill_goods(SubType, Player#player_status.id, Player#player_status.figure#figure.name, SubType, GradeId);
                        {false, Error} ->
                            {false, Error}
                    end
            end;
        false ->
            {false, ?ERRCODE(err331_act_closed)}
    end.

%% 跨服购买成功回调
buy_seckill_success(Node, RoleId, SubType, GradeId, Round) ->
    clusters_center:apply_cast(
        Node,
        lib_player,
        apply_cast,
        [RoleId, ?APPLY_CAST_STATUS, ?MODULE, buy_seckill_success_in_role, [SubType, GradeId, Round]]
    ).

%% 玩家进程处理扣钱+发奖
buy_seckill_success_in_role(Player, SubType, GradeId, Round) ->
    Type = ?CUSTOM_ACT_TYPE_SECKILL,
    case lib_custom_act_util:keyfind_reward_condition(Type, SubType, GradeId, limit_cost, []) of
        [] ->
            Player;
        Cost ->
            case lib_goods_api:cost_object_list_with_check(Player, Cost, seckill, "") of
                {true, Player1, _}  ->
                    {ok, Bin} = pt_331:write(33191, [GradeId, 1, Round]),
                    lib_server_send:send_to_sid(Player#player_status.sid, Bin),
                    #custom_act_reward_cfg{reward = Reward}
                        = lib_custom_act_util:get_act_reward_cfg_info(?CUSTOM_ACT_TYPE_SECKILL, SubType, GradeId),
                    Player2 = lib_goods_api:send_reward(Player1, Reward, seckill),
                    
                    Figure = Player#player_status.figure,
                    lib_log_api:log_custom_act_reward(Player#player_status.id, Type, SubType, GradeId, 0, Reward, "", Figure#figure.vip, Figure#figure.lv,
                        Player#player_status.gold, Player2#player_status.gold, Player#player_status.bgold, Player2#player_status.bgold, utime:unixtime()),
                    
                    Player2;
                {false, _Error, _} ->
                    Player
            end
    end.

%% 秒杀商品配置
get_reward_config(Type, SubType, GradeId) ->
    case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
        #custom_act_reward_cfg{} = Cfg -> Cfg;
        _ -> #custom_act_reward_cfg{}
    end.
keyfind_reward_condition(Type, SubType, GradeId, Key, Default) ->
    keyfind_reward_condition(get_reward_config(Type, SubType, GradeId), Key, Default).
keyfind_reward_condition(Config, Key, Default) ->
    case lists:keyfind(Key, 1, Config#custom_act_reward_cfg.condition) of
        {_, Value} ->
            Value;
        _ ->
            Default
    end.