%%%-------------------------------------------------
%%% @Module  : lib_custom_act_do_vip_bag_onsale.erl
%%% @Author  : ckx
%%% @Created : 2019-06-17
%%% @Description:  VIP特惠活动
%%%-------------------------------------------------

-module(lib_custom_act_do_vip_bag_onsale).
-include("custom_act.hrl").
-include("common.hrl").
-include("server.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("def_fun.hrl").
-include("goods.hrl").
-include("def_goods.hrl").
-include("vip_bag_onsale.hrl").

-export([
	send_reward_status/2,               %% 发送礼包详情
	check_receive_reward_conditions/3,  %% 检测购买礼包条件是否满足
	receive_reward/4,					%% 购买礼包
	do_receive_reward/4                 %% 购买礼包
]).

send_reward_status(Player, ActInfo) ->
	#player_status{figure = #figure{vip = VIP}} = Player,
    #act_info{key = {Type, SubType}} = ActInfo,
    GradeIds = lib_custom_act_util:get_act_reward_grade_list(Type, SubType),
    F = fun(GradeId, Acc) ->
        case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
            #custom_act_reward_cfg{name = Name, desc = Desc, condition = Condition, format = Format, reward = Reward} = RewardCfg ->
                %% 检查礼包状态，是否显示，是否可购买
            	case lib_vip_bag_onsale:check_bag_status(VIP, Condition) of
            		BagStatus when (BagStatus =:= ?CAN_BUY orelse BagStatus =:= ?ONLY_SHOW) ->
            			if
            				BagStatus =:= ?CAN_BUY ->
            					%% 计算奖励的领取状态
		            			Status = lib_custom_act:count_reward_status(Player, ActInfo, 0, RewardCfg),
				                %% 计算奖励进度，在此处用于告知客户端该礼包是否仅用于展示 仅用于展示则返回1，默认为0
				                Progress = lib_custom_act_dispatcher:count_reward_progress(Player, ActInfo, 0, RewardCfg);
            				true ->
            					%% 领取状态 不可领取
				                Status = ?ACT_REWARD_CAN_NOT_GET,
				                %% 计算奖励进度，在此处用于告知客户端该礼包是否仅用于展示 仅用于展示则返回1，默认为0
				                Progress = ?ONLY_SHOW
            			end,
		                %% 计算奖励的已领取次数
		                ReceiveTimes = lib_custom_act:count_receive_times(Player, ActInfo, RewardCfg),
		                ConditionStr = util:term_to_string(Condition),
		                RewardStr = util:term_to_string(Reward),
		                [{GradeId, Format, Progress, Status, ReceiveTimes, Name, Desc, ConditionStr, RewardStr} | Acc];
            		_ -> Acc
            	end;
            _ -> Acc
        end
    end,
    PackList = lists:foldl(F, [], GradeIds),
    {ok, BinData} = pt_331:write(33104, [Type, SubType, PackList]),
    % ?DEBUG("PackList:~p", [PackList]),
    lib_server_send:send_to_sid(Player#player_status.sid, BinData).

%% true | {false, ErrorCode}
check_receive_reward_conditions(Player, _ActInfo, RewardCfg) ->
	#custom_act_reward_cfg{type = Type, subtype = SubType, grade = Grade} = RewardCfg,
	case lib_vip_bag_onsale:is_vip_level_right(Player, Type, SubType, Grade) of
		true ->
			case lib_vip_bag_onsale:get_onsale_price(Player, Type, SubType, Grade) of
				{_Price, _Percent} ->
					true;
				false ->
					{false, ?ERRCODE(fail)}
			end;
		{false, ErrorCode} -> {false, ErrorCode}
	end.

receive_reward(Player, ActType, SubType, GradeId) ->
    case lib_custom_act_check:check_receive_reward(Player, ActType, SubType, GradeId) of
        {true, ActInfo, NewReceiveTimes, RewardList, RewardCfg} ->
        	%% 获取当前的优惠价格
            {Price, _Percent} = lib_vip_bag_onsale:get_onsale_price(Player, ActType, SubType, GradeId),
            % ?DEBUG("receive_reward-getprice:~p~n", [Price]),
            %% 添加扣除钻石的代码，钻石足够则扣除钻石并发放礼包，不足则返回钻石不足
            case lib_goods_api:cost_object_list_with_check(Player, [{?TYPE_GOLD, 0, Price}], vip_bag_onsale, "") of
            	{true, Player1,_} ->
            		% ?DEBUG("receive_reward-costobject:~p~n", [Price]),
		            %% 先更新次数 避免更新次数出问题玩家重复领取
		            Player2 = lib_custom_act_dispatcher:update_receive_times_normal(Player1, ActType, SubType, GradeId, NewReceiveTimes),
		            case lib_custom_act_dispatcher:do_receive_reward(Player2, ActInfo, RewardList, RewardCfg) of
		                {false, ErrorCode} -> NewPlayer = Player2;
		                NewPlayer -> ErrorCode = ?SUCCESS
		            end;
		        {false, ErrorCode, _PS} -> NewPlayer = Player
		    end;
        {false, ErrorCode} -> NewPlayer = Player
    end,
    {ok, BinData} = pt_331:write(33105, [ErrorCode, ActType, SubType, GradeId]),
    lib_server_send:send_to_sid(Player#player_status.sid, BinData),
    {ok, NewPlayer}.

%% 记录日志，发放礼包
do_receive_reward(Player, ActInfo, RewardList, RewardCfg) ->
	#custom_act_reward_cfg{type = Type, subtype = SubType, grade = Grade, name = _Name} = RewardCfg,
    %% 此处检查背包数量是否足够，不会影响结果，因为在lib_custom_act_check:check_receive_reward/4中已经检查过一次了
    %% 也就是说正常情况下，能走到这里的，背包格子数量都是足够的
	case lib_goods_api:can_give_goods(Player, RewardList) of
        true ->
            #act_info{
                wlv = _WLv,
                key = {MainActType, SubActType}
            } = ActInfo,
            Produce = #produce{
                type    = custom_act,
                subtype = MainActType,
                reward  = RewardList,
                remark  = util:term_to_string(SubActType)
            },
            %% 日志
            {_Price, _Percent} = lib_vip_bag_onsale:get_onsale_price(Player, Type, SubType, Grade),
            %% LogExtra = lib_custom_act_dispatcher:get_extra_log(RewardCfg),
            % LogExtra = io_lib:format("grade:~p, price:~p", [Grade, Price]),
            %LogExtra = unicode:characters_to_binary(io_lib:format("grade:~p, name:~ts, price:~p", [Grade, Name, Price])),
            % ?DEBUG("LogExtra:~p~n",[LogExtra]),
            % ?DEBUG("do_receive_reward-getlogextra:~p~n", [LogExtra]),
            %lib_log_api:log_custom_act_reward(Player#player_status.id, MainActType, SubActType, Grade, WLv, RewardList, LogExtra, utime:unixtime()),
            % ?DEBUG("do_receive_reward-log:~n", []),
            lib_goods_api:send_reward(Player, Produce);
        _ ->
            {false, ?ERRCODE(err150_no_cell)}
    end.
