using Snxxz.UI; 
 | 
using System; 
 | 
using System.Collections.Generic; 
 | 
using System.Linq; 
 | 
using System.Text; 
 | 
using UnityEngine; 
 | 
  
 | 
  
 | 
public class BuyItemController : Singleton<BuyItemController> 
 | 
{ 
 | 
  
 | 
    Dictionary<int, int> vipBuyCntDict = new Dictionary<int, int>(); 
 | 
  
 | 
    public bool CheckIsVipBuy(StoreConfig model, out int curVipIndex, out int nextVipIndex) 
 | 
    { 
 | 
        vipBuyCntDict.Clear(); 
 | 
        curVipIndex = -1; 
 | 
        nextVipIndex = -1; 
 | 
        bool isVipBuy = false; 
 | 
        if (model == null) return isVipBuy; 
 | 
  
 | 
        if (model.VIPLV.Length < 2) 
 | 
        { 
 | 
            if (model.VIPLV[0] != 0) 
 | 
            { 
 | 
                isVipBuy = true; 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                isVipBuy = false; 
 | 
            } 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            isVipBuy = true; 
 | 
        } 
 | 
  
 | 
        if (isVipBuy) 
 | 
        { 
 | 
            for (int i = model.VIPLV.Length - 1; i > -1; i--) 
 | 
            { 
 | 
                vipBuyCntDict.Add(model.VIPLV[i], model.PurchaseNumber[i]); 
 | 
            } 
 | 
            int playerVip = PlayerDatas.Instance.baseData.VIPLv; 
 | 
            for (int i = model.VIPLV.Length - 1; i > -1; i--) 
 | 
            { 
 | 
                if (model.VIPLV[i] > playerVip) 
 | 
                { 
 | 
                    nextVipIndex = i; 
 | 
                } 
 | 
                else if (model.VIPLV[i] <= playerVip) 
 | 
                { 
 | 
                    curVipIndex = i; 
 | 
                    break; 
 | 
                } 
 | 
            } 
 | 
  
 | 
        } 
 | 
  
 | 
        return isVipBuy; 
 | 
    } 
 | 
  
 | 
    public bool CheckIsLimitBuyCnt(StoreConfig model, out int canBuyCnt, out int addBuyCnt) 
 | 
    { 
 | 
        canBuyCnt = 0; 
 | 
        addBuyCnt = 0; 
 | 
        if (model == null) return false; 
 | 
  
 | 
        int[] canBuyNums = model.PurchaseNumber; 
 | 
        int curVipIndex = -1; 
 | 
        int nexVipIndex = -1; 
 | 
        bool isVipBuy = CheckIsVipBuy(model, out curVipIndex, out nexVipIndex); 
 | 
        if (isVipBuy) 
 | 
        { 
 | 
            if (curVipIndex != -1) 
 | 
            { 
 | 
                canBuyCnt = canBuyNums[curVipIndex]; 
 | 
            } 
 | 
  
 | 
            if (nexVipIndex != -1) 
 | 
            { 
 | 
                addBuyCnt = canBuyNums[nexVipIndex] - canBuyCnt; 
 | 
            } 
 | 
            return true; 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            if (canBuyNums[0] != 0) 
 | 
            { 
 | 
                canBuyCnt = canBuyNums[0]; 
 | 
                return true; 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                return false; 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public int goodId { get; private set; } 
 | 
    public readonly LogicInt wannaBuyCount = new LogicInt(); 
 | 
  
 | 
    StoreModel model { get { return ModelCenter.Instance.GetModel<StoreModel>(); } } 
 | 
  
 | 
    public void SetGood(int goodId) 
 | 
    { 
 | 
        this.goodId = goodId; 
 | 
        wannaBuyCount.value = 1; 
 | 
    } 
 | 
  
 | 
    public void SetBuyCount(int goodId, int count) 
 | 
    { 
 | 
        var countLimit = GetBuyCountLimit(goodId, PlayerDatas.Instance.baseData.VIPLv); 
 | 
        if (countLimit == -1) 
 | 
        { 
 | 
            wannaBuyCount.value = Mathf.Clamp(count, 1, 999); 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            wannaBuyCount.value = Mathf.Clamp(count, 1, countLimit); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public bool IsVipLimitGood(int goodId) 
 | 
    { 
 | 
        var config = StoreConfig.Get(goodId); 
 | 
        return config != null && config.VIPLV.Length > 1 && config.VIPLV[0] > 0; 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// 返回值为-1,表示没有vip限制,返回值为999表示已经没有下一个vip限制 
 | 
    /// </summary> 
 | 
    /// <param name="goodId"></param> 
 | 
    /// <param name="vipLevel"></param> 
 | 
    /// <returns></returns> 
 | 
    public int GetNextCanbuyVipLevel(int goodId, int vipLevel) 
 | 
    { 
 | 
        if (!IsVipLimitGood(goodId)) 
 | 
        { 
 | 
            return -1; 
 | 
        } 
 | 
  
 | 
        var config = StoreConfig.Get(goodId); 
 | 
        var nextLevel = 999; 
 | 
        for (int i = 0; i < config.VIPLV.Length; i++) 
 | 
        { 
 | 
            if (config.VIPLV[i] > vipLevel) 
 | 
            { 
 | 
                nextLevel = config.VIPLV[i]; 
 | 
                break; 
 | 
            } 
 | 
        } 
 | 
  
 | 
        return nextLevel; 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// 返回值如果是-1,表示无限制 
 | 
    /// </summary> 
 | 
    /// <param name="goodId"></param> 
 | 
    /// <returns></returns> 
 | 
    public int GetBuyCountLimit(int goodId, int vipLevel) 
 | 
    { 
 | 
        var config = StoreConfig.Get(goodId); 
 | 
        if (config.PurchaseNumber.Length == 0 || config.PurchaseNumber[0] == 0) 
 | 
        { 
 | 
            return -1; 
 | 
        } 
 | 
  
 | 
        var isVipLimitGood = config != null && config.VIPLV.Length > 1; 
 | 
        var canBuy = 0; 
 | 
        if (isVipLimitGood) 
 | 
        { 
 | 
            var index = -1; 
 | 
            for (var i = config.VIPLV.Length - 1; i >= 0; i--) 
 | 
            { 
 | 
                if (config.VIPLV[i] <= vipLevel) 
 | 
                { 
 | 
                    index = i; 
 | 
                    break; 
 | 
                } 
 | 
            } 
 | 
            
 | 
            if (index != -1) 
 | 
            { 
 | 
                canBuy = config.PurchaseNumber[index]; 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                canBuy = 0; 
 | 
            } 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            canBuy = config.PurchaseNumber[0]; 
 | 
        } 
 | 
  
 | 
        var goodLimit = model.GetBuyShopLimit((uint)goodId); 
 | 
        var haveBuy = goodLimit != null ? goodLimit.BuyCnt : 0; 
 | 
        return canBuy - haveBuy; 
 | 
    } 
 | 
  
 | 
    public bool IsSellOut(int goodId, int vipLevel) 
 | 
    { 
 | 
        var limitCount = GetBuyCountLimit(goodId, vipLevel); 
 | 
        return limitCount != -1 && limitCount <= 0; 
 | 
    } 
 | 
  
 | 
    public int GetTotalPrice(int goodId, int count) 
 | 
    { 
 | 
        var config = StoreConfig.Get(goodId); 
 | 
        return config.MoneyNumber * count; 
 | 
    } 
 | 
  
 | 
  
 | 
} 
 |