%%%-------------------------------------------------------------------
%%% @doc    宠物
%%% Module  : pp_pet.erl
%%% Created : 2019-12-09
%%% @Author : tyl
%%%-------------------------------------------------------------------
-module(pp_pet).

-include("common.hrl").
-include("server.hrl").
-include("pet.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("def_module.hrl").
-include("goods.hrl").
-include("role.hrl").
-include("def_goods.hrl").

-export([handle/3]).

handle(Cmd, Player, Data) ->
	#player_status{figure = Figure} = Player,
	OpenLv = lib_module:get_open_lv(?MOD_PET),
	case Figure#figure.lv >= OpenLv of
		true ->
			do_handle(Cmd, Player, Data);
		false -> skip
	end.

%% 获取宠物基本信息
do_handle(16301, Player, []) ->
	#player_status{sid = Sid} = Player,
	{FollowPetId, PetList, _AllPetPower} = lib_pet:get_pets_info(Player),
	{ok, Bin} = pt_163:write(16301, [FollowPetId, PetList]),
	lib_server_send:send_to_sid(Sid, Bin);

%% 宠物喂养
do_handle(16302, Player, [PetId, GoodId, GoodNum]) ->
	NewPlayer = lib_pet:feed_pet(Player, PetId, GoodId, GoodNum),
	{ok, NewPlayer};

%% 宠物激活/进化
do_handle(16303, Player, [PetId, ActionType]) ->
	case ActionType of
		?ACTION_ACTIVE ->
			NewPlayer = lib_pet:adopt_pet(Player, PetId);
		?ACTION_UPGRADE ->
			NewPlayer = lib_pet:upgrade_pet(Player, PetId);
		_ -> NewPlayer = Player
	end,
	{ok, NewPlayer};

%% 获取宠物技能和宝石栏基本信息
do_handle(16304, Player, [PetId]) ->
	lib_pet:get_pet_skill_and_skill_diamonds(Player, PetId),
	{ok, Player};

%% 宠物技能宝石镶嵌/替换
do_handle(16305, Player, [PetId, DiamondSlot, DiamondId, GoodsId]) ->
	NewPlayer = lib_pet:inlay_skill_diamond(Player, PetId, DiamondSlot, DiamondId, GoodsId),
	{ok, NewPlayer};

%% 玩家宠物助战信息
do_handle(16307, Player, []) ->
	#player_status{status_pet = StatusPet, sid = Sid} = Player,
	#status_pet{sum_pet_star = SumStar} = StatusPet,
	PackList = lib_pet:get_normal_support_info(Player),
	{ok, Bin} = pt_163:write(16307, [SumStar, PackList]),
	lib_server_send:send_to_sid(Sid, Bin),
	{ok, Player};

%% 宠物上阵(助战)
do_handle(16308, Player, [PetId, SlotId]) ->
	NewPlayer = lib_pet:support_pet_normal(Player, SlotId, PetId),
	{ok, NewPlayer};

%% 获取昨日全服/跨服投票榜信息
do_handle(16309, Player, []) ->
	RoleId = Player#player_status.id,
	case lib_pet_support_mod:check_enter_center() of
		{_, ZoneId} when is_integer(ZoneId) ->
			Node = clusters_node:get_node(),
			clusters_node:apply_cast(
				mod_pet_support_center, center_send_yest_power_rank,
				[ZoneId, Node, RoleId]
			);
		_ ->
			mod_pet_support_mgr:send_server_yest_power_rank(RoleId)
	end,
	{ok, Player};

%% 查询昨日战力榜领奖情况
do_handle(16310, Player, []) ->
	#player_status{id = RoleId, status_pet = StatusPet} = Player,
	#status_pet{receive_pet_list = ReceiveList} = StatusPet,
	mod_pet_support_mgr:send_reward_receive_info(RoleId, ReceiveList),
	{ok, Player};

%% 获取聊天频道上分享宠物信息
do_handle(16311, Player, [ShareRoleId, PetId]) ->
	case lib_pet_data:get_ets_pet(ShareRoleId, PetId) of
		#ets_pet_info{
			pet_star  = PetStar
			,pet_lv    = PetLv
			,pet_attr  = PetAttr
			,pet_power = PetPower
		} ->
			case lib_role:get_role_show(ShareRoleId) of
				#ets_role_show{figure = Figure} -> skip;
				_ -> Figure = #figure{}
			end,
			PackList = [PetId, PetStar, PetLv, PetAttr, PetPower, Figure],
			{ok, Bin} = pt_163:write(16311, PackList),
			lib_server_send:send_to_sid(Player#player_status.sid, Bin);
		_ ->
			lib_pet:send_error(Player, ?ERRCODE(err163_role_not_adopt_pet))
	end,
	{ok, Player};

%% 宠物全服/跨服战力榜单
do_handle(16312, Player, []) ->
	% RoleId = Player#player_status.id,
	% case lib_pet_support_mod:check_enter_center() of
	%	ZoneId when is_integer(ZoneId) ->
	%		Node = clusters_node:get_node(),
	%		clusters_node:apply_cast(mod_pet_support_center, center_send_pet_power_rank, [ZoneId, Node, RoleId]);
	%	_ ->
	%		mod_pet_support_mgr:send_pet_power_rank(RoleId)
	% end,
	{ok, Player};

%% 宠物今日投票信息
do_handle(16313, Player, []) ->
	lib_pet:vote_pet_info(Player),
	{ok, Player};

%% 宠物投票
do_handle(16314, Player, [PetId]) ->
	NewPlayer = lib_pet:vote_for_pet(Player, PetId),
	{ok, NewPlayer};

%% 领取昨日排行榜奖励
do_handle(16315, Player, [PetId]) ->
	lib_pet:receive_vote_rank_reward(Player, PetId),
	{ok, Player};

%% 宠物觉醒
do_handle(16316, Player, [PetId]) ->
	NewPlayer = lib_pet:awaken_pet(Player, PetId),
	{ok, NewPlayer};

%% 技能石合成
do_handle(16318, Player, [Type, DiamondId]) when Type == 1 orelse Type == 2 ->
	{ok, NewPlayer} = lib_pet:compound_stone(Player, Type, DiamondId),
	{ok, NewPlayer};
do_handle(16318, Player, [_Type, _DiamondId]) ->
	{ok, Player};

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


