%% ---------------------------------------------------------------------------
%% @doc pp_guild_labor.erl
%% @author  zmh
%% @since  20191207
%% @deprecated 社团劳务
%% ---------------------------------------------------------------------------
-module(pp_guild_labor).
-export([handle/3,check/1]).

-include("server.hrl").
-include("common.hrl").
-include("guild.hrl").
-include("errcode.hrl").
-include("goods.hrl").
-include("figure.hrl").
-include("def_module.hrl").
-include("guild_labor.hrl").


handle(40400, Status, []) ->
    #player_status{
        id = RoleID,
        guild_task = GuildTasks
    }=Status,
    TypeList = [{?MOD_GUILD_LABOR,0,1},{?MOD_GUILD_LABOR,0,2}],
    [{_,Stage},{_,Total}|_] = lib_daily:get_count(RoleID,TypeList),
    TaskList  = [{Tid,Ty,Pro,State}||#guild_task{id=Tid,content={Ty,Pro,_},state=State}<-GuildTasks],
    {ok,Bin} = pt_404:write(40400,[Total,Stage,TaskList]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,Status};

handle(40402,Status,[])->
    #player_status{
        id = RoleID,
        guild = GuildStatus
    }=Status,
    #status_guild{id=GuildId}=GuildStatus,
    [{_,Stage}] = lib_week:get_count(RoleID,[{?MOD_GUILD_LABOR,0,3}]),
    GuildId>0 andalso mod_guild:get_guild_week_labor(GuildId,RoleID,Stage),
    {ok,Status};

% handle(40403,Status,[0])->
%     #player_status{
%         id = RoleID,
%         guild = GuildStatus
%     }=Status,
%     #status_guild{lv=Glv}=GuildStatus,
%     TypeList = [{?MOD_GUILD_LABOR,0,1},{?MOD_GUILD_LABOR,0,2}],
%     [{_,CurStage},{_,Total}|_] = lib_daily:get_count(RoleID,TypeList),
%     OpenDay  = util:get_open_day(),
%     Config = data_guild_labor:get_role_labor_award(CurStage+1,Glv,OpenDay),
%     CheckList = [
%         {check_config,Total,Config}
%     ],
%     case util:check_lists(?MODULE,CheckList) of
%         {ok,Awards}->
%             Produce = #produce{title = utext:get(203), content = utext:get(204), reward = Awards, type = guild_role_labor, show_tips = 1},
%             lib_goods_api:send_reward_with_mail(RoleID, Produce,?MOD_GUILD_LABOR),
%             NStage = CurStage+1,
%             lib_daily:increment(RoleID, ?MOD_GUILD_LABOR, 1),
%             Res = ?SUCCESS;
%         {false,Res}->
%             NStage = 0
%     end,
%     {ok,Bin} = pt_404:write(40403,[0,Res,NStage]),
%     lib_server_send:send_to_uid(RoleID,Bin),
%     {ok,Status};

handle(40403,Status,[1])->
    #player_status{
        id = RoleID,
        figure = #figure{name=RName},
        guild = GuildStatus
    }=Status,
    #status_guild{id=GuildId,lv=Glv,name=GName}=GuildStatus,
    TypeList = [{?MOD_GUILD_LABOR,0,3}],
    [{_,CurStage}] = lib_week:get_count(RoleID,TypeList),
    TypeList0 = [{?MOD_GUILD_LABOR,0,2}],
    [{_,Total}|_] = lib_daily:get_count(RoleID,TypeList0),
    OpenDay  = util:get_open_day(),
    WeekLabors = mod_guild:get_guild_week_labor_value(GuildId),
    Config = data_guild_labor:get_guild_labor_award(CurStage+1,Glv,OpenDay),
    CheckList = [
        {check_min_active,Total},
        {check_config,WeekLabors,Config}
    ],
    case util:check_lists(?MODULE,CheckList) of
        {ok,Awards}->
            Produce = #produce{ reward = Awards, type = guild_labor, show_tips = 1},
            lib_goods_api:send_reward_with_mail(RoleID, Produce),
            NStage = CurStage+1,
            lib_week:increment(RoleID,?MOD_GUILD_LABOR,3),
            #guild_labor_awards{need=Need}=Config,
            lib_log_api:log_guild_lives_award(GuildId,GName,RoleID,RName,Total,Need,Awards),
            Res = ?SUCCESS;
        {false,Res}->
            NStage = 0
    end,
    {ok,Bin} = pt_404:write(40403,[1,Res,NStage]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,Status};

handle(40405,Status,[])->
    #player_status{
        id = RoleID,
        guild = GuildStatus
    }=Status,
    #status_guild{id=GuildId}=GuildStatus,
    GuildId>0 andalso mod_guild:get_guild_daily_log(GuildId, RoleID),
    {ok,Status};

handle(40407,Status,[TaskId])->
    #player_status{id = RoleID}=Status,
    case lib_guild_labor:fetch_task_award(TaskId,Status) of
        {ok,NStatus}->
            Res = ?SUCCESS;
        {false,Res}->
            NStatus = Status
    end,
    Total = lib_daily:get_count(RoleID,?MOD_GUILD_LABOR,2),
    {ok,Bin} = pt_404:write(40407,[Res,TaskId,Total]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,NStatus};

handle(_Cmd, _Player, _Data) ->
    {error, atom_to_list(?MODULE) ++ " no match"}.


check({check_min_active,Total})->
    Min = data_guild:get(min_active_require),
    case Total>= Min of
        true->
            true;
        _->
            {false,?ERRCODE(err404_need_more_role_lador)}
    end;
check({check_config,Total,Config})->
    case Config of
        #guild_labor_awards{need=Need,awards=Awards}->
            case Total>=Need of
                true->
                    {ok,Awards};
                _->
                    {false,?ERRCODE(err404_need_more_lador)}
            end;
        _->
            {false,?ERRCODE(err_config)}
    end.