%%%-----------------------------------
%%% @Module  : pt_300
%%% @Author  : zhenghehe
%%% @Created : 2010.06.17
%%% @Description: 30 任务信息
%%%-----------------------------------
-module(pt_300).
-include("common.hrl").
-include("task.hrl").
-export([read/2, write/2]).
-compile(export_all).

%%
%%客户端 -> 服务端 ----------------------------
%%

%% 任务列表
read(30000, _Data) ->
    {ok, []};

%% 任务追踪
read(30001, _Data) ->
    {ok, []};

%% 可接任务
read(30002, _Data) ->
    {ok, []};

%% 接受任务
read(30003, <<TaskId:32>>) ->
    {ok, [TaskId]};

%% 完成任务
read(30004, <<TaskId:32, DoubleType:8, ILen:16, Bin/binary>>) ->
    F = fun(_, {TB, Result}) ->
            <<ItemId:32, NewTB/binary>> = TB,
            {ok, {NewTB, Result++[ItemId]}}
    end,
    {ok,{ _, ItemList}} = util:for(1, ILen, F, {Bin, []}),
    {ok, [TaskId, DoubleType, ItemList]};

%% 放弃任务
read(30005, <<TaskId:32>>) ->
    {ok, [TaskId]};

%% 任务对话事件
read(30007, <<TaskId:32, NpcId:32>>) ->
    {ok, [TaskId, NpcId]};

%% 探索事件
read(30008, _) ->
    {ok, []};

%% 触发悬赏任务和公会周任务
read(30009, <<TaskType:8>>)->
    {ok, [TaskType]};

%% 查看当前进度
read(30010, <<TaskType:8>>)->
    {ok, [TaskType]};

%% 特殊任务奖励显示
read(30011, <<TaskId:32>>) ->
    {ok, [TaskId]};

%% 特殊任务奖励显示
read(30012, _) ->
    {ok, []};

%% 客户端触发任务杀怪数
read(30015, <<ContentType:8, MonId:64,TalkId:32>>) ->
    {ok, [ContentType, MonId,TalkId]};

%% 触发长剧情
read(30016, <<TaskId:32, Type:8, EndTime:32>>) ->
    {ok, [TaskId, Type, EndTime]};

read(30017, _) ->
    {ok, []};

%% 快速完成
read(30018, <<TaskId:32>>) ->
    {ok, [TaskId]};

read(30019, <<TaskType:8>>)->
    {ok, [TaskType]};

read(_Cmd, _R) ->
    {error, no_match}.

%%
%%服务端 -> 客户端 ------------------------------------
%%

%% --- NPC对话开始 ----------------------------------

%% 任务列表
write(30000,[TaskType, ActiveList, TriggerList])->
    ABin = pack_task_list_active(ActiveList),
    TBin = pack_task_list_trigger(TriggerList),
    Data = <<TaskType:8, ABin/binary, TBin/binary>>,
    {ok, pt:pack(30000, Data)};

%% 完成任务某环节
write(30001, [TaskId, Tip, State, NeedNum, NowNum])->
    TipBin = pack_task_tip_list(Tip),
    Data = <<TaskId:32, TipBin/binary, State:8, NeedNum:32, NowNum:32>>,
    {ok, pt:pack(30001, Data)};

%% 接受任务
write(30003,[TaskId, ErrCode])->
    {ok, pt:pack(30003, <<TaskId:32, ErrCode:32>>)};

%% 完成任务
write(30004,[Id, ErrCode, ObjL])->
    ObjLBin = pt:write_object_list(ObjL),
    {ok, pt:pack(30004, <<Id:32,ErrCode:32,ObjLBin/binary>>)};

%% 放弃任务
write(30005,[ErrCode])->
    {ok, pt:pack(30005, <<ErrCode:32>>)};

%% 更新任务数据
write(30006, [])->
    Data = <<>>,
    {ok, pt:pack(30006, Data)};

%% 触发悬赏任务和公会周任务
write(30009, [Code])->
    {ok, pt:pack(30009, <<Code:32>>)};

%% 查看当前进度
write(30010, [TaskType, FinishCount, Count, State])->
    {ok,  pt:pack(30010, <<TaskType:8, FinishCount:8, Count:8,State:8>>)};

%% 赏金任务和公会周任务奖励展示
write(30011, [TaskId, Rewards, ExtraRewards])->
    F = fun({Type, GId, Num}) -> <<Type:8, GId:32, Num:64>> end,
    RewardsBin = lists:map(F, Rewards),
    {RL, RB} = {length(RewardsBin), list_to_binary(RewardsBin)},
    ExRewardsBin = lists:map(F, ExtraRewards),
    {ERL, ERB} = {length(ExRewardsBin), list_to_binary(ExRewardsBin)},
    {ok,  pt:pack(30011, <<TaskId:32, RL:16, RB/binary, ERL:16, ERB/binary>>)};

%% 上一次完成的主线任务
write(30012, [TaskId])->
    {ok, pt:pack(30012, <<TaskId:32>>)};

%% 触发悬赏任务和公会周任务
write(30015, [Code])->
    {ok, pt:pack(30015, <<Code:32>>)};

%% 触发长剧情
write(30016, [Type])->
    {ok, pt:pack(30016, <<Type:8>>)};

write(30017, [TaskList])->
    L = length(TaskList),
    Bin = list_to_binary([<<Id:32>>||Id <- TaskList]),
    {ok, pt:pack(30017, <<L:16, Bin/binary>>)};

%%快速完成
write(30018, [Code])->
    {ok, pt:pack(30018, <<Code:32>>)};

write(30019, [Res,TaskType,State])->
    {ok, pt:pack(30019, <<Res:32,TaskType:8,State:8>>)};

write(_Cmd, _R) ->
    ?DEBUG("error cmd ~w R:~p~n", [_Cmd, _R]),
    {ok, pt:pack(0, <<>>)}.

%% -----------私有函数------------
%% 可接任务数据
pack_task_list_active([]) -> <<0:16>>;
pack_task_list_active(ActiveList)->
    Len = length(ActiveList),
    Bin = list_to_binary([pack_task_active(X) || X <- ActiveList]),
    <<Len:16, Bin/binary>>.

pack_task_active({Tid, Tip})->
    TipBin = pack_task_tip_list(Tip),
    <<Tid:32, TipBin/binary>>.

%% 已接任务数据
pack_task_list_trigger([]) -> <<0:16>>;
pack_task_list_trigger(TriggerList)->
    Len = length(TriggerList),
    Bin = list_to_binary([pack_task_trigger(X) || X <- TriggerList]),
    <<Len:16, Bin/binary>>.

pack_task_trigger({Tid, Tip, WitchState, NeedNum, NowNum, TriggerTime})->
    TipBin = pack_task_tip_list(Tip),
    <<Tid:32, TipBin/binary, WitchState:8, NeedNum:32, NowNum:32, TriggerTime:32>>.

%% pack_task_list([]) -> <<0:16>>;
%% pack_task_list(TaskList) ->
%%     Len = length(TaskList),
%%     Bin = list_to_binary([pack_task(X) || X <- TaskList]),
%%     <<Len:16, Bin/binary>>.

%% pack_task({Tid, Tip, WitchState, NeedNum, NowNum, TriggerTime}) ->
%%     TipBin = pack_task_tip_list(Tip),
%%     <<Tid:32, TipBin/binary, WitchState:8, NeedNum:32, NowNum:32, TriggerTime:32>>.

 %% 打包任务目标
pack_task_tip_list([]) -> <<0:16>>;
pack_task_tip_list(TipList) ->
    Len = length(TipList),
    Bin = list_to_binary([ pack_task_tip(X) || X <- TipList]),
    <<Len:16, Bin/binary>>.

pack_task_tip(EachTip) ->
    [State, Finish, Type, Id, Num, SceneId, X, Y, NowNum, PathFind] = EachTip,
    SceneType = 0,
    Ex = [],
    Name = <<>>,
    NLen = byte_size(Name),
    ExBin = list_to_binary(ulists:implode("#&", Ex)),
    ExL = byte_size(ExBin),
    <<Type:16, Finish:16, Id:32, NLen:16, Name/binary, Num:32, NowNum:32, SceneId:32, SceneType:8, X:16, Y:16, PathFind:8, ExL:16, ExBin/binary, State:8>>.
