%%%-------------------------------------------------------------------
%%% @doc    孩子-点赞排行管理进程
%%% Module  : mod_child_vote_mgr.erl
%%% Created : 2020-02-17
%%% @Author : tyl
%%%-------------------------------------------------------------------
-module(mod_child_vote_mgr).
-behaviour(gen_server).

-include("common.hrl").
-include("server.hrl").
-include("figure.hrl").
-include("role.hrl").
-include("child.hrl").
-include("errcode.hrl").
-include("def_module.hrl").
-include("predefine.hrl").

%% API
-export([
	get_child_be_voted_count/4      %% 查询孩子点赞次数
	,server_child_vote_rank/1       %% 查询孩子点赞榜信息
	,add_child_vote_count/3         %% 为其他孩子打call
	,send_vote_rank_reward/0        %% 发放排行榜奖励

	,change_child_name/2            %% 修改孩子名字
	% ,login_refresh/3              %% 登录刷新点赞榜
	,change_player_power/2          %% 玩家战力更新
]).

-export([start_link/0, cast/1 ,call/1 ,info/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).


%% API
%% 查询孩子点赞次数
get_child_be_voted_count(RoleId, SRoleId, FromNode, ToNode) ->
	cast({get_child_be_voted_count, RoleId, SRoleId, FromNode, ToNode}).

%% 查询孩子点赞榜信息
server_child_vote_rank(RoleId) ->
	cast({server_child_vote_rank, RoleId}).

%% 点赞孩子
add_child_vote_count(RoleId, BeVotedRoleId, ChildName) ->
	cast({add_child_vote_count, RoleId, BeVotedRoleId, ChildName}).

%% 排行榜奖励结算
%% modify: 2021-07-01 改为玩家自己领取奖励
send_vote_rank_reward() ->
	cast({send_vote_rank_reward}).

%% 孩子名字修改
change_child_name(RoleId, ChildName) ->
	cast({change_child_name, RoleId, ChildName}).

%% 登录刷新(处理停服维护情况)
%%login_refresh(RoleInfo, BeVotedCount, ChildName) ->
%%	cast({login_refresh, RoleInfo, BeVotedCount, ChildName}).

%% 战力刷新
change_player_power(RoleId, RolePower) ->
	cast({change_player_power, RoleId, RolePower}).

%% ---------------------------------------------------------------------------
start_link() ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

cast(Msg) ->
	gen_server:cast(?MODULE, Msg).

call(Msg) ->
	gen_server:call(?MODULE, Msg).

info(Msg) ->
	erlang:send(?MODULE, Msg).

%% 停服重启需要重新排序
init([]) ->
	process_flag(trap_exit, true),
	erlang:send_after(50, self(), {init_child_vote_mgr}),
	{ok, #child_vote_mgr_state{}}.

handle_call(Msg, From, State) ->
	try do_handle_call(Msg, From, State) of
		Reply ->
			Reply
	catch ?EXCEPTION(Class, Reason, StackTrace) ->
		Error = {Class, Reason, StackTrace},
		?ERR("~w:handle_call error:~p~n", [?MODULE, Error]),
		{error, Reason}
	end.

handle_cast(Msg, State) ->
	try do_handle_cast(Msg, State) of
		Result ->
			Result
	catch ?EXCEPTION(Class, Reason, StackTrace) ->
		Error = {Class, Reason, StackTrace},
		?ERR("~w:handle_cast error:~p~n", [?MODULE, Error]),
		{noreply, State}
	end.

handle_info(Info, State) ->
	try do_handle_info(Info, State) of
		Result ->
			Result
	catch ?EXCEPTION(Class, Reason, StackTrace) ->
		Error = {Class, Reason, StackTrace},
		?ERR("~w:handle_info error:~p~n", [?MODULE, Error]),
		{noreply, State}
	end.

terminate(_Reason, _State) ->
	ok.

code_change(_OldVsn, State, _Extra) ->
	{ok, State}.

%% ---------------------------------------------------------------------------
do_handle_call(_Msg, _From, State) ->
	{reply, ok, State}.

%% ---------------------------------------------------------------------------
%% 查询孩子点赞次数
do_handle_cast({get_child_be_voted_count, RoleId, SRoleId, FromNode, ToNode}, State) ->
	lib_child_vote_mod:get_child_be_voted_count(State, RoleId, SRoleId, FromNode, ToNode),
	{noreply, State};

%% 查询点赞排行榜数据
do_handle_cast({server_child_vote_rank, RoleId}, State) ->
	lib_child_vote_mod:send_vote_rank_list(State, RoleId),
	{noreply, State};

%% 玩家点赞孩子
do_handle_cast({add_child_vote_count, RoleId, BeVotedRoleId, ChildName}, State) ->
	NewState = lib_child_vote_mod:add_child_vote_count(State, [RoleId, {BeVotedRoleId, ChildName}]),
	{noreply, NewState};

%% 发放点赞排行榜奖励
do_handle_cast({send_vote_rank_reward}, State) ->
	NewState = lib_child_vote_mod:send_vote_rank_reward(State),
	{noreply, NewState};

%% 修改孩子名字
do_handle_cast({change_child_name, RoleId, ChildName}, State) ->
	NewState = lib_child_vote_mod:change_child_name(State, RoleId, ChildName),
	{noreply, NewState};

%% 登录刷新点赞榜
%%do_handle_cast({login_refresh, RoleInfo, BeVotedCount, ChildName}, State) ->
%%	NewState = lib_child_vote_mod:login_refresh(State, RoleInfo, BeVotedCount, ChildName),
%%	{noreply, NewState};

%% 玩家战力更新,刷新排行榜
do_handle_cast({change_player_power, RoleId, RolePower}, State) ->
	NewState = lib_child_vote_mod:change_player_power(State, RoleId, RolePower),
	{noreply, NewState};

do_handle_cast(_Msg, State) ->
	?ERR("Handle unknow msg[~w]~n", [_Msg]),
	{noreply, State}.

%% ---------------------------------------------------------------------------
do_handle_info({init_child_vote_mgr}, State) ->
	case lib_child_util:get_db_child_vote_rank() of
		[] ->
			VoteRankList = [];
		RankInfos ->
			VoteRankList = lib_child_vote_mod:make_list(RankInfos)
	end,
	SortRankList = lib_child_vote_mod:sort_rank_list(VoteRankList),
	NewState = State#child_vote_mgr_state{
		rank_list     = SortRankList
		,refresh_time = utime:unixtime()},
	{noreply, NewState};

do_handle_info(_Info, State) ->
	?ERR("Handle unknow msg[~w]~n", [_Info]),
	{noreply, State}.


%% -------------------------- builtin function ---------------------------------
