%% ---------------------------------------------------------------------------
%% @doc lib_task_event
%% @author ming_up@foxmail.com
%% @since  2017-03-02
%% @deprecated lib_task_event
%% ---------------------------------------------------------------------------
-module(lib_task_event).

-include("common.hrl").
-include("task.hrl").
-include("predefine.hrl").
-include("def_module.hrl").
-include("server.hrl").

-export([async_finish/3, finish/3, trigger_after/2 ]).

%% 异步完成任务事件
async_finish(PlayerId, TaskId, Talk) ->
    lib_player:apply_cast(PlayerId, ?APPLY_CAST_SAVE, lib_task_event, finish, [TaskId,Talk]).

%% 完成任务事件 -> #player_status{}
finish(PS,TaskId,_Talk) ->
    RoleId = PS#player_status.id,
    #task{type = Type, next = Next} = data_task:get(TaskId),
    if
        TaskId =:= 11230->
            TaskArgs = lib_task_api:ps2task_args(PS),
            lib_role_task:auto_trigger_daily_task(TaskArgs),
            NewPS3 = PS;
        %% 主线任务
    	Type == ?TASK_TYPE_MAIN ->
            lib_task_api:finish_time_task(PS, TaskId), %% 触发限时任务
            NewPS3 = PS#player_status{last_task_id = TaskId};
        %% 转生任务
        Type == ?TASK_TYPE_TURN ->
            NewPS2 = PS#player_status{turn_task_id = 0},
            {ok, NewPS3} = transform:finish_task(NewPS2, TaskId, Next);
        %% 日常任务
        Type == ?TASK_TYPE_DAILY ->
            lib_custom_act_slices:task_event(PS,?MOD_TASK,1),
            NewPS3 = PS;
        %% 公会任务
        Type == ?TASK_TYPE_GUILD ->
            lib_custom_act_slices:task_event(PS,?MOD_TASK,2),
            NewPS3 = PS;
        true -> NewPS3 = PS
    end,
    NewPS4 = lib_pushmail:pushmail_task(NewPS3, TaskId),
    lib_task_api:event(?TASK_CONTENT_EXPECT_TASK, {id, TaskId}, RoleId),
    LastPS = NewPS4,
    {ok, LastPS}.

%%触发任务后事件
trigger_after(#task{id = TaskId, type = ?TASK_TYPE_TURN}, TaskArgs) ->
    #task_args{id = RoleId} = TaskArgs,
    lib_player:update_player_info(RoleId, [{turn_task_id, TaskId}]),
    ok;
trigger_after(_TD, _TaskArgs) ->
   ok.
