%%%-------------------------------------------------------------------
%%% @doc    扭蛋协议模块
%%% Module  : pp_capsule_egg.erl
%%% Created : 2020-06-24
%%% @Author : tyl
%%%-------------------------------------------------------------------
-module(pp_capsule_egg).

-include("common.hrl").
-include("server.hrl").
-include("capsule_egg.hrl").
-include("figure.hrl").
-include("goods.hrl").
-include("def_module.hrl").
-include("def_goods.hrl").
-include("predefine.hrl").
-include("errcode.hrl").

-export([handle/3]).

handle(Cmd, Status, Data) ->
	#player_status{figure = Figure} = Status,
	% OpenLv = lib_module:get_open_lv(?MOD_CAPSULE_EGG),
	case Figure#figure.lv >= 0 of
		true ->
			case lists:member(Cmd, [17004,17005,17006,17007,17008,17009]) of
				true -> skip;
				_ -> do_handle(Cmd, Status, Data)
			end;
		false -> skip
	end.

%% 查询扭蛋信息
do_handle(17000, Player, [PoolType]) ->
	case is_act_open(Player, PoolType) of
		true ->
			lib_capsule_egg:get_capsule_egg_info(Player, PoolType);
		{false, _Err} ->
			% lib_capsule_egg:send_msg(Player#player_status.sid, 17000, Err),
			{ok, Player}
	end;

%% 查询全服抽奖记录
do_handle(17001, Player, [PoolType]) ->
	case is_act_open(Player, PoolType) of
		true ->
			case custom_unite_buff:check_buff(Player, capsule_egg_keep) of
				{ok, AddBuff} -> ok;
				_ -> AddBuff = 0
			end,
			mod_capsule_egg:capsule_egg_info(Player#player_status.id, PoolType, AddBuff);
		{false, _Err} ->
			ignore
			% lib_capsule_egg:send_msg(Player#player_status.sid, 17009, Err)
	end,
	{ok, Player};

%% 抽奖
do_handle(17003, Player, [PoolType, DrawTimes, IsFree]) ->
	case is_act_open(Player, PoolType) of
		true ->
			case custom_unite_buff:check_buff(Player, capsule_egg_keep) of
				{ok, AddBuff} -> ok;
				_ -> AddBuff = 0
			end,
			{ok, NewPlayer} = lib_capsule_egg:draw_capsule_egg(Player, PoolType, DrawTimes, IsFree, AddBuff),
			{ok, NewPlayer};
		{false, _Err} ->
			% lib_capsule_egg:send_msg(Player#player_status.sid, 17000, Err),
			{ok, Player}
	end;

%% 查询积分
do_handle(17004, Player, []) ->
	#player_status{capsule_egg = StatusCapsuleEgg, sid = Sid} = Player,
	#status_capsule_egg{score = Score} = StatusCapsuleEgg,
	{ok, Bin} = pt_170:write(17004, [Score]),
	lib_server_send:send_to_sid(Sid, Bin),
	{ok, Player};

%% 查询积分商城商品信息
do_handle(17005, Player, []) ->
	lib_capsule_egg:send_score_store_goods(Player),
	{ok, Player};

%% 积分兑换
do_handle(17006, Player, [Pos]) ->
	{ok, NewPlayer} = lib_capsule_egg:exchange_goods(Player, Pos),
	{ok, NewPlayer};

%% 查询仓库物品
do_handle(17007, Player, []) ->
	GoodsStatus = lib_goods_do:get_goods_status(),
	GoodsList = lib_goods_util:get_goods_list(GoodsStatus#goods_status.dict, ?GOODS_LOC_CAPSULE_EGG_BAG),
	F = fun(#goods{id = GoodsId, goods_id = GoodTypeId, num = GoodNum, ctime = CreateTime}, TPackList) ->
		[{GoodsId, GoodTypeId, GoodNum, CreateTime}|TPackList];
		(_, TPackList) ->
			TPackList
	end,
	PackList = lists:foldl(F, [], GoodsList),
	{ok, Bin} = pt_170:write(17007, [PackList]),
	lib_server_send:send_to_sid(Player#player_status.sid, Bin),
	{ok, Player};

%% 从仓库提取单个物品
do_handle(17008, Player, [GoodsId]) ->
	ErrorCode = lib_goods_api:move_a2b(Player, GoodsId, ?GOODS_LOC_CAPSULE_EGG_BAG, ?GOODS_LOC_BAG),
	{ok, Bin} = pt_170:write(17008, [ErrorCode, GoodsId]),
	lib_server_send:send_to_sid(Player#player_status.sid, Bin),
	{ok, Player};

%% 从仓库提取所有物品
do_handle(17009, Player, []) ->
	ErrorCode = lib_goods_api:move_more_a2b(Player, ?GOODS_LOC_CAPSULE_EGG_BAG, ?GOODS_LOC_BAG),
	{ok, Bin} = pt_170:write(17009, [ErrorCode]),
	lib_server_send:send_to_sid(Player#player_status.sid, Bin),
	{ok, Player};

%% 查询/领取积分奖励
do_handle(17011, Player, [Option, PoolType, Times]) ->
	case Option of
		1 ->
			lib_capsule_egg:send_times_reward_info(Player, PoolType),
			{ok, Player};
		2 ->
			lib_capsule_egg:receive_times_reward(Player, PoolType, Times);
		_ ->
			{ok, Player}
	end;

do_handle(_Cmd, _PS, _Args) ->
	?ERR("Nomatch Cmd:~p Args:~p", [_Cmd, _Args]),
	{ok, _PS}.

%% 判断是否符合条件
is_act_open(Player, PoolType) ->
	#player_status{figure = Figure} = Player,
	OpenDay = util:get_open_day(),
	Lv      = Figure#figure.lv,
	PreTask = data_module:get_sub_pre_task_id(?MOD_CAPSULE_EGG, PoolType),
	OpenLv  = data_module:get_module_open_lv(?MOD_CAPSULE_EGG, PoolType),
	OpenDayCfg = data_module:get_open_day(?MOD_CAPSULE_EGG, PoolType),
	CheckList = [{lv, Lv, OpenLv}, {pre_task, PreTask}, {open_day, OpenDay, OpenDayCfg}],
	case check(CheckList) of
		true -> true;
		{false, Err} -> {false, Err}
	end.

check([]) -> true;
check([Check|L]) ->
	case do_check(Check) of
		true ->
			check( L);
		Other ->
			Other
	end.

do_check({lv, Lv, OpenLv}) ->
	case Lv >= OpenLv of
		true -> true;
		_ -> {false, ?ERRCODE(err170_lv_not_enough)}
	end;
do_check({pre_task, PreTask}) ->
	case PreTask of
		0 -> true;
		_ ->
			case lib_role_task:is_finish_task_id(PreTask) of
				true -> true;
				_    ->	{false, ?ERRCODE(err170_pre_task_not_finish)}
			end
	end;
do_check({open_day, OpenDay, OpenDayCfg}) ->
	case OpenDay >= OpenDayCfg of
		true -> true;
		_ -> {false, ?ERRCODE(err170_open_day_err)}
	end;
do_check(_) ->
	?ERR("<<Check capsule_egg false>>"),
	true.

