%%%------------------------------------
%%% @Module  : mod_rush_rank
%%% @Author  :  xiaoxiang
%%% @Created :  2016-11-17
%%% @Description: 冲榜榜单
%%%------------------------------------
-module(mod_rush_rank).

-include("rush_rank.hrl").
-include("common.hrl").

-export([
         refresh_rush_rank_by_list/1
        , refresh_rush_rank/3
        , send_rank_list/5
        ]).

%% -export([
%%         reload/0
%%         , get_state/0
%%     ]).

-export([start_link/0]).
-export([init/1,handle_call/3,handle_cast/2,handle_info/2,terminate/2,code_change/3]).

-compile(export_all).

%% @param [{RankType, CommonRankRole}]
refresh_rush_rank_by_list(List) ->
    gen_server:cast(?MODULE, {'refresh_rush_rank_by_list', List}).

%% 根据类型刷新榜单
refresh_rush_rank(RankType, SubType, RushRole) ->
    gen_server:cast(?MODULE, {'refresh_rush_rank', RankType, SubType, RushRole}).

%% 发送榜单的数据
send_rank_list(RankType, SubType, RoleId, SelValue1,SelValue2) ->
    gen_server:cast(?MODULE, {'send_rank_list', RankType, SubType, RoleId, SelValue1,SelValue2}).

%% 发送目标奖励列表
send_goal_list(SubType, RoleId) ->
     gen_server:cast(?MODULE, {'send_goal_list', SubType, RoleId}).

%% 领取奖励
get_goal_reward(RoleId, RankType, SubType, Goal, SelValue) ->
    gen_server:cast(?MODULE, {'get_goal_reward', RoleId, RankType, SubType, Goal, SelValue}).

%% 每日结算榜单
day_clear(DelaySec) ->
    RandTime = urand:rand(1, 1000),
    spawn(fun() -> timer:sleep(RandTime), gen_server:cast(?MODULE, {'day_clear', DelaySec}) end).

apply_cast(M, F, A) ->
    gen_server:cast(?MODULE, {'apply_cast', M, F, A}).
%% ---------------------------------------------------------------------------
start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

%% ---------------------------------------------------------------------------
init([]) ->
    State = lib_rush_rank_mod:init(),
    {ok, State}.

%% ---------------------------------------------------------------------------
%% handle_call({'reload'}, _From, _State) ->
%%     State = lib_common_rank_mod:init(),
%%     {reply, ok, State};
%% handle_call({'state'}, _From, State) ->
%%     % ?ERR1("~p state:~p~n", [?MODULE, State]),
%%     {reply, State, State};
handle_call(_Request, _From, State) ->
    %% ?ERR1("Handle unkown request[~w]~n", [_Request]),
    Reply = ok,
    {reply, Reply, State}.

%% ---------------------------------------------------------------------------
handle_cast(Msg, State) ->
    case catch do_handle_cast(Msg, State) of
        {ok, NewState} ->
            {noreply, NewState};
        Err ->
            %% util:errlog("~p ~p Msg:~p Cast_Error:~p ~n", [?MODULE, ?LINE, Msg, Err]),
            ?ERR("Msg:~p Cast_Error:~p~n", [Msg, Err]),
            {noreply, State}
    end.

do_handle_cast({'refresh_rush_rank_by_list', List}, State) ->
    lib_rush_rank_mod:refresh_rush_rank_by_list(State, List);
do_handle_cast({'refresh_rush_rank', RankType, SubType, RushRole}, State) ->
    lib_rush_rank_mod:refresh_rush_rank(State, RankType, SubType, RushRole);
do_handle_cast({'send_rank_list', RankType,  SubType, RoleId, SelValue1,SelValue2}, State) ->
    lib_rush_rank_mod:send_rank_list(State, RankType, SubType, RoleId, SelValue1,SelValue2),
    {ok, State};
do_handle_cast({'send_goal_list',  SubType, RoleId}, State) ->
    lib_rush_rank_mod:send_goal_list(State, SubType, RoleId),
    {ok, State};
do_handle_cast({'get_goal_reward',  RoleId, RankType, SubType, Goal, SelValue}, State) ->
    lib_rush_rank_mod:get_goal_reward(State,  RoleId, RankType, SubType, Goal, SelValue);
do_handle_cast({'day_clear', DelaySec}, State) ->
    lib_rush_rank_mod:day_clear(DelaySec, State);
do_handle_cast({'apply_cast', M, F, A}, State) ->
    lib_rush_rank_mod:apply_cast(State, M, F, A);

do_handle_cast(_Msg, State) ->
    %% ?ERR1("Handle unkown msg[~w]~n", [_Msg]),
    {ok, State}.

%% ---------------------------------------------------------------------------
handle_info(_Info, State) ->
    ?ERR("Handle unkown info[~w]~n", [_Info]),
    {noreply, State}.


%% ---------------------------------------------------------------------------
terminate(_Reason, _State) ->
    ok.

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