%%%--------------------------------------
%%% @Module  : lib_shop
%%% @Author  : Yanwf
%%% @Created : 2018-11-27
%%% @Description:  商城检测
%%%--------------------------------------
-module (lib_shop_check).
-include("server.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("shop.hrl").
-include("def_fun.hrl").
-include("def_goods.hrl").
-include("goods.hrl").
-include("def_module.hrl").
-include("vip.hrl").
-export ([
	check_buy_goods/2
    ,get_buyed_time/5
    ,add_buyed_time/6
	]).

%% ------------------- check -------------------------------------
check_buy_goods([], Player) -> {true, Player};
check_buy_goods([Check|List], Player) ->
    case do_check_buy_goods(Check, Player) of
        {true, NewPlayer} -> check_buy_goods(List, NewPlayer);
        Error -> Error
    end.

%% 检查参数
do_check_buy_goods({args, [BuyNum]}, Player) ->
    if
        BuyNum =< 0 -> {false, ?ERRCODE(err153_goods_not_equal), Player};
        true -> {true, Player}
    end;

%% 开服时间
do_check_buy_goods({open_day, OnSale, HaltSale}, Player) ->
    OpTime = util:get_open_day(),
    if
        (OpTime < OnSale) orelse (OpTime > HaltSale andalso HaltSale =/= 0 ) ->
            {false, ?ERRCODE(err153_not_enough_server_time), Player};
        true ->
            {true, Player}
    end;

%% 世界等级
do_check_buy_goods({wlv, WlvSale, WlvUnsale}, Player) ->
    ServerLv = util:get_world_lv(),
    if
        (ServerLv < WlvSale) orelse (ServerLv > WlvUnsale andalso WlvUnsale /= 0) ->
            {false, ?ERRCODE(err153_not_enough_world_lv), Player};
        true ->
            {true, Player}
    end;

%% 购买条件检查
do_check_buy_goods({condition, Condition}, Player)->
    case check_player_condition(Condition, Player) of
        true -> {true, Player};
        Res -> Res
    end;

%% 次数限制
do_check_buy_goods({quota_num, Condition, QuotaType, TmpQuotaNum, CounterModule, CounterId, KeyId, Num}, #player_status{id = RoleId} = Player) ->
    QuotaNum = lib_shop:get_quota_num(Player, Condition, TmpQuotaNum),
    case QuotaNum of
        0 ->
            {true, Player};
        _ ->
            GoodsNum = ?IF(QuotaNum > 0, get_buyed_time(RoleId, KeyId, CounterModule, CounterId, QuotaType), 0),
            case GoodsNum < QuotaNum of
                false -> {false, ?ERRCODE(err153_goods_sold_out), Player};
                true ->
                    case ((GoodsNum + Num) =< QuotaNum) of
                        false -> {false, ?ERRCODE(err153_not_enough_goods_to_buy), Player};
                        true -> {true, Player}
                    end
            end
    end;

%% 背包是否够放
do_check_buy_goods({bag_is_enough, GoodsList}, Player) ->
    case lib_goods_api:can_give_goods(Player, GoodsList) of
        true -> {true, Player};
        _ ->
            ErrCode = get_bag_not_enough_msg(GoodsList),
            {false, ErrCode, Player}
    end;

%% 钱是不是有带够呀
do_check_buy_goods({money_is_enough, CostList}, Player) ->
    NewCostList = lib_goods_api:make_reward_unique(CostList),
    case lib_goods_api:check_object_list(Player, NewCostList) of
        true -> {true, Player};
        _ -> {false, ?ERRCODE(money_not_enough), Player}
    end;

%% 角色是否达到展示等级
do_check_buy_goods({on_lev, OnLev, DownLev}, Player) ->
    #player_status{figure = #figure{lv = Lev}} = Player,
    case OnLev =< Lev andalso Lev =< DownLev of
        true -> {true, Player};
        _ -> {false, ?ERRCODE(err153_not_enough_lv), Player}
    end;

%% 角色是否符合职业要求
do_check_buy_goods({career, Career}, Player) ->
    #player_status{figure = #figure{career = RoleCareer}} = Player,
    case Career =:= RoleCareer orelse Career =:= 0 of
        true -> {true, Player};
        _ -> {false, ?ERRCODE(err153_error_career), Player}
    end;

do_check_buy_goods(_Condition, PS) ->
    {false, ?FAIL, PS}.

%% ------------------------ 检查物品的购买条件 -----------------------------------
check_player_condition([], _Player) -> true;
check_player_condition([{lv, SLv, ELv}|Condition], Player)-> %% 等级区间
    if
        Player#player_status.figure#figure.lv < SLv orelse Player#player_status.figure#figure.lv > ELv ->
        	{false, ?ERRCODE(err153_not_enough_lv), Player};
        true ->
        	check_player_condition(Condition, Player)
    end;
check_player_condition([{lv, Lv}|Condition], Player)-> %% 最小等级限制
    if
        Player#player_status.figure#figure.lv < Lv -> {false, ?ERRCODE(err153_not_enough_lv), Player};
        true -> check_player_condition(Condition, Player)
    end;
check_player_condition([{vip, Vip}|Condition], Player)-> %% vip等级限制
    if
        Player#player_status.figure#figure.vip < Vip -> {false, ?ERRCODE(err159_vip_not_enough), Player};
        true -> check_player_condition(Condition, Player)
    end;
check_player_condition([{vip_type, VipType}|Condition], Player)-> %% vip类型限制
    if
        Player#player_status.figure#figure.vip_type < VipType -> {false, ?ERRCODE(err159_vip_not_enough), Player};
        true -> check_player_condition(Condition, Player)
    end;
check_player_condition([{tier_3v3, Tier}|Condition], Player)->
    MyTier = lib_3v3_api:get_tier(Player),
    if
        MyTier < Tier -> {false, ?ERRCODE(err650_tier_not_enough), Player};
        true -> check_player_condition(Condition, Player)
    end;
check_player_condition([{kf_1vn_turn, Turn}|Condition], Player) -> %% 跨服1vn轮数、会同时检测是否是擂主
    case lib_kf_1vn:check_def_shop(Player, Turn) of
        true -> check_player_condition(Condition, Player);
        false -> {false, ?ERRCODE(err621_turn_not_enough), Player}
    end;
check_player_condition([{is_kf_1vn_def}|Condition], Player) -> %% 跨服1vn检测是否是擂主
    case lib_kf_1vn:check_def_shop(Player) of
        true -> check_player_condition(Condition, Player);
        false -> {false, ?ERRCODE(err621_not_kf_1vn_def), Player}
    end;
check_player_condition([{kf_1vn_area, Area}|Condition], Player) -> %% 跨服1vn赛区检测
    case lib_kf_1vn:check_area(Player, Area) of
        true -> check_player_condition(Condition, Player);
        false -> {false, ?ERRCODE(err621_not_kf_1vn_area), Player}
    end;
check_player_condition([{is_kf_1vn_challenger}|Condition], Player) -> %% 跨服1vn检测是否是挑战者
    case lib_kf_1vn:check_challenger_shop(Player) of
        true -> check_player_condition(Condition, Player);
        false -> {false, ?ERRCODE(err621_not_kf_1vn_challenger), Player}
    end;

% check_player_condition([{diamond_vip_need_buy, KeyList}|Condition], Player) -> %% 至尊vip商城是否满足前置购买条件
%     VipStatus = Player#player_status.vip_status,
%     DiamondVipShopBuyRecord = VipStatus#vip_status.diamond_vip_shop_buy_record,
%     case ulists:all_in_list(KeyList, DiamondVipShopBuyRecord) of
%         true -> check_player_condition(Condition, Player);
%         false -> {false, ?ERRCODE(err450_diamond_vip_need_buy), Player}
%     end;
check_player_condition([_|Condition], Player) ->
    check_player_condition(Condition, Player).

%% 获取已经购买的次数
get_buyed_time(RoleId, _KeyId, CounterModule, CounterId, QuotaType) ->
    if
        QuotaType == ?QUOTA_WEEK ->
            lib_week:get_count(RoleId, CounterModule, CounterId);
        QuotaType == ?QUOTA_DAILY ->
            lib_daily:get_count(RoleId, CounterModule, CounterId);
        true ->
            lib_counter:get_count(RoleId, CounterModule, CounterId)
    end.

%% 添加购买的次数
add_buyed_time(RoleId, _KeyId, CounterModule, CounterId, QuotaType, BuyNum) ->
    if
        QuotaType == ?QUOTA_WEEK ->
            lib_week:plus_count(RoleId, CounterModule, CounterId, BuyNum);
        QuotaType == ?QUOTA_DAILY ->
            lib_daily:plus_count(RoleId, CounterModule, CounterId, BuyNum);
        true ->
            lib_counter:plus_count(RoleId, CounterModule, CounterId, BuyNum)
    end.

%% 获取背包空间不足的错误码
get_bag_not_enough_msg(GoodsTypeId) when is_integer(GoodsTypeId)->
    do_get_bag_not_enough_msg(GoodsTypeId);
get_bag_not_enough_msg([{_Type, GoodsTypeId, _Num, _Bind}|_]) ->
    do_get_bag_not_enough_msg(GoodsTypeId);
get_bag_not_enough_msg(_) -> ?ERRCODE(err153_bag_not_enough).

do_get_bag_not_enough_msg(GoodsTypeId) ->
    case data_goods_type:get(GoodsTypeId) of
        #ets_goods_type{bag_location = BagLocation} ->
            do_get_bag_not_enough_msg_core(BagLocation);
        _ -> ?ERRCODE(err150_no_goods)
    end.
do_get_bag_not_enough_msg_core(BagLocation) ->
    case BagLocation of
        ?GOODS_LOC_BAG ->
            ?ERRCODE(err153_bag_not_enough);
        _ ->
            ?ERRCODE(err153_bag_not_enough)
    end.
