%%%-------------------------------------------------------------------
%%% @doc    孩子
%%% Module  : pp_child.erl
%%% Created : 2020-02-17
%%% @Author : tyl
%%%-------------------------------------------------------------------
-module(pp_child).

-include("common.hrl").
-include("server.hrl").
-include("child.hrl").
-include("figure.hrl").
-include("def_module.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_CHILD),
	%% 点赞不需要功能开放等级控制，有自己的等级配置
	case Cmd =:= 16510 orelse Cmd =:= 16511 of
		true -> do_handle(Cmd, Status, Data);
		_ ->
			case Figure#figure.lv >= OpenLv of
				true ->
					%% 屏蔽玩具
					case lists:member(Cmd, [16520, 16521, 16522, 16523, 16524, 16525, 16526, 16527, 16528, 16532]) of
						true -> skip;
						_ ->
							do_handle(Cmd, Status, Data)
					end;
				false -> skip
			end
	end.

%% 查询孩子基础信息
do_handle(16501, Player, []) ->
	Res = lib_child:get_child_info(Player),
	Res;

%% 领养孩子
do_handle(16502, Player, [Sex, Name]) ->
	NewPlayer = lib_child:adopt_child(Player, Sex, Name),
	{ok, NewPlayer};

%% 孩子改名
do_handle(16503, Player, [Name]) ->
	{ok, NewPlayer} = lib_child:rename_child(Player, Name),
	{ok, NewPlayer};

%% 孩子培养
do_handle(16504, Player, [IsAuto, GoodId, GoodNum]) ->
	{ok, NewPlayer} = lib_child:grow_up_child(Player, IsAuto, GoodId, GoodNum),
	{ok, NewPlayer};

%% 获取孩子日常任务信息
do_handle(16505, Player, []) ->
	lib_child_task:get_child_daily_task_info(Player),
	{ok, Player};

%% 同心度培养
do_handle(16506, Player, [Type, Id, Num]) ->
	{ok, NewPlayer} = lib_child:upgrade_link_heart(Player, Type, Id, Num),
	{ok, NewPlayer};

%% 点赞排行榜
do_handle(16507, Player, []) ->
	mod_child_vote_mgr:server_child_vote_rank(Player#player_status.id),
	{ok, Player};

%% 点赞记录
do_handle(16508, Player, []) ->
	lib_child_vote_mod:get_be_voted_log(Player),
	{ok, Player};

%% 频道晒娃
do_handle(16509, Player, [Channel]) ->
	{ok, NewPlayer} = lib_child_vote_mod:show_off_child(Player, Channel),
	{ok, NewPlayer};

%% 点赞孩子
do_handle(16510, Player, [BeVotedId]) ->
	#player_status{sid = Sid, id = RoleId, figure = Figure,
		status_child = StatusChild, server_id = ServerId} = Player,
%%	VoteTimes = lib_daily:get_count(RoleId, ?MOD_CHILD, 1),  %% 每日晒娃次数
	#status_child{vote_times = VoteTimes, be_voted_log = BeVotedLog,
		vote_role_list = VoteRoleList} = StatusChild,
	#child_constant_cfg{constant = VoteLimit} = data_child:get_child_kv_cfg(like_limit),
	#child_constant_cfg{constant = VoteLvLimit} = data_child:get_child_kv_cfg(like_lv),
	BeVotedSrvId = mod_player_create:get_serid_by_id(BeVotedId),
	IsSameSrv = lib_child:is_same_srv(ServerId, BeVotedId),
	IsConnected = clusters_api:is_connected_center(),
	%% 宠物生活技能增加给玩家的点赞次数
	PerAddVoteTimes = lib_pet_api:pet_life_skill_about_child_vote_add(Player),
	DailyAddTimes = lib_daily:get_count(RoleId, ?MOD_CHILD, 1, BeVotedId),
	CanAdd = case PerAddVoteTimes > 0 of
				 true when DailyAddTimes < PerAddVoteTimes -> true;
				 _ -> false
	         end,
	VoteLimit1 = case PerAddVoteTimes > 0 of
		             true -> VoteLimit * 2;
		             _ -> VoteLimit
	             end,
	if
		Figure#figure.lv < VoteLvLimit ->
			lib_child:send_error(Sid, ?ERRCODE(err165_child_role_lv_not_reach)), ok;
		IsSameSrv == false andalso IsConnected == false ->
			lib_child:send_error(Sid, ?ERRCODE(err333_not_connect_center)), ok;
		true ->
			case BeVotedId == RoleId of
				true ->
					lib_child:send_error(Sid, ?ERRCODE(err165_cannot_vote_my_child)), ok;
				_ ->
					case lists:member(BeVotedId, VoteRoleList) of
						true when CanAdd == false ->
							lib_child:send_error(Sid, ?ERRCODE(err165_has_vote_for_this_child)),
							{ok, Player};
						_ ->
							case VoteTimes < VoteLimit1 of
								true ->
									%% 记录给单个玩家增加的次数(第二次点赞的时候加)
									case lists:member(BeVotedId, VoteRoleList) of
										true -> lib_daily:increment(RoleId, ?MOD_CHILD, 1, BeVotedId);
										_ -> ignore
									end,
									NewBeVotedLog = lib_child:change_feed_back_state(RoleId, BeVotedId, BeVotedLog),
									%% 区分本服和跨服点赞
									case IsSameSrv of
										true ->
											lib_player:apply_cast(
												BeVotedId, ?APPLY_CAST_SAVE, ?HAND_OFFLINE, lib_child, add_vote_for_child, [[RoleId, Figure#figure.name, CanAdd]]
											);
										_ ->
											clusters_node:apply_cast(lib_child, add_vote_for_child_on_node, [BeVotedSrvId, BeVotedId, [RoleId, Figure#figure.name, CanAdd]])
									end,
									%% 孩子任务
									Player1 = lib_child_task:finish_vote_for_child(Player),
									contract_task_api:vote_child_trigger(RoleId, 1),
									NewVoteTimes = VoteTimes + 1,
									NewVoteList  = [BeVotedId|lists:delete(BeVotedId, VoteRoleList)],
									NewStatusChild = StatusChild#status_child{vote_times = NewVoteTimes,
										be_voted_log = NewBeVotedLog, vote_role_list = NewVoteList},
									NewPlayer = lib_child_util:save_dict_status_child(NewStatusChild, Player1),
									%% 存库
									case StatusChild#status_child.child_sex == 0 of
										true ->
											lib_child_util:save_db_child(RoleId, "", 0, 0, 0, 0,
												StatusChild#status_child.heart_link_exp, StatusChild#status_child.heart_reward,
												NewVoteTimes, 0, 0, 0, 0, 0, 0
											);
										_ ->
											lib_child_util:update_db_child_vote_times(RoleId, NewVoteTimes, utime:unixtime())
									end,

									SFun = fun(TRoleId, TList) ->
										TTimes = lib_daily:get_count(RoleId, ?MOD_CHILD, 1, TRoleId),
										[{TRoleId, 1+min(1, TTimes)}|TList]
										end,
									SendVoteList = lists:foldl(SFun, [], NewVoteList),

									lib_child:send_msg(Sid, 16519, [SendVoteList]), % 推送点赞玩家列表
									lib_child:send_msg(Sid, 16510, [NewVoteTimes]),
									lib_achievement_api:child_vote_event(NewPlayer, 1),
									lib_seven_days_goal:finish_task(RoleId, ?MOD_CHILD, 0, 1),
									{ok, NewPlayer};
								_ ->
									lib_child:send_error(Sid, ?ERRCODE(err165_child_vote_times_not_enough)),
									{ok, Player}
							end
					end
			end
	end;

%% 查询孩子的点赞数
do_handle(16511, Player, [SRoleId]) ->
	#player_status{id = RoleId, sid = Sid} = Player,
	ServerId = config:get_server_id(),
	FromNode = clusters_node:get_node(),
	IsSameSrv    = lib_child:is_same_srv(ServerId, SRoleId),
	IsConnected  = clusters_api:is_connected_center(),
	if
		IsSameSrv == false andalso IsConnected == false ->
			lib_child:send_error(Sid, ?ERRCODE(err333_not_connect_center)), ok;
		IsSameSrv == false ->
			lib_child_vote_mod:get_child_be_voted_count_via_center(RoleId, SRoleId, FromNode),
			ok;
		true ->
			mod_child_vote_mgr:get_child_be_voted_count(RoleId, SRoleId, FromNode, FromNode)
	end,
	{ok, Player};

%% 查询孩子技能信息
do_handle(16512, Player, []) ->
	lib_child:get_child_skill(Player),
	{ok, Player};

%% 激活/升级孩子技能
do_handle(16513, Player, [Type, SkillId]) ->
	case Type of
		1 ->
			lib_child:active_child_skill(Player, SkillId);
		2 ->
			lib_child:upgrade_child_skill(Player, SkillId);
		_ ->
			{ok, Player}
	end;

%% 查询孩子载具信息
do_handle(16514, Player, []) ->
	lib_child_items:get_child_vehicle_info(Player),
	{ok, Player};

%% 激活/升阶孩子载具
do_handle(16515, Player, [Type, VehicleId]) ->
	case Type of
		1 ->
			lib_child_items:active_child_vehicle(Player, VehicleId);
		2 ->
			lib_child_items:upgrade_child_vehicle(Player, VehicleId);
		_ ->
			{ok, Player}
	end;

%% 设置孩子跟随
do_handle(16516, Player, []) ->
	NewPlayer = lib_child:set_child_follow(Player),
	{ok, NewPlayer};

%% 设置载具跟随
do_handle(16517, Player, [Type, VehicleId]) ->
	case Type == 1 orelse Type == 2 of
		true ->
			NewPlayer = lib_child:set_vehicle_follow(Player, Type, VehicleId),
			{ok, NewPlayer};
		_ ->
			{ok, Player}
	end;

%% 查询点赞过对方孩子的玩家列表
do_handle(16519, Player, []) ->
	lib_child:get_vote_role_list(Player),
	{ok, Player};

%% 查询玩具基本信息
do_handle(16520, Player, []) ->
	lib_child_items:get_toy_slot_info(Player),
	{ok, Player};

%% 查询玩具信息
do_handle(16521, Player, [GoodId]) ->
	lib_child_items:get_toy_info(Player, GoodId),
	{ok, Player};

%% 装备/卸下玩具
do_handle(16522, Player, [Type, SlotId, GoodId]) ->
	case Type of
		1 ->
			NewPlayer = lib_child_items:inlay_toy(Player, SlotId, GoodId),
			{ok, NewPlayer};
		2 ->
			NewPlayer = lib_child_items:load_off_toy(Player, SlotId, GoodId),
			{ok, NewPlayer};
		_ ->
			{ok, Player}
	end;

%% 玩具强化/强化回退
do_handle(16523, Player, [Type, GoodId, AimLv]) ->
	case Type of
		1 ->
			NewPlayer = lib_child_items:stren_toy(Player, GoodId, AimLv),
			{ok, NewPlayer};
		2 ->
			% NewPlayer = lib_child_items:stren_return(Player, GoodId),
			{ok, Player};
		_ ->
			{ok, Player}
	end;

%% 玩具合成
do_handle(16524, Player, [GoodIdList]) ->
	lib_child_items:compose_toy(Player, GoodIdList);

%% 查询玩具打造信息
do_handle(16525, Player, []) ->
	lib_child_items:get_toy_forge_info(Player),
	{ok, Player};

%% 玩具打造
do_handle(16526, Player, [SlotId]) ->
	{ok, NewPlayer} = lib_child_items:forge_toy(Player, SlotId),
	{ok, NewPlayer};

%% 领取完成打造玩具
do_handle(16527, Player, [SlotId]) ->
	{ok, NewPlayer} = lib_child_items:draw_out_toy(Player, SlotId),
	{ok, NewPlayer};

%% 加速打造玩具
do_handle(16528, Player, [SlotId]) ->
	{ok, NewPlayer} = lib_child_items:speed_up_forge(Player, SlotId),
	{ok, NewPlayer};

%% 获取时装基本信息
do_handle(16529, Player, [Type]) ->
	lib_child_items:get_fashion_info(Player, Type);

%% 穿戴时装
do_handle(16530, Player, [Option, Type, FashionId]) ->
	case Option of
		1 ->
			{ok, NewPlayer} = lib_child_items:put_on_fashion(Player, Type, FashionId),
			{ok, NewPlayer};
		2 ->
			{ok, NewPlayer} = lib_child_items:put_off_fashion(Player, Type, FashionId),
			{ok, NewPlayer};
		_ ->
			{ok, Player}
	end;

%% 激活/升级时装
do_handle(16531, Player, [Type, FashionId, IsAuto]) ->
	case Type of
		1 ->
			{ok, NewPlayer} = lib_child_items:active_fashion(Player, FashionId, IsAuto),
			{ok, NewPlayer};
		2 ->
			{ok, NewPlayer} = lib_child_items:upgrade_fashion(Player, FashionId, IsAuto),
			{ok, NewPlayer};
		_ ->
			{ok, Player}
	end;

%% 开启槽位/打造位
do_handle(16532, Player, [Type, SlotId]) ->
	case Type of
		1 ->
			lib_child_items:active_toy_slot(Player, SlotId);
		2 ->
			lib_child_items:active_forge_slot(Player, SlotId);
		_ ->
			{ok, Player}
	end;

%% 玩具套装信息
do_handle(16533, Player, []) ->
	lib_child_items:get_toy_suit_info(Player);

%% 套装奖励信息
do_handle(16534, Player, []) ->
	lib_child_items:get_toy_suit_reward_info(Player);

%% 领取套装奖励
do_handle(16535, Player, [SuitType]) ->
	lib_child_items:receive_toy_suit_reward(Player, SuitType);

%% 激活玩具
do_handle(16536, Player, [SuitType, ToyId]) ->
	lib_child_items:active_toy(Player, SuitType, ToyId);

%% 升级玩具盒子
do_handle(16537, Player, []) ->
	lib_child_items:upgrade_toy_box(Player);

%% 领取同心经验阶段奖励
do_handle(16539, Player, [Type, RewardId]) ->
	case Type of
		1 ->
			lib_child_task:receive_daily_task_reward(Player, RewardId);
		2 ->
			lib_child_task:quick_receive_task_reward(Player);
		_ ->
			{ok, Player}
	end;

%% 领取分享/昨日榜奖励
do_handle(16540, Player, [Type]) ->
	case Type of
		1 ->
			lib_child:receive_share_reward(Player);
		2 ->
			lib_child:receive_yest_reward(Player);
		_ ->
			{ok, Player}
	end;

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

