using System;  
 | 
using System.Collections.Generic;  
 | 
  
 | 
  
 | 
//充值自选功能  
 | 
public class CustomizedRechargeModel : GameSystemManager<CustomizedRechargeModel>  
 | 
{  
 | 
    public const int MaxGridCount = 4;      //最多4个可选格子  
 | 
  
 | 
    //用户选择面板  
 | 
    public int chooseWinIndex = 0;              //用户选择面板 当前选中的格子(自选位索引)  
 | 
    public int chooseCTGID = 0;                 //用户选择面板 当前选中的礼包id  
 | 
    public Dictionary<int, int> chooseIndexDict = new Dictionary<int, int>();  //用户在选择面板中已选中物品栏中的索引  
 | 
  
 | 
    public Action UpdateWin;  
 | 
  
 | 
    public override void Init()  
 | 
    {  
 | 
    }  
 | 
  
 | 
  
 | 
    //自选礼包 物品信息  
 | 
    //count为固定的数量,sumCount包含自选格子总数量不管是否已选  
 | 
    public bool TryGetRechargeItemEx(int ctgId, out List<Item> items, out int count, out int sumCount)  
 | 
    {  
 | 
        count = 0;  
 | 
        sumCount = 0;  
 | 
        items = new List<Item>();  
 | 
        List<Item> awards = new List<Item>();  
 | 
        if (!RechargeManager.Instance.TryGetRechargeItem(ctgId, out awards))  
 | 
            return false;  
 | 
  
 | 
        RechargeCount rechargeCount;  
 | 
        if (!RechargeManager.Instance.TryGetRechargeCount(ctgId, out rechargeCount))  
 | 
        {  
 | 
            return false;  
 | 
        }  
 | 
  
 | 
        count = awards.Count;  
 | 
        for (int i = 0; i < awards.Count; i++)  
 | 
        {  
 | 
            items.Add(awards[i]);  
 | 
        }  
 | 
        var selectItemInfo = CTGConfig.Get(ctgId).SelectItemInfo;  
 | 
        if (selectItemInfo != null && selectItemInfo.Length != 0)  
 | 
        {  
 | 
            sumCount = selectItemInfo.Length + items.Count;  
 | 
  
 | 
            var selectedItemIndexs = GetSelectedItems(rechargeCount.selectItemValue);  
 | 
            //自选  
 | 
            for (int j = 0; j < selectedItemIndexs.Count; j++)  
 | 
            {  
 | 
                int selectID = selectItemInfo[j][selectedItemIndexs[j] - 1];  
 | 
  
 | 
                items.Add(new Item(CTGSelectItemConfig.Get(selectID).ItemID, CTGSelectItemConfig.Get(selectID).ItemCount));  
 | 
            }  
 | 
        }  
 | 
        else  
 | 
        {  
 | 
            sumCount = count;  
 | 
        }  
 | 
  
 | 
        return true;  
 | 
    }  
 | 
  
 | 
    //约定最多4个可选, 只有全选和未选两种状态  
 | 
    public List<int> GetSelectedItems(int SelectItemValue)  
 | 
    {  
 | 
        if (SelectItemValue <= 0)  
 | 
            return new List<int>();  
 | 
        var list = new List<int>();  
 | 
  
 | 
        for (int i = 0; i < MaxGridCount; i++)  
 | 
        {  
 | 
            int itemIndex = (int)(SelectItemValue % 100);  
 | 
            if (itemIndex == 0)  
 | 
                break;  
 | 
            SelectItemValue /= 100;  
 | 
            list.Add(itemIndex);  
 | 
        }  
 | 
  
 | 
        return list;  
 | 
    }  
 | 
  
 | 
    public uint GetSelectedItemValue()  
 | 
    {  
 | 
        uint SelectItemValue = 0;  
 | 
  
 | 
        for (int i = 0; i < MaxGridCount; i++)  
 | 
        {  
 | 
            if (chooseIndexDict.ContainsKey(i))  
 | 
            {  
 | 
                SelectItemValue += (uint)(chooseIndexDict[i] * Math.Pow(100, i));  
 | 
            }  
 | 
        }  
 | 
  
 | 
        return SelectItemValue;  
 | 
    }  
 | 
  
 | 
  
 | 
  
 | 
    public void InitChoose()  
 | 
    {  
 | 
        RechargeCount rechargeCount;  
 | 
        RechargeManager.Instance.TryGetRechargeCount(chooseCTGID, out rechargeCount);  
 | 
  
 | 
        var selectedItemIndexs = CustomizedRechargeModel.Instance.GetSelectedItems(rechargeCount.selectItemValue);  
 | 
  
 | 
        if (selectedItemIndexs.Count == 0)  
 | 
        {  
 | 
            chooseIndexDict.Clear();  
 | 
            return;  
 | 
        }  
 | 
  
 | 
        for (int i = 0; i < selectedItemIndexs.Count; i++)  
 | 
        {  
 | 
            chooseIndexDict[i] = selectedItemIndexs[i];  
 | 
        }  
 | 
    }  
 | 
  
 | 
    //0 未选中,从1开始  
 | 
    public int GetChooseSubIndex(int index)  
 | 
    {  
 | 
        if (chooseIndexDict.ContainsKey(index))  
 | 
            return chooseIndexDict[index];  
 | 
        return 0;  
 | 
    }  
 | 
  
 | 
    public void ShowUIItems(List<CustomizedItemCell> itemCells, int ctgID)  
 | 
    {  
 | 
        int goodsCount; //固定商品种类数量  
 | 
        int goodsSumCount; //商品总数量  
 | 
        List<Item> awards = new List<Item>();  
 | 
        TryGetRechargeItemEx(ctgID, out awards, out goodsCount, out goodsSumCount);  
 | 
        RechargeCount rechargeCount;  
 | 
        RechargeManager.Instance.TryGetRechargeCount(ctgID, out rechargeCount);  
 | 
        for (int i = 0; i < itemCells.Count; i++)  
 | 
        {  
 | 
            if (i < goodsSumCount)  
 | 
            {  
 | 
                itemCells[i].SetActive(true);  
 | 
                int index = i;  
 | 
                if (i < awards.Count)  
 | 
                {  
 | 
                    var award = awards[i];  
 | 
                    var itemData = new ItemCellModel(award.id, false, award.countEx);  
 | 
                    itemCells[i].Init(itemData);  
 | 
                    itemCells[i].button.SetListener(() =>  
 | 
                    {  
 | 
                        if (index < goodsCount)  
 | 
                            ItemTipUtility.Show(award.id);  
 | 
                        else  
 | 
                        {  
 | 
                            //可自选  
 | 
                            chooseWinIndex = index - goodsCount;  
 | 
                            chooseCTGID = ctgID;  
 | 
                            UIManager.Instance.OpenWindow<CustomizedGiftChooseWin>();  
 | 
                        }  
 | 
  
 | 
                    });  
 | 
                }  
 | 
                else  
 | 
                {  
 | 
                    itemCells[i].Init(null);  
 | 
                    itemCells[i].button.SetListener(() =>  
 | 
                    {  
 | 
                        chooseWinIndex = index - goodsCount;  
 | 
                        chooseCTGID = ctgID;  
 | 
                        UIManager.Instance.OpenWindow<CustomizedGiftChooseWin>();  
 | 
                    });  
 | 
                }  
 | 
            }  
 | 
            else  
 | 
            {  
 | 
                itemCells[i].SetActive(false);  
 | 
            }  
 | 
        }  
 | 
    }  
 | 
}  
 |