%% ---------------------------------------------------------------------------
%% @doc mod_task
%% @author ming_up@foxmail.com
%% @since  2016-11-19
%% @deprecated  玩家任务进程
%% ---------------------------------------------------------------------------
-module(mod_task).
-export([start_link/1, stop/1, init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-compile(export_all).

-include("common.hrl").
-include("server.hrl").
-include("task.hrl").
-include("def_module.hrl").
-include("daily.hrl").
-include("def_fun.hrl").

%% 开启任务进程
start_link(TaskArgs) ->
    gen_server:start_link(?MODULE, [TaskArgs], []).

%% 停止任务进程
stop(Pid) when is_pid(Pid) ->
    gen_server:cast(Pid, stop).

%% ================================= moduel api =================================
init([TaskArgs]) ->
    %% process_flag(priority, high),
    lib_task:flush_role_task(TaskArgs),
    {ok, 0}.

%% 执行函数
handle_call(Req, From, State) ->
    case catch do_handle_call(Req, From, State) of
        {reply, Reply, NewState} ->
            {reply, Reply, NewState};
        {stop, normal, NewState} ->
            {stop, normal, NewState};
        Reason ->
            ?ERR("handle_call Request: ~p, Reason=~p~n", [Req, Reason]),
            {reply, error, State}
    end.

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

handle_info(Info, State) ->
    case catch do_handle_info(Info, State)of
        {noreply, NewState} ->
            {noreply, NewState};
        {stop, normal, NewState} ->
            {stop, normal, NewState};
        Reason ->
            ?ERR("handle_info error: ~p, Reason:=~p~n",[Info, Reason]),
            {noreply, State}
    end.

terminate(Reason, State) ->
    if
        Reason == normal ->
            skip;
        true ->
            case lib_task:get_task_bag_id_list() of
                undefined -> skip;
                [] -> skip;
                RoleTaskList ->
                    spawn(fun()-> lib_task:task_offline_up(RoleTaskList) end)
            end
    end,
    {ok, State}.

code_change(_oldvsn, State, _extra) ->
    {ok, State}.

%% ================================= private fun =================================
%% call
do_handle_call({Fun, Args}, _FROM, State) ->
    {reply, apply(lib_task, Fun, Args), State};

%% 默认匹配
do_handle_call(Event, _From, Status) ->
    ?ERR("do_handle_call not match: ~p", [Event]),
    {reply, ok, Status}.

%% cast
%% 停止任务进程
do_handle_cast(stop, State) ->
    case lib_task:get_task_bag_id_list() of
        undefined -> skip;
        [] -> skip;
        RoleTaskList -> spawn(fun()-> lib_task:task_offline_up(RoleTaskList) end)
    end,
    {stop, normal, State};

%% 执行函数
do_handle_cast({Fun, Arg}, State) ->
    apply(lib_task, Fun, Arg),
    {noreply, State};

%% 默认匹配
do_handle_cast(_Event, State) ->
    ?PRINT("do_handle_cast not match: ~p", [_Event]),
    {noreply, State}.

%% info
%% 默认匹配
do_handle_info(_Info, State) ->
    ?PRINT("do_handle_info not match: ~p", [_Info]),
    {noreply, State}.
