%%-----------------------------------------------------------------------------
%% @Module  :       lib_custom_act_do_limit_buy
%% @Author  :       Ywf
%% @Email   :       349162727@qq.com
%% @Created :       2019-05-13
%% @Description:	特惠商城
%%-----------------------------------------------------------------------------
-module (lib_custom_act_do_limit_buy).

-compile(export_all).
-include("custom_act.hrl").
-include("common.hrl").
-include("server.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("def_fun.hrl").
-include("goods.hrl").

%% 特惠商城
do_receive_reward(Player, #act_info{wlv = WLv, key = {Type, SubType}}, RewardList, RewardCfg) ->
    #custom_act_reward_cfg{
        grade     = GradeId,
        condition = Conditions
    } = RewardCfg,
    case lists:keyfind(cost, 1, Conditions) of
        {cost, [{CostGoodsType, CostGoodsId, CostNum} | _]} ->
            case lists:keyfind(discount, 1, Conditions) of
                {discount, Discount} -> ok;
                _ -> Discount = 100
            end,
            NewCostNum = round(CostNum * Discount / 100),
            case NewCostNum =< 0 of
                true ->
                    {false, ?FAIL};
                false ->
                    case lib_goods_api:cost_object_list_with_check(Player, [{CostGoodsType, CostGoodsId, NewCostNum}], limit_buy, util:term_to_string(RewardList)) of
                        {true, NewPs,_} ->
                            %% 日志
                            #player_status{figure = #figure{lv = Lv, vip = Vip}} = Player,
                            lib_log_api:log_custom_act_reward(Player#player_status.id, Type, SubType, GradeId, WLv, RewardList, [],
                                Vip, Lv, Player#player_status.gold, NewPs#player_status.gold, Player#player_status.bgold, NewPs#player_status.bgold, utime:unixtime()),
                            lib_goods_api:send_reward(NewPs, RewardList, limit_buy, 0),
                            mod_limit_buy:add_grade_daily_num(SubType, GradeId, Player#player_status.id),
                            NewPs;
                        {false, Res, _} ->
                            {false, Res}
                    end
            end;
        _ ->
            {false, ?MISSING_CONFIG}
    end.

    %% 特惠商城
check_receive_reward_conditions(Player, ActInfo = #act_info{key = {_, SubType}}, RwCfg) ->
    #custom_act_reward_cfg{
        grade = GradeId,
        condition = Conditions
    } = RwCfg,
    case mod_limit_buy:limit_buy_check(SubType, GradeId, Player#player_status.id) of
        {false, Code} ->
            {false, Code};
        true ->
            case lib_custom_act_util:count_act_reward(Player, ActInfo, RwCfg) of
                [] ->
                    {false, ?FAIL};
                _ ->
                    {cost, CostList} = lists:keyfind(cost, 1, Conditions),
                    {discount, Discount} = lists:keyfind(discount, 1, Conditions),
                    [{CostGoodsType, CostGoodsId, CostNum}|_] = CostList,
                    NewCostNum = round(CostNum*Discount/100),
                    lib_goods_api:check_object_list(Player, [{CostGoodsType, CostGoodsId, NewCostNum}])
            end
    end.

%% 特惠商城不在这判断次数
check_reward_receive_times(Player, #act_info{key = {ActType, SubType}, stime = Stime, etime = Etime}, #custom_act_reward_cfg{grade = GradeId}) ->
    StatusCustomAct = Player#player_status.status_custom_act,
    RewardMap = StatusCustomAct#status_custom_act.reward_map,
    case maps:get({ActType, SubType, GradeId}, RewardMap, []) of
        #reward_status{utime = Utime, receive_times = ReceiveTimes}
        when Utime >= Stime andalso Utime =< Etime ->
            {true, ReceiveTimes};
        _ ->
            {true, 0}
    end.

send_info(PS, SubType) ->
	ActType = ?CUSTOM_ACT_TYPE_LIMIT_BUY,
	#player_status{
        id = RoleId
    } = PS,
    case lib_custom_act_api:is_open_act(ActType, SubType) of
        false ->
            {ok, Bin} = pt_331:write(33111, [?ERRCODE(err331_act_closed), ActType, SubType, []]),
            lib_server_send:send_to_uid(RoleId, Bin);
        true ->
            mod_limit_buy:open_limit_buy(RoleId, SubType)
    end.
