//--------------------------------------------------------  
 | 
//    [Author]:           第二世界  
 | 
//    [  Date ]:           Monday, September 18, 2017  
 | 
//--------------------------------------------------------  
 | 
  
 | 
using Snxxz.UI;  
 | 
using System;  
 | 
using System.Collections;  
 | 
using System.Collections.Generic;  
 | 
  
 | 
using UnityEngine;  
 | 
using UnityEngine.UI;  
 | 
  
 | 
namespace Snxxz.UI  
 | 
{  
 | 
  
 | 
    public class UnionStoreWin : StoreUIBasic, SecondWindowInterface  
 | 
    {  
 | 
  
 | 
        #region 成员变量  
 | 
        [SerializeField]  
 | 
        private ScrollerController _shopCtrl;  
 | 
        [SerializeField]  
 | 
        private Text _moneyNumText;  
 | 
        [SerializeField]  
 | 
        private Toggle _toolToggle;  
 | 
  
 | 
        [SerializeField] Button getPathBtn;  
 | 
  
 | 
        [SerializeField]  
 | 
        private Toggle _mountStoneToggle;  
 | 
        [SerializeField]  
 | 
        private Toggle _mountSkillBookToggle;  
 | 
        [SerializeField]  
 | 
        private Text m_RefreshTimeText;  
 | 
        #endregion  
 | 
  
 | 
        private int _unionLv;  
 | 
  
 | 
        StoreModel _storeModel;  
 | 
        StoreModel m_storeModel {  
 | 
            get { return _storeModel ?? (_storeModel = ModelCenter.Instance.GetModel<StoreModel>()); }  
 | 
        }  
 | 
  
 | 
        int shopId = 0;  
 | 
        #region Built-in  
 | 
  
 | 
        List<StoreModel.StoreData> displayStoreDatas = new List<StoreModel.StoreData>();  
 | 
  
 | 
        public Button close { get; set; }  
 | 
  
 | 
        protected override void BindController()  
 | 
        {  
 | 
            if (this is SecondWindowInterface)  
 | 
            {  
 | 
                var frame = this.GetComponentInChildren<SecondFrameLoader>();  
 | 
                frame.Create();  
 | 
                close = frame.GetComponentInChildren<Button>();  
 | 
            }  
 | 
        }  
 | 
  
 | 
        protected override void AddListeners()  
 | 
        {  
 | 
            _shopCtrl.OnRefreshCell += RefreshShopLineCell;  
 | 
            _shopCtrl.lockType = EnhanceLockType.KeepVertical;  
 | 
            _toolToggle.onValueChanged.AddListener((bool isOn) => { OnClickToolToggle(isOn); });  
 | 
            _mountStoneToggle.onValueChanged.AddListener((bool isOn) => { OnClickMountStoneToggle(isOn); });  
 | 
            _mountSkillBookToggle.onValueChanged.AddListener((bool isOn) => { OnClickMountSkillBookToggle(isOn); });  
 | 
            close.onClick.AddListener(CloseWin);  
 | 
            getPathBtn.AddListener(ClickGetPathBtn);  
 | 
        }  
 | 
  
 | 
        protected override void OnPreOpen()  
 | 
        {  
 | 
            m_storeModel.storeFuncType = StoreFunc.ToolStore;  
 | 
            PlayerDatas.Instance.playerDataRefreshEvent += OnRefreshPlayerInfo;  
 | 
            PlayerDatas.Instance.fairyData.OnRefreshFairyInfo += RefreshShopDisplay;  
 | 
            m_storeModel.RefreshBuyShopLimitEvent += RefreshShopDisplay;  
 | 
            CreateCell();  
 | 
            RefreshUI();  
 | 
        }  
 | 
  
 | 
        protected override void OnAfterOpen()  
 | 
        {  
 | 
            switch (m_storeModel.storeFuncType)  
 | 
            {  
 | 
                case StoreFunc.ToolStore:  
 | 
                    _toolToggle.isOn = true;  
 | 
                    break;  
 | 
                case StoreFunc.MountStoneStore:  
 | 
                    _mountStoneToggle.isOn = true;  
 | 
                    break;  
 | 
                case StoreFunc.MountSkillBookStore:  
 | 
                    _mountSkillBookToggle.isOn = true;  
 | 
                    break;  
 | 
            }  
 | 
            _shopCtrl.JumpIndex(0);  
 | 
            CheckJumpToModel();  
 | 
        }  
 | 
  
 | 
        protected override void OnPreClose()  
 | 
        {  
 | 
            _storeModel.fairyStoreJumpShopId = 0;  
 | 
  
 | 
            m_storeModel.RefreshBuyShopLimitEvent -= RefreshShopDisplay;  
 | 
            PlayerDatas.Instance.playerDataRefreshEvent -= OnRefreshPlayerInfo;  
 | 
            PlayerDatas.Instance.fairyData.OnRefreshFairyInfo -= RefreshShopDisplay;  
 | 
        }  
 | 
  
 | 
        protected override void OnAfterClose()  
 | 
        {  
 | 
        }  
 | 
        #endregion  
 | 
  
 | 
        private void CheckJumpToModel()  
 | 
        {  
 | 
            if (m_storeModel.jumpToItemId != 0)  
 | 
            {  
 | 
                var p_shopItemlist = m_storeModel.TryGetStoreDatas(m_storeModel.storeFuncType);  
 | 
                for (int i = 0; i < p_shopItemlist.Count; i++)  
 | 
                {  
 | 
                    var storeData = p_shopItemlist[i];  
 | 
                    if (storeData.storeConfig.ItemID == m_storeModel.jumpToItemId)  
 | 
                    {  
 | 
                        shopId = storeData.shopId;  
 | 
                        int index = i / 2;  
 | 
                        _shopCtrl.JumpIndex(index);  
 | 
                        _shopCtrl.m_Scorller.RefreshActiveCellViews();  
 | 
                        break;  
 | 
                    }  
 | 
                }  
 | 
  
 | 
                m_storeModel.ClearJump();  
 | 
            }  
 | 
            else if (_storeModel.fairyStoreJumpShopId != 0)  
 | 
            {  
 | 
                shopId = _storeModel.fairyStoreJumpShopId;  
 | 
                var index = GetShopLineIndex(_storeModel.fairyStoreJumpShopId);  
 | 
                _shopCtrl.JumpIndex(index);  
 | 
                _shopCtrl.m_Scorller.RefreshActiveCellViews();  
 | 
            }  
 | 
        }  
 | 
  
 | 
        int GetShopLineIndex(int _shopId)  
 | 
        {  
 | 
            for (int i = 0; i < displayStoreDatas.Count; i++)  
 | 
            {  
 | 
                if (displayStoreDatas[i].shopId == _shopId)  
 | 
                {  
 | 
                    shopId = _shopId;  
 | 
                    int index = i / 2;  
 | 
                    return index;  
 | 
                }  
 | 
            }  
 | 
            return 0;  
 | 
        }  
 | 
  
 | 
        public int CompareByMoney(StoreModel.StoreData start, StoreModel.StoreData end)  
 | 
        {  
 | 
            int money1 = start.storeConfig.MoneyNumber;  
 | 
            int money2 = end.storeConfig.MoneyNumber;  
 | 
            if (money1.CompareTo(money2) != 0) return money1.CompareTo(money2);  
 | 
            int index1 = displayStoreDatas.IndexOf(start);  
 | 
            int index2 = displayStoreDatas.IndexOf(end);  
 | 
            if (index1.CompareTo(index2) != 0) return index1.CompareTo(index2);  
 | 
            return 0;  
 | 
        }  
 | 
  
 | 
        private void RefreshUI()  
 | 
        {  
 | 
            _moneyNumText.text = ItemLogicUtility.Instance.OnChangeCoinsUnit(UIHelper.GetMoneyCnt(15));  
 | 
        }  
 | 
  
 | 
        private void OnRefreshPlayerInfo(PlayerDataType refreshType)  
 | 
        {  
 | 
            if (refreshType == PlayerDataType.UnionLiven)  
 | 
                RefreshUI();  
 | 
        }  
 | 
  
 | 
        private void RefreshShopDisplay()  
 | 
        {  
 | 
            _shopCtrl.m_Scorller.RefreshActiveCellViews();  
 | 
        }  
 | 
  
 | 
        private void CreateCell()  
 | 
        {  
 | 
            shopId = 0;  
 | 
            _unionLv = 0;  
 | 
  
 | 
            if (PlayerDatas.Instance.fairyData.HasFairy)  
 | 
            {  
 | 
                _unionLv = PlayerDatas.Instance.fairyData.fairy.FamilyLV;  
 | 
            }  
 | 
  
 | 
            displayStoreDatas.Clear();  
 | 
  
 | 
            _shopCtrl.Refresh();  
 | 
            p_shopItemlist = m_storeModel.TryGetStoreDatas(m_storeModel.storeFuncType);  
 | 
  
 | 
            //设置刷新时间  
 | 
            if (p_shopItemlist != null && p_shopItemlist.Count > 0)  
 | 
            {  
 | 
                m_RefreshTimeText.text = m_storeModel.GetStoreRefreshTimeByType(p_shopItemlist[0].storeConfig.RefreshType);  
 | 
            }  
 | 
            else  
 | 
            {  
 | 
                m_RefreshTimeText.text = "";  
 | 
            }  
 | 
  
 | 
  
 | 
            if (p_shopItemlist != null)  
 | 
            {  
 | 
                displayStoreDatas.AddRange(p_shopItemlist);  
 | 
                displayStoreDatas.Sort((x, y) =>  
 | 
                {  
 | 
                    //限购(不刷新)且剩余数量0 > [所需仙盟等级(不足按等级排) > 所需vip等级(不足按等级排)] > 价格  
 | 
                    var canbuycount_x = 0;  
 | 
                    var canbuycount_y = 0;  
 | 
                    var addcount = 0;  
 | 
                    BuyItemController.Instance.CheckIsLimitBuyCnt(x.storeConfig, out canbuycount_x, out addcount);  
 | 
                    BuyItemController.Instance.CheckIsLimitBuyCnt(x.storeConfig, out canbuycount_y, out addcount);  
 | 
                    var limit_x = m_storeModel.GetBuyShopLimit((uint)x.storeConfig.ID);  
 | 
                    var limit_y = m_storeModel.GetBuyShopLimit((uint)y.storeConfig.ID);  
 | 
                    var remain_x = canbuycount_x - (limit_x == null ? 0 : limit_x.BuyCnt);  
 | 
                    var remain_y = canbuycount_y - (limit_y == null ? 0 : limit_y.BuyCnt);  
 | 
                    //不刷新的物品购买数量为0的排后  
 | 
                    var isEmptyX = x.storeConfig.RefreshType == 0 && remain_x == 0;  
 | 
                    var isEmptyY = y.storeConfig.RefreshType == 0 && remain_y == 0;  
 | 
                    if (isEmptyX != isEmptyY)  
 | 
                    {  
 | 
                        return isEmptyX.CompareTo(isEmptyY);  
 | 
                    }  
 | 
                    var fairylevel_x = x.storeConfig.LimitValue;  
 | 
                    var fairylevel_y = y.storeConfig.LimitValue;  
 | 
                    var isSatisfy_x = _unionLv >= fairylevel_x;  
 | 
                    var isSatisfy_y = _unionLv >= fairylevel_y;  
 | 
                    if (isSatisfy_x != isSatisfy_y)  
 | 
                        return isSatisfy_y.CompareTo(isSatisfy_x);  
 | 
  
 | 
                    if (isSatisfy_x == isSatisfy_y && !isSatisfy_x)  
 | 
                    {  
 | 
                        if (fairylevel_x != fairylevel_y)  
 | 
                        {  
 | 
                            return fairylevel_x.CompareTo(fairylevel_y);  
 | 
                        }  
 | 
                    }  
 | 
  
 | 
                    var vip_x = x.storeConfig.VIPLV.Length > 0 ? x.storeConfig.VIPLV[0] : 0;  
 | 
                    var vip_y = y.storeConfig.VIPLV.Length > 0 ? y.storeConfig.VIPLV[0] : 0;  
 | 
                    isSatisfy_x = PlayerDatas.Instance.baseData.VIPLv >= vip_x;  
 | 
                    isSatisfy_y = PlayerDatas.Instance.baseData.VIPLv >= vip_y;  
 | 
  
 | 
                    if (isSatisfy_x != isSatisfy_y)  
 | 
                        return isSatisfy_y.CompareTo(isSatisfy_x);  
 | 
  
 | 
                    if (isSatisfy_x == isSatisfy_y && !isSatisfy_x)  
 | 
                    {  
 | 
                        if (vip_x != vip_y)  
 | 
                        {  
 | 
                            return vip_x.CompareTo(vip_y);  
 | 
                        }  
 | 
                    }  
 | 
  
 | 
                    return x.storeConfig.MoneyNumber.CompareTo(y.storeConfig.MoneyNumber);  
 | 
                });  
 | 
            }  
 | 
  
 | 
            var line = Mathf.CeilToInt(displayStoreDatas.Count / 2f);  
 | 
            for (int i = 0; i < line; i++)  
 | 
            {  
 | 
                _shopCtrl.AddCell(ScrollerDataType.Header, i);  
 | 
            }  
 | 
            _shopCtrl.Restart();  
 | 
        }  
 | 
  
 | 
        private void RefreshShopLineCell(ScrollerDataType type, CellView cell)  
 | 
        {  
 | 
            int childCode = 0;  
 | 
            for (childCode = 0; childCode < cell.transform.childCount; childCode++)  
 | 
            {  
 | 
                int cellCount = (cell.transform.childCount) * cell.index + (childCode + 1);  
 | 
                UnionShopCell shopItemCell = cell.transform.GetChild(childCode).GetComponent<UnionShopCell>();  
 | 
                if (shopItemCell == null)  
 | 
                {  
 | 
                    shopItemCell = cell.transform.GetChild(childCode).gameObject.AddComponent<UnionShopCell>();  
 | 
                }  
 | 
  
 | 
                if (displayStoreDatas.Count >= cellCount)  
 | 
                {  
 | 
                    StoreConfig shopInfo = displayStoreDatas[cellCount - 1].storeConfig;  
 | 
                    cell.transform.GetChild(childCode).SetActive(true);  
 | 
                    if (shopInfo.SalesStatus.Trim() != "")  
 | 
                    {  
 | 
                        shopItemCell.stateImage.SetActive(true);  
 | 
                        shopItemCell.stateImage.SetSprite(shopInfo.SalesStatus);  
 | 
                    }  
 | 
                    else  
 | 
                    {  
 | 
                        shopItemCell.stateImage.SetActive(false);  
 | 
                    }  
 | 
                    ItemConfig chinModel = ItemConfig.Get(m_storeModel.GetReplaceId(shopInfo.ID, shopInfo.ItemID));  
 | 
                    if (chinModel != null)  
 | 
                    {  
 | 
                        ItemCellModel cellModel = new ItemCellModel(chinModel.ID, false, 0);  
 | 
                        shopItemCell.itemCell.enabled = false;  
 | 
                        shopItemCell.itemCell.Init(cellModel);  
 | 
                        shopItemCell.itemCell.button.SetListener(() =>  
 | 
                        {  
 | 
                            ItemTipUtility.Show(chinModel.ID);  
 | 
                        });  
 | 
                        shopItemCell.itemNameText.text = chinModel.ItemName;  
 | 
                        shopItemCell.itemNameText.color = UIHelper.GetUIColor(chinModel.ItemColor, true);  
 | 
                        shopItemCell.coinsIcon.SetIconWithMoneyType(shopInfo.MoneyType);  
 | 
                        ulong shopCost = (ulong)(shopInfo.MoneyNumber);  
 | 
                        shopItemCell.coinNumText.text = UIHelper.ReplaceLargeNum(shopCost);  
 | 
  
 | 
                        shopItemCell.redpoint.redpointId = StoreModel.StoreFunc8_RedKey * 10000 + shopInfo.ID;  
 | 
  
 | 
                        BuyItemController buyItem = BuyItemController.Instance;  
 | 
                        int curVipIndex = -1;  
 | 
                        int nextVipIndex = -1;  
 | 
                        bool isVipBuy = buyItem.CheckIsVipBuy(shopInfo, out curVipIndex, out nextVipIndex);  
 | 
                        int canBuyCnt = 0;  
 | 
                        int addBuyCnt = 0;  
 | 
                        bool isLimitBuy = buyItem.CheckIsLimitBuyCnt(shopInfo, out canBuyCnt, out addBuyCnt);  
 | 
                        bool isShowBuyPrice = true;  
 | 
                        shopItemCell.buyBtn.SetActive(true);  
 | 
                        if (isVipBuy)  
 | 
                        {  
 | 
                            if (curVipIndex == -1 && nextVipIndex != -1)  
 | 
                            {  
 | 
                                shopItemCell.coinsIcon.SetActive(false);  
 | 
                                shopItemCell.vipText.SetActive(true);  
 | 
                                shopItemCell.vipText.text = Language.Get("StoreWin101", shopInfo.VIPLV[nextVipIndex]);  
 | 
                                shopItemCell.sellImage.SetActive(false);  
 | 
                                isShowBuyPrice = false;  
 | 
                            }  
 | 
                        }  
 | 
                        if (isShowBuyPrice)  
 | 
                        {  
 | 
                            shopItemCell.coinsIcon.SetActive(true);  
 | 
                            shopItemCell.vipText.SetActive(false);  
 | 
                            shopItemCell.sellImage.SetActive(false);  
 | 
                            shopItemCell.itemCell.countText.SetActive(false);  
 | 
                            if (canBuyCnt > 0)  
 | 
                            {  
 | 
                                shopItemCell.itemCell.countText.SetActive(true);  
 | 
                                BuyShopItemLimit shopItemLimit = m_storeModel.GetBuyShopLimit((uint)shopInfo.ID);  
 | 
                                int remainNum = canBuyCnt;  
 | 
                                if (shopItemLimit != null)  
 | 
                                {  
 | 
                                    remainNum = canBuyCnt - shopItemLimit.BuyCnt;  
 | 
                                }  
 | 
  
 | 
                                if (remainNum > 0)  
 | 
                                {  
 | 
                                    shopItemCell.itemCell.countText.text = StringUtility.Contact(remainNum.ToString(),  
 | 
                                        "/" + canBuyCnt.ToString());  
 | 
                                    shopItemCell.sellImage.SetActive(false);  
 | 
  
 | 
                                }  
 | 
                                else  
 | 
                                {  
 | 
                                    shopItemCell.itemCell.countText.text = StringUtility.Contact(UIHelper.AppendColor(TextColType.Red, remainNum.ToString()),  
 | 
                                        "/" + canBuyCnt.ToString());  
 | 
                                    shopItemCell.sellImage.SetActive(true);  
 | 
                                    shopItemCell.buyBtn.SetActive(false);  
 | 
                                    shopItemCell.stateImage.SetActive(false);  
 | 
                                }  
 | 
                            }  
 | 
                        }  
 | 
  
 | 
                        if (_unionLv >= shopInfo.LimitValue)  
 | 
                        {  
 | 
                            shopItemCell.moneyObj.SetActive(true);  
 | 
                            shopItemCell.buyNeedText.SetActive(false);  
 | 
                            //shopItemCell.buyBtnIcon.SetSprite("");  
 | 
                            shopItemCell.buyBtn.interactable = true;  
 | 
                            shopItemCell.LoadGuidEffectCtrl(shopInfo.ID, shopId);  
 | 
                        }  
 | 
                        else  
 | 
                        {  
 | 
                            shopItemCell.buyNeedText.SetActive(true);  
 | 
                            shopItemCell.buyNeedText.text = Language.Get("FairyStore101", shopInfo.LimitValue);  
 | 
                            shopItemCell.buyBtn.interactable = false;  
 | 
                            shopItemCell.moneyObj.SetActive(false);  
 | 
                            //shopItemCell.buyBtnIcon.SetSprite("");  
 | 
                        }  
 | 
                    }  
 | 
  
 | 
  
 | 
                    shopItemCell.buyBtn.RemoveAllListeners();  
 | 
                    shopItemCell.buyBtn.AddListener(() =>  
 | 
                    {  
 | 
                        if (shopInfo.ID == shopId)  
 | 
                        {  
 | 
                            shopId = 0;  
 | 
                            shopItemCell.LoadGuidEffectCtrl(shopInfo.ID, shopId);  
 | 
                        }  
 | 
                        m_storeModel.SendBuyShopItem(shopInfo, 1);  
 | 
                        m_storeModel.SetDailyRedRedmind(shopInfo.ID);  
 | 
                    });  
 | 
                    shopItemCell.ShopCellBtn.RemoveAllListeners();  
 | 
                    shopItemCell.ShopCellBtn.AddListener(() =>  
 | 
                    {  
 | 
                        if (shopInfo.ID == shopId)  
 | 
                        {  
 | 
                            shopId = 0;  
 | 
                            shopItemCell.LoadGuidEffectCtrl(shopInfo.ID, shopId);  
 | 
                        }  
 | 
                        m_storeModel.OnClickShopCell(shopInfo);  
 | 
                        m_storeModel.SetDailyRedRedmind(shopInfo.ID);  
 | 
                    });  
 | 
  
 | 
                }  
 | 
                else  
 | 
                {  
 | 
                    cell.transform.GetChild(childCode).SetActive(false);  
 | 
                }  
 | 
            }  
 | 
        }  
 | 
  
 | 
        private void OnClickMountSkillBookToggle(bool isOn)  
 | 
        {  
 | 
            m_storeModel.storeFuncType = StoreFunc.MountSkillBookStore;  
 | 
            CreateCell();  
 | 
            _shopCtrl.JumpIndex(0);  
 | 
        }  
 | 
  
 | 
        private void OnClickToolToggle(bool isOn)  
 | 
        {  
 | 
            m_storeModel.storeFuncType = StoreFunc.ToolStore;  
 | 
            CreateCell();  
 | 
            _shopCtrl.JumpIndex(0);  
 | 
        }  
 | 
  
 | 
        private void OnClickMountStoneToggle(bool isOn)  
 | 
        {  
 | 
            m_storeModel.storeFuncType = StoreFunc.MountStoneStore;  
 | 
            CreateCell();  
 | 
            _shopCtrl.JumpIndex(0);  
 | 
        }  
 | 
        private void ClickGetPathBtn()  
 | 
        {  
 | 
            int itemId = GeneralDefine.moneyDisplayIds[15];  
 | 
            ItemTipUtility.Show(itemId);  
 | 
        }  
 | 
        private void CloseWin()  
 | 
        {  
 | 
            Close();  
 | 
        }  
 | 
  
 | 
    }  
 | 
  
 | 
}  
 | 
  
 | 
  
 | 
  
 | 
  
 |