%% ---------------------------------
%% desc     :新手任务进度
%% module   :lib_role_newer
%% author   :zmh
%% ---------------------------------

-module(lib_role_newer).

-export([
	login/1,
	get_already_got/1,
	fetch_awards/3,
	check/1
]).
-include("errcode.hrl").
-include("def_module.hrl").
-include("predefine.hrl").
-include("goods.hrl").
-include("server.hrl").

-define(SQL_PLAYER_NEWER_GET, <<"SELECT `type`, `task_id` FROM `player_newer_task` WHERE rid=~p">>).
-define(SQL_PLAYER_NEWER_INSERT,<<"REPLACE INTO `player_newer_task` (`rid`,`type`,`task_id`,`time`) VALUES (~p,~p,~p,~p)">>).

login(RoleID)->
	Types = data_newer:get_types(),
	Sql = io_lib:format(?SQL_PLAYER_NEWER_GET, [RoleID]),
	All = db:get_all(Sql),
	dict_put(All,Types),
	ok.

get_already_got(RoleID)->
	Types = data_newer:get_types(),
	GotList = lists:foldl(fun(Type,Args)->
		List = dict_get(Type),
		List++Args
	end,[],Types),
	{ok,Bin} = pt_114:write(11400,[GotList]),
	lib_server_send:send_to_uid(RoleID,Bin),
	ok.

fetch_awards(Status,Type,TaskId)->
	#player_status{id=RoleID}=Status,
	GotList = dict_get(Type),
	CkList = [
        {check_got,TaskId,GotList},
        {check_finish,TaskId},
        {check_cfg,Type,TaskId}
    ],
	case util:check_lists(?MODULE,CkList) of
    	{ok,_} ->

    		All = data_newer:get_type_tasks(Type),
    		Now = utime:unixtime(),
    		[NGotList,Awards] = calc_awards(All,GotList,RoleID,Type,TaskId,Now,[]),

    		put({newer_got,Type},NGotList),
    		Produce = #produce{type=role_newer,reward=Awards},
            lib_goods_api:send_reward_with_mail(RoleID, Produce, ?MOD_NEWER),

            Last = lists:last(All),
            case TaskId =:= Last of
            	true->
            		lib_task_api:finish_newer(Status,Type);
            	_->
            		skip
            end,
    		Res = ?SUCCESS;
    	{false,Res}->
    		skp
    		%NStatus = Status
    end,
    {ok,Bin} = pt_114:write(11401,[Res,Type,TaskId]),
    lib_server_send:send_to_uid(RoleID,Bin),
    Status.

check({check_got,TaskId,GotList})->
	case lists:keymember(TaskId,2,GotList) of
		true->
			{false,?ERRCODE(err114_cant_fetch_again)};
		_->
			true
	end;
check({check_cfg,Type,TaskId})->
	Awards =  data_newer:get_task_awards(Type,TaskId),
	case Awards /= [] of
		true->
			{ok,Awards};
		_->
			{false,?ERRCODE(err_config)}
	end;
check({check_finish,TaskId})->
	case lib_role_task:is_finish_task_id(TaskId) of
		true->
			true;
		_->
			{false,?ERRCODE(err114_need_finish_task)}
	end.

dict_put(_All,[])->
	skip;
dict_put(All,[H|T])->
	Lists = [{Type,TaskId}||[Type,TaskId]<-All,Type=:=H],
	put({newer_got,H},Lists),
	dict_put(All,T).


dict_get(Type)->
	case get({newer_got,Type}) of
		List when is_list(List)->
			List;
		_->
			[]
	end.

calc_awards([],GotList,_RoleID,_Type,_TaskId,_Now,Awards)->
	[GotList,util:combine_list(Awards,1)];
calc_awards([H|T],GotList,RoleID,Type,TaskId,Now,Awards)->
	case H=<TaskId of
		true->
			case lists:keymember(H,2,GotList) of
				false->
					Awards0 = data_newer:get_task_awards(Type,H),
					NAwards = Awards0++Awards,
					NGotList = [{Type,H}|GotList],
					Sql = io_lib:format(?SQL_PLAYER_NEWER_INSERT, [RoleID,Type,H,Now]),
    				db:execute(Sql),
					calc_awards(T,NGotList,RoleID,Type,TaskId,Now,NAwards);
				_->
					calc_awards(T,GotList,RoleID,Type,TaskId,Now,Awards)
			end;
		_->
			calc_awards(T,GotList,RoleID,Type,TaskId,Now,Awards)
	end.