%%-----------------------------------------------------------------------------
%% @Module  :       lib_goods_exchange
%% @Author  :       Czc
%% @Email   :       389853407@qq.com
%% @Created :       2017-12-18
%% @Description:    兑换
%%-----------------------------------------------------------------------------
-module(lib_goods_exchange).

-include("server.hrl").
-include("goods.hrl").
-include("def_vip.hrl").
-include("errcode.hrl").
-include("def_module.hrl").
-include("def_goods.hrl").
-include("figure.hrl").
-include("gift.hrl").
-export([
    exchange/3,
    get_exchange_info/1,
    get_free_exchange_time/1
    ]).

-define(EXCHANGE_GOLD_TO_COIN,  1).     %% 元宝兑换交易卷
-define(EXCHANGE_GOODS_TO_COIN, 2).     %% 物品兑换交易券

-define(EXCHANGE_GOODS_ID, 101014).     %% 兑换的礼包id

-define(DEF_EXCHANGE_COST_GOLD,  40). %% 一次消耗的元宝数量
-define(DEF_EXCHANGE_GET_COIN,  600). %% 一次兑换得到的交易券数量

-define(DEF_EXCHANGE_FREE_TIME, 86400*2). %% 每两天免费一次

%% 先使用免费次数
exchange(Player, GoldExchangeTimes, GoodsExchangeTimes) ->
    exchange_normal(Player, GoldExchangeTimes, GoodsExchangeTimes).

exchange_normal(Player, GoldExchangeTimes, 0) ->
    exchange_do(Player, ?EXCHANGE_GOLD_TO_COIN, GoldExchangeTimes);
exchange_normal(Player, 0, GoodsExchangeTimes) ->
    exchange_do(Player, ?EXCHANGE_GOODS_TO_COIN, GoodsExchangeTimes);
exchange_normal(Player, GoldExchangeTimes, GoodsExchangeTimes) ->
    case exchange_do(Player, ?EXCHANGE_GOLD_TO_COIN, GoldExchangeTimes) of
        {false, Err} -> {false, Err};
        {ok, NewPlayer} ->
            exchange_do(NewPlayer, ?EXCHANGE_GOODS_TO_COIN, GoodsExchangeTimes)
    end.

exchange_do(Player, ?EXCHANGE_GOLD_TO_COIN, ExchangeTimes) ->
    DailyCount = get_exchange_info(Player),
    Limit = lib_vip_api:get_vip_privilege(Player, ?VIP_COIN_EXCHANGE, 1),
    Now = utime:unixtime(),
    LastTime = lib_counter:get_count(Player#player_status.id, ?MOD_GOODS, ?GOODS_COUNTER_LIFE_GOLD_TO_COIN_TIME),
    IsFree = Now - LastTime > ?DEF_EXCHANGE_FREE_TIME,
    if
        (not IsFree) andalso (DailyCount + ExchangeTimes > Limit) -> {false, ?ERRCODE(err150_exchange_counts_limit)};
        IsFree andalso (DailyCount + ExchangeTimes - 1 > Limit) -> {false, ?ERRCODE(err150_exchange_counts_limit)}; %% 有免费次数的时候，-1判断
        true ->
            case IsFree of
                true  when ExchangeTimes =:= 1 ->
                    CostCount = 0,
                    Cost = [];
                true ->
                    CostCount = ExchangeTimes - 1,
                    Cost = [{?TYPE_GOLD, 0, ?DEF_EXCHANGE_COST_GOLD * CostCount}];
                false ->
                    CostCount = ExchangeTimes,
                    Cost = [{?TYPE_GOLD, 0, ?DEF_EXCHANGE_COST_GOLD * CostCount}]
            end,
            Get  = [{?TYPE_COIN, 0, ?DEF_EXCHANGE_GET_COIN*ExchangeTimes}],
            %% 宠物生活技能，概率暴击双倍。
            StrikeRatio = lib_pet_api:pet_life_skill_about_gold_exchange_double(Player),
            case urand:rand(1, 100) =< StrikeRatio of
                true ->
                    Get1 = [{?TYPE_COIN, 0, ?DEF_EXCHANGE_GET_COIN*ExchangeTimes*2}],
                    Strike = 1;
                _ ->
                    Get1 = Get,
                    Strike = 0
            end,
            AfterCost =
            case Cost =:= [] of
                true  ->
                    {true, Player};
                false ->
                    case lib_goods_api:cost_object_list_with_check(Player, Cost, goods_exchange, "") of
                        {true, NewPlayer, _} ->
                            {true, NewPlayer};
                        {false, Err, _} ->
                            {false, Err}
                    end
            end,
            case AfterCost of
                {true, Player1} ->
                    IsFree andalso lib_counter:set_count(Player#player_status.id, ?MOD_GOODS, ?GOODS_COUNTER_LIFE_GOLD_TO_COIN_TIME, Now),
                    CostCount > 0 andalso
                    lib_daily:set_count(Player#player_status.id, ?MOD_GOODS, ?GOODS_COUNTER_DAILY_GOLD_TO_COIN, CostCount + DailyCount),
                    LastPlayerStatus = lib_goods_api:send_reward(Player1, Get1, goods_exchange),
                    {ok, LastPlayerStatus, Strike};
                Other ->
                    Other
            end
    end;
exchange_do(Player, ?EXCHANGE_GOODS_TO_COIN, ExchangeTimes) ->
    #player_status{figure = #figure{lv = Lv}} = Player,
    case data_gift:get_gift_reward(?EXCHANGE_GOODS_ID, Lv, 0, 0, 0) of
        #ets_gift_reward{fixed_gifts = Award} ->
            Cost = [{?TYPE_GOODS, ?EXCHANGE_GOODS_ID, ExchangeTimes}],
            Get  = lib_goods_util:goods_object_multiply_by(Award, ExchangeTimes),
            case lib_goods_api:cost_object_list_with_check(Player, Cost, goods_exchange, "") of
                {true, NewPlayer, _} ->
                    LastPlayerStatus = lib_goods_api:send_reward(NewPlayer, Get, goods_exchange),
                    %% 宠物技能，礼包兑换没有暴击
                    Strike = 0,
                    {ok, LastPlayerStatus, Strike};
                {false, Err, _} ->
                    {false, Err}
            end;
        _ ->
            {false, ?MISSING_CONFIG}
    end;
exchange_do(_, _, _) -> {false, 0}.

get_exchange_info(Player) ->
    lib_daily:get_count(Player#player_status.id, ?MOD_GOODS, ?GOODS_COUNTER_DAILY_GOLD_TO_COIN).

get_free_exchange_time(Player) ->
    lib_counter:get_count(Player#player_status.id, ?MOD_GOODS, ?GOODS_COUNTER_LIFE_GOLD_TO_COIN_TIME)
    + ?DEF_EXCHANGE_FREE_TIME.
