%%%------------------------------------
%%% @Module  : mod_growth_score
%%% @Author  :  ywf
%%% @Created :  2019-4-24
%%% @Description: 成长积分活动
%%%------------------------------------

-module(mod_growth_score).

-include("common.hrl").

-export([
    act_end/1     %% 活动结束，清理数据
    , add_score/5 %% 加分
    , recieve_reward/4 %% 领取奖励
    , act_info/3  %% 获取界面信息
    ]).

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

act_end(SubType) ->
    gen_server:cast(?MODULE, {'act_end', SubType}).

add_score(RoleId, RwParam, GoodsId, Num, OpenSubList) ->
    gen_server:cast(?MODULE, {'add_score', RoleId, RwParam, GoodsId, Num, OpenSubList}).

recieve_reward(RoleId, RwParam, SubType, RewardId) ->
    gen_server:cast(?MODULE, {'recieve_reward', RoleId, RwParam, SubType, RewardId}).

act_info(RoleId, RwParam, SubType) ->
    gen_server:cast(?MODULE, {'act_info', RoleId, RwParam, SubType}).

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

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

handle_call(_Request, _From, State) ->
    Reply = ok,
    {reply, Reply, State}.

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

do_handle_cast({'act_info', RoleId, RwParam, SubType}, State) ->
    lib_growth_score:act_info(State, RoleId, RwParam, SubType),
    {ok, State};

do_handle_cast({'act_end', SubType}, State) ->
    NewState = lib_growth_score:clean_subtype(State, SubType),
    {ok, NewState};

do_handle_cast({'add_score', RoleId, RwParam, GoodsId, Num, OpenSubList}, State) ->
    NewState = lib_growth_score:add_score(State, RoleId, RwParam, GoodsId, Num, OpenSubList),
    {ok, NewState};

do_handle_cast({'recieve_reward', RoleId, RwParam, SubType, RewardId}, State) ->
    NewState = lib_growth_score:recieve_reward(State, RoleId, RwParam, SubType, RewardId),
    {ok, NewState};

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

handle_info(_Info, State) ->
    {noreply, State}.
terminate(_Reason, _State) ->
    ok.
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.