%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     福利大厅
%%% @end
%%%-------------------------------------------------------------------
-module(pp_welfare).

-include("def_module.hrl").
-include("welfare.hrl").
-include("server.hrl").
-include("figure.hrl").

-export([handle/3]).

%% 福利大厅-签到状态
handle(Cmd = 16001, Player, []) ->
    #player_status{id = RoleId, welfare = Welfare, sid = Sid} = Player,
    #status_welfare{sign_in_round = Round, sign_in_days = Days, cumulative_sign_in_info = CumulativeInfo} = Welfare,
    Status = lib_daily:get_count(RoleId, ?MOD_WELFARE, ?COUNTER_WELFARE_SIGN_IN),
    {ok, Bin} = pt_160:write(Cmd, [Round, Days, Status, CumulativeInfo]),
    lib_server_send:send_to_sid(Sid, Bin),
    ok;

%% 福利大厅-签到
handle(Cmd = 16002, Player, []) ->
    #player_status{sid = Sid} = Player,
    case welfare_api:welfare_sign_in(Player) of
        {false, Err} ->
            {ok, Bin} = pt_160:write(Cmd, [Err]),
            lib_server_send:send_to_sid(Sid, Bin),
            ok;
        {ok, NewPlayer} ->
            {ok, Bin} = pt_160:write(Cmd, [1]),
            lib_server_send:send_to_sid(Sid, Bin),
            handle(16001, NewPlayer, []),
            {ok, NewPlayer}
    end;

%% 福利大厅-在线奖励状态
handle(Cmd = 16003, Player, []) ->
    #player_status{id = RoleId, welfare = Welfare, sid = Sid} = Player,
    #status_welfare{online_type = OnlineType} = Welfare,
    OnlineTime = welfare_api:get_now_online_time(Player),
    AwardState = lib_daily:get_count(RoleId, ?MOD_WELFARE, ?COUNTER_WELFARE_ONLINE_AWARD),
    {ok, Bin} = pt_160:write(Cmd, [OnlineTime, OnlineType, AwardState]),
    lib_server_send:send_to_sid(Sid, Bin),
    ok;

%% 福利大厅-在线奖励领取
handle(Cmd = 16004, Player, [No]) ->
    #player_status{sid = Sid} = Player,
    case welfare_api:get_welfare_online_award(Player, No) of
        {false, Err} ->
            {ok, Bin} = pt_160:write(Cmd, [Err]),
            lib_server_send:send_to_sid(Sid, Bin),
            ok;
        {ok, NewPlayer} ->
            {ok, Bin} = pt_160:write(Cmd, [1]),
            lib_server_send:send_to_sid(Sid, Bin),
            handle(16003, NewPlayer, []),
            {ok, NewPlayer}
    end;

%% 福利大厅-冲级奖励状态
handle(Cmd = 16005, Player, []) ->
    #player_status{
        welfare = Welfare,
        sid = Sid,
        figure = #figure{career = Career}
    } = Player,
    #status_welfare{level_award_state = AwardState} = Welfare,
    %% 获得有限制的奖励id
    Types = lists:foldl(
        fun(Id, Temp) ->
            case data_welfare:get_welfare_level_award(Career, Id) of
                #base_welfare_level_award{limit_num = LimitNum} when LimitNum > 0 ->
                    [{?MOD_WELFARE, ?COUNTER_GLOBAL_LEVEL_AWARD, Id}|Temp];
                _ ->
                    Temp
            end
        end, [], data_welfare:get_all_limit_level_award_id(Career)
    ),
    %% 转化成奖励id和数量的形式
    LimitState = [
        {Type, Num}
        || {{_, _, Type}, Num} <- mod_global_counter:get_count(Types, []), Num > 0
    ],
    {ok, Bin} = pt_160:write(Cmd, [AwardState, LimitState]),
    lib_server_send:send_to_sid(Sid, Bin),
    ok;

%% 福利大厅-领取冲级奖励
handle(Cmd = 16006, Player, [No]) ->
    #player_status{sid = Sid} = Player,
    case welfare_api:get_welfare_level_award(Player, No) of
        {false, Err} ->
            {ok, Bin} = pt_160:write(Cmd, [Err, No]),
            lib_server_send:send_to_sid(Sid, Bin),
            ok;
        {ok, NewPlayer} ->
            {ok, Bin} = pt_160:write(Cmd, [1, No]),
            lib_server_send:send_to_sid(Sid, Bin),
            handle(16005, NewPlayer, []),
            {ok, NewPlayer}
    end;

%% 福利大厅-资源下载奖励状态
handle(Cmd = 16007, Player, []) ->
    #player_status{welfare = Welfare, sid = Sid} = Player,
    #status_welfare{download_award_state = State} = Welfare,
    {ok, Bin} = pt_160:write(Cmd, [State]),
    lib_server_send:send_to_sid(Sid, Bin),
    ok;

%% 福利大厅-领取资源下载奖励奖励
handle(Cmd = 16008, Player, []) ->
    #player_status{sid = Sid} = Player,
    case welfare_api:get_welfare_download_award(Player) of
        {false, Err} ->
            {ok, Bin} = pt_160:write(Cmd, [Err]),
            lib_server_send:send_to_sid(Sid, Bin),
            ok;
        {ok, NewPlayer} ->
            {ok, Bin} = pt_160:write(Cmd, [1]),
            lib_server_send:send_to_sid(Sid, Bin),
            handle(16007, NewPlayer, []),
            {ok, NewPlayer}
    end;

%% 福利大厅-领取激活码礼包
handle(Cmd = 16009, PS, [CardNo, GoodsId]) ->
    try
        CardNo1 = re:replace(CardNo, "\"", "", [{return, list}, global]),
        CardNo2 = re:replace(CardNo1, "\'", "", [{return, list}, global]),
        case lib_gift_card:trigger_card(PS, CardNo2, GoodsId) of
            {false, Code} ->
                ta_agent_fire:gift_card(PS, [CardNo, "", 0, false, Code]),
                {ok, BinData} = pt_160:write(Cmd, [Code]),
                lib_server_send:send_to_sid(PS#player_status.sid, BinData);
            _ ->
                ok
        end
    catch
        _:Error ->
            erlang:display({?MODULE, ?LINE, Error}),
            ok
    end;

%% 福利大厅-领取累计签到奖励
handle(Cmd = 16010, Player, [No]) ->
    #player_status{sid = Sid} = Player,
    case welfare_api:get_cumulative_sign_in_award(Player, No) of
        {false, Err} ->
            {ok, Bin} = pt_160:write(Cmd, [Err]),
            lib_server_send:send_to_sid(Sid, Bin),
            ok;
        {ok, NewPlayer} ->
            {ok, Bin} = pt_160:write(Cmd, [1]),
            lib_server_send:send_to_sid(Sid, Bin),
            handle(16001, NewPlayer, []),
            {ok, NewPlayer}
    end;

%% 新手引导领奖查询 mod_global_counter:get_count
handle(Cmd = 16011, Player, []) ->
    Info = welfare_api:get_newbee_info(Player),
    {ok, Bin} = pt_160:write(Cmd, Info),
    lib_server_send:send_to_sid(Player#player_status.sid, Bin),
    ok;

handle(_Code, _Player, _Arg) ->
    erlang:display({?MODULE, ?LINE, _Code, _Arg}),
    ok.

