%%%------------------------------------
%%% @Module  : pp_recharge_act
%%% @Author  :  xiaoxiang
%%% @Created :  2017-04-06
%%% @Description: 充值活动
%%%------------------------------------


-module (pp_recharge_act).
-include("server.hrl").
-include("common.hrl").
-include("errcode.hrl").
-include("rec_recharge.hrl").
-include("def_recharge.hrl").
-include("def_fun.hrl").
-include("daily.hrl").
-include("custom_act.hrl").
-include("recharge_act.hrl").
-include("def_module.hrl").
-export ([handle/3]).

%% 查看福利卡
handle(15901, Player, _Data) ->
    lib_recharge_welfare:get_welfare(Player);

%% 领取福利卡福利
handle(15902, Player, [ProductId]) ->
    NewPlayer = lib_recharge_welfare:get_welfare_reward(Player, ProductId),
    {ok, NewPlayer};

%% 商品信息
handle(15903, Player, [ProductType]) ->
    #player_status{sid = Sid} = Player,
    Info = lib_recharge_act:send_recharge_goods(Player, ProductType),
    lib_server_send:send_to_sid(Sid, pt_159, 15903, Info);

%% 商品购买检查
handle(15904, Player, [ProductId]) ->
    case lib_recharge_check:check_product(Player, ProductId) of
        {true, #base_recharge_product{money = 0, product_type = ?PRODUCT_TYPE_GIFT} = RechargeCfg}  ->
            lib_recharge:deal_with_charge(Player, RechargeCfg, 0);
        {true, _}  ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_159, 15904, [ProductId, 1]),
            ok;
        {false, Error} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_159, 15904, [ProductId, Error]),
            ok
    end;

%% 查看首充奖励状态
handle(15905, Player, []) ->
    lib_recharge_first:send_first_state_to_client(Player);

%% 领取首充奖励
handle(Cmd = 15906, Player, [Index, ProductId]) ->
    case lib_recharge_first:get_recharge_gift_award(Player, ProductId, Index) of
        {true, ErrCode, NewPS} ->
            {ok, Bin} = pt_159:write(Cmd, [Index, ProductId, ErrCode]),
            lib_server_send:send_to_sid(NewPS#player_status.sid, Bin),
            {ok, NewPS};
        {false, ErrCode} ->
            {ok, Bin} = pt_159:write(Cmd, [Index, ProductId, ErrCode]),
            lib_server_send:send_to_sid(Player#player_status.sid, Bin),
            {ok, Player}
    end;

%% 查看每日礼包状态
handle(15951, Player, []) ->
    lib_daily_gift:get_daily_gift_state(Player);

%% 购买每日礼包(检查是否能购买)
handle(15952, Player, [ProductId]) ->
    case lib_daily_gift:purchase_daily_gift(Player, ProductId) of
        {true, NewPS} ->
            {ok, Bin} = pt_159:write(15952, [?SUCCESS]),
            lib_server_send:send_to_sid(NewPS#player_status.sid, Bin),
            {ok, NewPS};
        {false, Res, NewPS} ->
            {ok, Bin} = pt_159:write(15952, [Res]),
            lib_server_send:send_to_sid(NewPS#player_status.sid, Bin),
            {ok, NewPS}
    end;

%% 领取每日礼包
handle(15953, Player, [ProductId]) ->
    case lib_daily_gift:get_daily_gift(Player, ProductId) of
        {true, NewPS} ->
            {ok, Bin} = pt_159:write(15953, [?SUCCESS]),
            lib_server_send:send_to_sid(NewPS#player_status.sid, Bin),
            {ok, NewPS};
        {false, Res, NewPS} ->
            {ok, Bin} = pt_159:write(15953, [Res]),
            lib_server_send:send_to_sid(NewPS#player_status.sid, Bin),
            {ok, NewPS}
    end;

handle(15955, PS, [SubType]) ->
    NowTime = utime:unixtime(),
    case lib_custom_act_util:get_custom_act_open_info(?CUSTOM_ACT_TYPE_DAILY_CHARGE_CYCLE, SubType) of
        #act_info{
            etime = Etime
        } = ActInfo when NowTime < Etime ->
            TodayGold = lib_recharge_data:get_today_pay_gold(PS#player_status.id), %% 获取当天充值量
            RewardIds = lib_custom_act_util:get_act_reward_grade_list(?CUSTOM_ACT_TYPE_DAILY_CHARGE_CYCLE, SubType), %% 获取奖励列表
            F = fun(Id, Acc) ->
                    case lib_custom_act_util:get_act_reward_cfg_info(?CUSTOM_ACT_TYPE_DAILY_CHARGE_CYCLE, SubType, Id) of
                        #custom_act_reward_cfg{condition = Condition, desc = Desc} = RewardCfg ->
                            case lib_custom_act_check:check_act_condtion([type, gold], Condition) of
                                [1, Gold] ->
                                    {State, _} = lib_recharge_cumulation:get_reward_state(PS, ActInfo, RewardCfg),
                                    RewardList = lib_custom_act_util:count_act_reward(PS, ActInfo, RewardCfg),
                                    [{Id, State, TodayGold, Gold, RewardList, Desc}|Acc];
                                _ ->
                                    Acc
                            end;
                        _ ->
                            Acc
                    end
            end,
            DataList = lists:foldl(F, [], RewardIds),
            {ok, BinData} = pt_159:write(15955, [SubType, TodayGold, DataList]),
            lib_server_send:send_to_sid(PS#player_status.sid, BinData);
        _ ->
            {ok, BinData} = pt_159:write(15955, [SubType, 0, []]),
            lib_server_send:send_to_sid(PS#player_status.sid, BinData)
    end;

handle(15956, PS, [SubType]) ->
    NowTime = utime:unixtime(),
    case lib_custom_act_util:get_custom_act_open_info(?CUSTOM_ACT_TYPE_DAILY_CHARGE_CYCLE, SubType) of
        #act_info{
            stime = StartTime,
            etime = Etime
        } = ActInfo when NowTime < Etime ->
            #player_status{cumulative_charge = Cumulation} = PS,
            CycleTime = maps:get({cycle_time, SubType}, Cumulation, StartTime),
            % TodayGold = lib_recharge_data:get_today_pay_gold(PS#player_status.id),
            %% 开始时间
            HistoryList = lib_recharge_data:get_my_daily_recharge_summaries(PS#player_status.id, CycleTime),
            % HistoryListWithToday = if CycleTime > NowTime -> HistoryList; true -> [{0, {TodayGold, 0}}|HistoryList] end,
            RewardIds = lib_custom_act_util:get_act_reward_grade_list(?CUSTOM_ACT_TYPE_DAILY_CHARGE_CYCLE, SubType),
            F = fun
                (Id, Acc) ->
                    case lib_custom_act_util:get_act_reward_cfg_info(?CUSTOM_ACT_TYPE_DAILY_CHARGE_CYCLE, SubType, Id) of
                        #custom_act_reward_cfg{condition = Condition, desc = Desc} = RewardCfg ->
                            case lib_custom_act_check:check_act_condtion([type, gold, day], Condition) of
                                [2, Gold, Day] ->
                                    {State, _} = lib_recharge_cumulation:get_reward_state(PS, ActInfo, RewardCfg),
                                    if
                                        State =:= ?ACT_REWARD_CAN_NOT_GET ->
                                            X = lists:sum([1 || {_, {G, _}} <- HistoryList, G >= Gold]);
                                        true ->
                                            X = Day
                                    end,
                                    RewardList = lib_custom_act_util:count_act_reward(PS, ActInfo, RewardCfg),
                                    [{Id, State, X, Day, RewardList, Desc}|Acc];
                                _ ->
                                    Acc
                            end;
                        _ ->
                            Acc
                    end
            end,
            DataList = lists:foldl(F, [], RewardIds),
            {ok, BinData} = pt_159:write(15956, [SubType, DataList]),
            lib_server_send:send_to_sid(PS#player_status.sid, BinData);
        _ ->
            {ok, BinData} = pt_159:write(15956, [SubType, []]),
            lib_server_send:send_to_sid(PS#player_status.sid, BinData)
    end;

%% 获取从某个时间0点开始到现在的充值总数
%% STime会转换成那个时刻当天的0点
handle(15957, PS, [Type, SubType, STime]) ->
    HistoryList = lib_recharge_data:get_my_daily_recharge_summaries(PS#player_status.id, STime),
    % HistoryListWithToday = [{0, {TodayGold, 0}}|HistoryList],
    TotalGold = lists:sum([X || {_, {X, _}} <- HistoryList]),
    {ok, BinData} = pt_159:write(15957, [Type, SubType, TotalGold]),
    lib_server_send:send_to_sid(PS#player_status.sid, BinData);


handle(_Cmd, _PS, _Data) ->
    % ?PRINT("no match :~p~n", [{_Cmd, _Data}]),
    {error, "pp_recharge no match"}.
    

