%% ---------------------------------------------------------------------------
%% @doc    充值活动 每日礼包
%% @author liuxl
%% @since  2017-04-10
%% @deprecated
%% ---------------------------------------------------------------------------

-module (lib_daily_gift).
-include ("server.hrl").
-include ("figure.hrl").
-include ("recharge_act.hrl").
-include ("rec_recharge.hrl").
-include ("def_cache.hrl").
-include ("def_recharge.hrl").
-include ("def_event.hrl").
-include ("goods.hrl").
-include ("language.hrl").
-include ("errcode.hrl").
-include ("common.hrl").
-include ("rec_event.hrl").
-include ("def_module.hrl").
-include ("def_vip.hrl").
-include("def_goods.hrl").
-compile (export_all).

can_recharge(PS, ProductId) ->
	case catch mod_recharge_act:get_daily_gift_state(PS#player_status.id, ProductId) of
		{ok, ?DAILY_STATE_NOT_PURCHASE} -> true;
		_ -> {false, ?ERRCODE(err158_already_buy)}
	end.


%% 3点刷新可开放的每日礼包列表id
update_daily_gift_cache() ->
	CacheKey = ?CACHE_KEY(?CACHE_RECHARGE_DAILY_GIFT, {?PRODUCT_TYPE_DAILY_GIFT, daily_gift}),
	ProductIds = get_daily_gift_list(),
	mod_cache:put(CacheKey, ProductIds).

%% 获取玩家每日礼包的状态
get_daily_gift_state(PS) ->
	#player_status{id = RoleId, sid = Sid} = PS,
	CacheKey = ?CACHE_KEY(?CACHE_RECHARGE_DAILY_GIFT, {?PRODUCT_TYPE_DAILY_GIFT, daily_gift}),
	Result = mod_cache:get(CacheKey),
	ProductIds = case Result of
		undefined ->
			GiftList = get_daily_gift_list(),
			mod_cache:put(CacheKey, GiftList),
			GiftList;
		Data -> Data
	end,
	ProductList = case catch mod_recharge_act:get_player_daily_gift(RoleId, ProductIds) of
		List when is_list(List) -> List;
		_Err -> []
	end,
	{ok, Bin} = pt_159:write(15951, [ProductList]),
	lib_server_send:send_to_sid(Sid, Bin),
	{ok, PS}.

%% 购买每日礼包检查
purchase_daily_gift(PS, ProductId) ->
	% #player_status{figure = #figure{vip = Vip}} = PS,
	DailyGiftCfg = data_daily_gift:get_daily_gift(ProductId),
	case lib_recharge_check:check_product(ProductId) of
		{true, Product} when is_record(DailyGiftCfg, base_recharge_daily_gift) ->
			case Product#base_recharge_product.product_type of
				?PRODUCT_TYPE_DAILY_GIFT ->
					case catch mod_recharge_act:get_daily_gift_state(PS#player_status.id, ProductId) of
						{ok, ?DAILY_STATE_NOT_PURCHASE} -> {true, PS};
						{ok, _State} -> {false, ?ERRCODE(err159_daily_purchased), PS};
						_ -> {false, ?FAIL, PS}
					end;
				_ -> {false, ?ERRCODE(err159_daily_type_err), PS}
			end;
		{true, _Product} -> {?ERRCODE(err159_daily_cfg_err), PS};
		{false, Res} -> {false, Res, PS}
	end.

%% 领取每日礼包
get_daily_gift(PS, ProductId) ->
	case catch mod_recharge_act:get_daily_gift_state(PS#player_status.id, ProductId) of
		{ok, State} -> get_daily_gift_do(PS, State, ProductId);
		_ -> {false, ?FAIL, PS}
	end.

get_daily_gift_do(PS, State, ProductId) ->
	if
		State == ?DAILY_STATE_NOT_PURCHASE -> {false, ?ERRCODE(err159_daily_not_purchase), PS};
		State == ?DAILY_STATE_GET -> {false, ?ERRCODE(err159_daily_is_get), PS};
		true -> get_daily_gift_core(PS, ProductId)
	end.

get_daily_gift_core(PS, ProductId) ->
	#base_recharge_daily_gift{reward = Reward} = data_daily_gift:get_daily_gift(ProductId),
	mod_recharge_act:receive_daily_gift(PS#player_status.id, ProductId),
	PS1 = lib_goods_api:send_reward(PS, #produce{type = recharge_daily_gift, reward = Reward}),
	{true, PS1}.

%% 4点清楚玩家每日礼包
clear_daily_gift(DailyGift) ->
	F = fun(_K, V, Acc) ->
            #ps_daily{player_id = _RoleId, product_id = ProductId, state = State} = V,
            if Acc rem 20 == 0 -> timer:sleep(100); true -> skip end,
            if
                State =/= ?DAILY_STATE_NOT_GET -> Acc;
                true ->
                    #base_recharge_daily_gift{reward = _Reward} = data_daily_gift:get_daily_gift(ProductId),
                    Acc+1
            end
        end,
    maps:fold(F, 1, DailyGift).

%% 购买每日礼包
deal_with_charge(PS, ProductCfg, _PayNo) ->
	#player_status{id = RoleId} = PS,
	#base_recharge_product{
		product_id = ProductId
	} = ProductCfg,
    mod_recharge_act:purchase_daily_gift(RoleId, ProductId),
    log_daily_gift(RoleId, ProductId),
    case get_daily_gift(PS, ProductId) of
        {true, NewPS} ->
            {ok, Bin} = pt_159:write(15953, [?SUCCESS]),
            lib_server_send:send_to_sid(NewPS#player_status.sid, Bin);
        {false, Res, NewPS} ->
            {ok, Bin} = pt_159:write(15953, [Res]),
            lib_server_send:send_to_sid(NewPS#player_status.sid, Bin)
    end,
    get_daily_gift_state(NewPS),
    LastPS=lib_activitycalen_api:role_success_end_activity(NewPS, ?MOD_RECHARGE_ACT, ?MOD_RECHARGE_ACT_BUY_DAILY),
    {ok, LastPS}.


get_daily_gift_list() ->
	ProductIds = data_daily_gift:get_daily_gift_ids(),
	F = fun(Id, List) ->
		case lib_recharge_check:check_product(Id) of
			{true, _Product} -> [Id|List];
			_ -> List
		end
	end,
	lists:foldl(F, [], ProductIds).


%% 数据库db函数
db_update_daily_gift(PSDaily) ->
	#ps_daily{player_id = RoleId, product_id = ProductId, state = State} = PSDaily,
	Sql = io_lib:format(?SQL_DAILY_GIFT_UPDATE, [RoleId, ProductId, State]),
	db:execute(Sql).


db_clear_daily_gift() ->
	Sql = io_lib:format(?SQL_DAILY_GIFT_CLEAR, []),
	db:execute(Sql).


log_daily_gift(RoleId, ProductId) ->
	Now = utime:unixtime(),
	#base_recharge_daily_gift{level = Level, reward = Gift} = data_daily_gift:get_daily_gift(ProductId),
	ProductName = get_product_name(Level),
	F = fun(Item) ->
		case Item of
			{?TYPE_GOODS, TypeId, _Num} ->
				lib_log_api:log_daily_gift(RoleId, ProductName, TypeId, Now);
			_ -> ok
		end
	end,
	lists:foreach(F, Gift).

get_product_name(Level) ->
	case Level of
		1 -> unicode:characters_to_binary(data_language:get(?DAILY_GIFT_NAME1));
		_ -> <<"">>
	end.
