%%%--------------------------------------
%%% @Module  : pp_login_reward
%%% @Author  : hyh
%%% @Created : 2018.01.12
%%% @Description:  七天登录
%%%--------------------------------------
-module(pp_login_reward).

-export([handle/3]).

-include("server.hrl").
-include("errcode.hrl").
-include("figure.hrl").
-include("def_fun.hrl").
-include("predefine.hrl").
-include("common.hrl").
-include("def_module.hrl").
-include("login_reward.hrl").
-include("goods.hrl").

%% 获取七天登录状态
handle(17500, Ps, []) ->
    #player_status{id = RoleId, login_reward = LoginReward} = Ps,
    #login_reward{day_states = DayState, login_days = LoginDays} = LoginReward,
    DayIdList = data_login_reward:get_login_reward_day_list(),
    MaxCfgDay = lists:max(DayIdList),
    DayInfos = lib_login_reward:calc_day_states(DayState, LoginDays, MaxCfgDay),
    GettedDays = length(DayState),
    Code = if GettedDays >= LoginDays andalso GettedDays >= MaxCfgDay -> 
            ?ERRCODE(err175_login_reward_closed);
        true ->
            ?SUCCESS
    end,
    % ?ERR("~p ~p", [DayInfos, LoginDays]),
    {ok, Bin} = pt_175:write(17500, [Code, DayInfos, LoginDays]),
    lib_server_send:send_to_uid(RoleId, Bin),
    {ok, Ps};

%% 领取奖励
handle(17501, Ps, [DayId]) ->
    #player_status{id = RoleId, login_reward = LoginReward, figure = Figure, combat_power = CombatPower} = Ps,
    #figure{lv = Lv, career = Career} = Figure,
    #login_reward{day_states = DayState, login_days = LoginDays} = LoginReward,
    DayIdList = data_login_reward:get_login_reward_day_list(),
    DayIdMax  = lists:max(DayIdList),
    DayGettedMax = ?IF(DayState =/= [], length(DayState), 0),
    TodayDate = utime:unixdate(),
    LoginRewardCon = data_login_reward:get_login_reward_day_con(DayId),
    IsRewarded = lists:member(DayId, DayState),
    CfgLv = lib_module:get_open_lv(?MOD_LOGIN_REWARD),
    if
        Lv < CfgLv ->
            Code = ?LEVEL_LIMIT,
            NewPs = Ps;
        DayGettedMax >= DayIdMax -> % 已领完所有奖励
            Code = ?ERRCODE(err175_login_reward_closed),
            NewPs = Ps;
        DayId > LoginDays -> % 登录天数不足
            Code = ?ERRCODE(err175_not_enough_days),
            NewPs = Ps;
        not is_record(LoginRewardCon, login_reward_day_con) ->
            Code = ?ERRCODE(err_config),
            NewPs = Ps;
        IsRewarded ->
            Code = ?ERRCODE(err175_the_day_have_rewarded),
            NewPs = Ps;
        true ->
            #login_reward_day_con{reward_list = RewardList2, extra_reward_type = RewardType, extra_reward = ExtraReward} = LoginRewardCon,
            RewardList1 = lib_login_reward:filter_reward(RewardType, ExtraReward, Lv, Career, CombatPower), % 额外奖励
            % case lists:keyfind(Career, 1, Reward) of
            %     {Career, RewardList2} ->
            %         skip;
            %     _ ->
            %         RewardList2 = []
            % end,
            RewardList = lib_goods_util:merge_same_goods(RewardList1 ++ RewardList2),
            case lib_goods_api:can_give_goods(Ps, RewardList) of
                false ->
                    Code = ?ERRCODE(err401_bag_cell_not_enough),
                    NewPs = Ps;
                true ->
                    Produce = #produce{
                        type = login_reward, subtype = 0, reward = RewardList, remark = lists:concat(["DayId:", DayId])
                    },
                    case lib_login_reward:transaction_reward_do(Ps, Produce, DayId, TodayDate) of
                        {ok, NewPs} -> 
                            Code = ?SUCCESS, Now = utime:unixtime(),
                            spawn(fun() -> timer:sleep(urand:rand(1000, 5000)), lib_log_api:log_login_reward_day(RoleId, DayId, TodayDate, Now, RewardList) end);
                        {false, Code} -> 
                            NewPs = Ps
                    end
            end
    end,
    {ok, Bin} = pt_175:write(17501, [Code]),
    lib_server_send:send_to_uid(RoleId, Bin),
    {ok, NewPs};

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