%%%--------------------------------------
%%% @Module  : pp_sup_vip
%%% @Author  : hh
%%% @Created : 2020-04-15
%%% @Description: 贵族协议映射
%%%--------------------------------------
-module(pp_sup_vip).
-include("server.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("vip.hrl").
-include("predefine.hrl").
-include("common.hrl").
-include("def_module.hrl").
-include("def_daily.hrl").
-include("def_vip.hrl").
-include("def_fun.hrl").
-include("def_goods.hrl").

-export([handle/3]).

handle(Cmd, Player, Data) ->
    #player_status{figure = #figure{lv = RoleLv}} = Player,
    OpenLv = lib_module:get_open_lv(?MOD_SUP_VIP),
    if RoleLv >= OpenLv ->
            do_handle(Cmd, Player, Data);
        true ->
            skip
    end.

%% 贵族信息
do_handle(45101, Player, _) ->
    #player_status{id = RoleId, sid = Sid, vip_status = VipStatus} = Player,
    #vip_status{sup_vip = SupVip} = VipStatus,
    #role_sup_vip{type = SupVipType, end_time = EndTime, last_time = LastTime, today_charge = TodayCharge, total_charge = TotalCharge, continue_charge_days = ContinueDay} = SupVip,
    GiftCount = lib_daily:get_count(RoleId, ?MOD_SUP_VIP, ?SUP_VIP_DAILY_EXCHANGE, ?SUP_VIP_GIFT_EXCHANGE),
    case utime:is_today(LastTime) of
        true -> Recharge = TodayCharge;
        _    -> Recharge = 0
    end,
    % NowTime = utime:unixtime(),
    % DiffDay = utime:day_span(LastTime, NowTime),
    % ContinueDay1 = ?IF(DiffDay == 1 orelse DiffDay == 2, ContinueDay, 0),
    Day = lib_sup_vip:get_day(EndTime),
    lib_server_send:send_to_sid(Sid, pt_451, 45101, [SupVipType, EndTime, Recharge, TotalCharge, ContinueDay, Day, GiftCount]),
    ok;

%% 激活贵族
%% 0体验
do_handle(45102, Player, [Type]) ->
    #player_status{id = RoleId, sid = Sid, vip_status = VipStatus} = Player,
    case lib_vip_util:check_active_sup_vip(RoleId, Type, VipStatus) of
        {true, Cost, CostType} ->
            case lib_goods_api:cost_object_list_with_check(Player, Cost, CostType, "") of
                {true, NewPlayer, _} ->
                    % if Type == 0 ->
                    NewPlayer1 = lib_sup_vip:active_trial_sup_vip(NewPlayer),
                    %     true ->
                    %         NewPlayer1 = lib_sup_vip:active_sup_vip(NewPlayer)
                    % end,
                    lib_server_send:send_to_sid(Sid, pt_451, 45102, [?SUCCESS, Type]),
                    {ok, NewPlayer1};
                {false, Errcode, _} ->
                    lib_server_send:send_to_sid(Sid, pt_451, 45102, [Errcode, Type]),
                    ok
            end;
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_451, 45102, [Errcode, Type]),
            ok
    end;

%% 贵族兑换
do_handle(45103, Player, [Type, GoodsId, ExchangeNum]) ->
    #player_status{id = RoleId, sid = Sid} = Player,
    case lib_vip_util:check_sup_vip_exchange(Type, GoodsId, ExchangeNum, Player) of
        {true, Cost, Rewards, SpiltGoods, OldCount} ->
            if Type == ?SUP_VIP_GIFT_EXCHANGE -> %% 礼包领取
                    NewPlayer = lib_goods_api:send_reward(Player, Rewards, sup_vip_daily_gift),
                    lib_daily:increment(RoleId, ?MOD_SUP_VIP, ?SUP_VIP_DAILY_EXCHANGE, Type),
                    lib_server_send:send_to_sid(Sid, pt_451, 45103, [?SUCCESS, Type]),
                    {ok, NewPlayer};
                Type == ?SUP_VIP_GOLD_EXCHANGE -> %% 彩钻兑换
                    [{?TYPE_BGOLD, 0, CostNum}|_] = Cost,
                    if Player#player_status.bgold >= CostNum ->
                            NewPlayer = lib_sup_vip:bgold_to_gold(Player, Cost, Rewards, OldCount, ExchangeNum),
                            {ok, NewPlayer};
                        true ->
                            lib_server_send:send_to_sid(Sid, pt_451, 45103, [?BGOLD_NOT_ENOUGH, Type])
                    end;
                Type == ?SUP_VIP_FOSTER_GOODS_EXCHANGE -> %% 培养丹兑换
                    lib_sup_vip:expire_goods_exchange(Type, Cost, Rewards, SpiltGoods, OldCount, ExchangeNum, Player);
                true ->
                    lib_server_send:send_to_sid(Sid, pt_451, 45103, [?FAIL, Type])
            end;
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_451, 45103, [Errcode, Type])
    end;

%% 贵族商城信息
do_handle(45104, Player, _) ->
    #player_status{id = RoleId, vip_status = VipStatus} = Player,
    #vip_status{sup_vip = RoleSupVip} = VipStatus,
    #role_sup_vip{unlock_ids = UnlockIds} = RoleSupVip,
    lib_sup_vip:get_store_info(RoleId, UnlockIds, 0),
    ok;

%% 贵族商城购买
do_handle(45105, Player, [GoodsId, GoodsNum]) ->
    lib_sup_vip:sup_vip_buy(GoodsId, GoodsNum, Player);

%% 星钻信息
do_handle(45106, Player, _) ->
    lib_sup_vip:get_sup_vip_star(Player);

%% 贵族兑换信息
do_handle(45107, Player, [Type]) ->
    lib_sup_vip:exchange_info(Type, Player);

%% 每日礼包七天登录
do_handle(45109, Player, []) ->
    lib_sup_vip:login_rewards_status(Player);

%% 每日礼包 1首次领|2再次领
do_handle(45110, Player, [Day, Type]) ->
    lib_sup_vip:get_login_rewards(Player, Day, Type);

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