| //--------------------------------------------------------  | 
| //    [Author]:           第二世界  | 
| //    [  Date ]:           Thursday, December 13, 2018  | 
| //--------------------------------------------------------  | 
|   | 
| using System;  | 
| using System.Collections;  | 
| using System.Collections.Generic;  | 
| using UnityEngine;  | 
| using UnityEngine.UI;  | 
| using System.Linq;  | 
|   | 
| namespace Snxxz.UI  | 
| {  | 
|     [XLua.Hotfix]  | 
|     public class GatherSoulResolveWin : Window  | 
|     {  | 
|         [SerializeField] int m_LineCount = 6;  | 
|   | 
|         [SerializeField] ScrollerController m_ScrollerControl;  | 
|         [SerializeField] Toggle m_AutoResolveToggle;  | 
|         [SerializeField] Button m_OpenQualitySort;  | 
|         [SerializeField] Image m_Arrow;  | 
|         [SerializeField] ClickScreenOtherSpace m_ClickSpace;  | 
|         [SerializeField] SortTable m_QualitySort;  | 
|         [SerializeField] Text m_Quality;  | 
|         [SerializeField] Text m_SoulDust;  | 
|         [SerializeField] Button m_Resolve;  | 
|         [SerializeField] Button m_Close;  | 
|   | 
|         GatheringSoulModel model  | 
|         {  | 
|             get { return ModelCenter.Instance.GetModel<GatheringSoulModel>(); }  | 
|         }  | 
|   | 
|         VirtualPackModel virtualPack  | 
|         {  | 
|             get { return ModelCenter.Instance.GetModel<VirtualPackModel>(); }  | 
|         }  | 
|   | 
|         int selectQuality = 2;  | 
|   | 
|         bool qualitySortOpen = false;  | 
|   | 
|         #region Built-in  | 
|         protected override void BindController()  | 
|         {  | 
|         }  | 
|   | 
|         protected override void AddListeners()  | 
|         {  | 
|             m_Close.onClick.AddListener(CloseClick);  | 
|             m_Resolve.onClick.AddListener(Resolve);  | 
|             m_OpenQualitySort.onClick.AddListener(OpenQualitySort);  | 
|             m_ScrollerControl.OnRefreshCell += OnRefreshCell;  | 
|             m_ScrollerControl.lockType = EnhanceLockType.KeepVertical;  | 
|         }  | 
|   | 
|         protected override void OnPreOpen()  | 
|         {  | 
|             m_AutoResolveToggle.isOn = model.autoResolve;  | 
|             model.resolveItems.Clear();  | 
|             m_QualitySort.gameObject.SetActive(false);  | 
|             qualitySortOpen = false;  | 
|             m_Arrow.transform.localEulerAngles = new Vector3(0, 0, 180);  | 
|             selectQuality = GetDefaultQuality();  | 
|             m_QualitySort.SetDefault();  | 
|             m_Quality.text = Language.Get(StringUtility.Contact("GatherSoulResolve_", selectQuality));  | 
|             Display();  | 
|             PlayerDatas.Instance.playerDataRefreshEvent += PlayerDataRefreshInfoEvent;  | 
|             model.gatherSoulPackRefresh += GatherSoulPackRefresh;  | 
|             m_QualitySort.onSelectSortEvent += OnSelectSort;  | 
|             model.oneKeyResolveRefresh += OneKeyResolveRefresh;  | 
|             DTC0721_tagMakeItemAnswer.MakeItemAnswerEvent += MakeItemAnswerEvent;  | 
|             m_ScrollerControl.JumpIndex(0);  | 
|         }  | 
|   | 
|         protected override void OnAfterOpen()  | 
|         {  | 
|         }  | 
|   | 
|         protected override void OnPreClose()  | 
|         {  | 
|             model.autoResolve = m_AutoResolveToggle.isOn;  | 
|             model.resolveItems.Clear();  | 
|             PlayerDatas.Instance.playerDataRefreshEvent -= PlayerDataRefreshInfoEvent;  | 
|             model.gatherSoulPackRefresh -= GatherSoulPackRefresh;  | 
|             m_QualitySort.onSelectSortEvent -= OnSelectSort;  | 
|             model.oneKeyResolveRefresh -= OneKeyResolveRefresh;  | 
|             DTC0721_tagMakeItemAnswer.MakeItemAnswerEvent -= MakeItemAnswerEvent;  | 
|         }  | 
|   | 
|         protected override void OnAfterClose()  | 
|         {  | 
|         }  | 
|   | 
|         protected override void LateUpdate()  | 
|         {  | 
|             if (qualitySortOpen != m_QualitySort.gameObject.activeSelf)  | 
|             {  | 
|                 qualitySortOpen = m_QualitySort.gameObject.activeSelf;  | 
|                 QualitySortRefresh();  | 
|             }  | 
|         }  | 
|         #endregion  | 
|   | 
|         int GetDefaultQuality()  | 
|         {  | 
|             //for (int i = 4; i >= 2; i--)  | 
|             //{  | 
|             //    model.GetResolveSouls(i, model.resolveItems);  | 
|             //    if (model.resolveItems.Count > 0)  | 
|             //    {  | 
|             //        return i;  | 
|             //    }  | 
|             //}  | 
|             return GatheringSoulModel.RARA_GATHERSOUL_QUALITY - 1;  | 
|         }  | 
|   | 
|         private void PlayerDataRefreshInfoEvent(PlayerDataType refreshType)  | 
|         {  | 
|             if (refreshType == PlayerDataType.CDBPlayerRefresh_SoulDust)  | 
|             {  | 
|                 DisplayResolveSoulDust();  | 
|             }  | 
|         }  | 
|   | 
|         private void OnRefreshCell(ScrollerDataType type, CellView cell)  | 
|         {  | 
|             var resolveCell = cell as GatherSoulResolveCell;  | 
|             resolveCell.Display(cell.index);  | 
|         }  | 
|   | 
|         private void MakeItemAnswerEvent(H0721_tagMakeItemAnswer package)  | 
|         {  | 
|             if (package.MakeType == (int)MakeType.GatherSoulDecompose)  | 
|             {  | 
|                 EffectMgr.Instance.PlayUIEffect(3080, 3100, transform, false);  | 
|             }  | 
|         }  | 
|   | 
|         private void OnSelectSort(SortType type, int sort)  | 
|         {  | 
|             if (type == SortType.EquipQuality)  | 
|             {  | 
|                 selectQuality = sort;  | 
|                 Display();  | 
|             }  | 
|         }  | 
|   | 
|         private void QualitySortRefresh()  | 
|         {  | 
|             m_Arrow.transform.localEulerAngles = qualitySortOpen ? Vector3.zero : new Vector3(0, 0, 180);  | 
|         }  | 
|   | 
|         private void OneKeyResolveRefresh()  | 
|         {  | 
|             m_ScrollerControl.m_Scorller.RefreshActiveCellViews();  | 
|             DisplayResolveSoulDust();  | 
|         }  | 
|   | 
|         private void Display()  | 
|         {  | 
|             DisplayResolveItems();  | 
|             DisplayResolveSoulDust();  | 
|         }  | 
|   | 
|         private void GatherSoulPackRefresh()  | 
|         {  | 
|             bool change = false;  | 
|             for (int i = 0; i < model.resolveItems.Count; i++)  | 
|             {  | 
|                 if (model.resolveItems[i] == null)  | 
|                 {  | 
|                     continue;  | 
|                 }  | 
|                 if (!model.CheckExist(model.resolveItems[i]))  | 
|                 {  | 
|                     change = true;  | 
|                     model.resolveItems[i] = null;  | 
|                 }  | 
|             }  | 
|             if (change)  | 
|             {  | 
|                 m_ScrollerControl.m_Scorller.RefreshActiveCellViews();  | 
|             }  | 
|         }  | 
|   | 
|         void DisplayResolveItems()  | 
|         {  | 
|             model.GetResolveSouls(selectQuality, model.resolveItems);  | 
|             model.resolveItems.Sort(model.CommonSort2);  | 
|             if (m_ScrollerControl.GetNumberOfCells(m_ScrollerControl.m_Scorller) == 0)  | 
|             {  | 
|                 var count = virtualPack.GetPackCapacity(PackType.GatherSoul);  | 
|                 var line = Mathf.CeilToInt((float)count / m_LineCount);  | 
|                 m_ScrollerControl.Refresh();  | 
|                 for (int i = 0; i < line; i++)  | 
|                 {  | 
|                     m_ScrollerControl.AddCell(ScrollerDataType.Header, i);  | 
|                 }  | 
|                 m_ScrollerControl.Restart();  | 
|             }  | 
|             else  | 
|             {  | 
|                 m_ScrollerControl.m_Scorller.RefreshActiveCellViews();  | 
|             }  | 
|         }  | 
|   | 
|         void DisplayResolveSoulDust()  | 
|         {  | 
|             var result = 0;  | 
|             for (int i = 0; i < model.resolveItems.Count; i++)  | 
|             {  | 
|                 if (model.resolveItems[i] == null)  | 
|                 {  | 
|                     continue;  | 
|                 }  | 
|                 result += model.GetResolveReturnCost(model.resolveItems[i].id, model.resolveItems[i].level);  | 
|             }  | 
|             var soulDust = UIHelper.ReplaceLargeNum(UIHelper.GetMoneyCnt(28));  | 
|             m_SoulDust.text = StringUtility.Contact(soulDust, "+",  | 
|                 UIHelper.AppendColor(TextColType.DarkGreen, UIHelper.ReplaceLargeNum(result)));  | 
|         }  | 
|   | 
|         private void OpenQualitySort()  | 
|         {  | 
|             if (!m_QualitySort.gameObject.activeSelf)  | 
|             {  | 
|                 m_QualitySort.gameObject.SetActive(true);  | 
|             }  | 
|         }  | 
|   | 
|         private void Resolve()  | 
|         {  | 
|             if (model.resolveItems.Count == 0)  | 
|             {  | 
|                 return;  | 
|             }  | 
|   | 
|             bool existUnEquipBetterProperty = false;  | 
|             bool existRareGatherSoul = false;  | 
|   | 
|             for (int i = 0; i < model.resolveItems.Count; i++)  | 
|             {  | 
|                 var item = model.resolveItems[i];  | 
|                 if (item != null && model.CheckExist(item))  | 
|                 {  | 
|                     if (item.itemType == GatheringSoulModel.GATHERSOUL_ESSENCE_TYPE)  | 
|                     {  | 
|                         continue;  | 
|                     }  | 
|                     if (item.itemColor >= GatheringSoulModel.RARA_GATHERSOUL_QUALITY)  | 
|                     {  | 
|                         existRareGatherSoul = true;  | 
|                         break;  | 
|                     }  | 
|                 }  | 
|             }  | 
|             if (!existRareGatherSoul)  | 
|             {  | 
|                 for (int i = 0; i < model.resolveItems.Count; i++)  | 
|                 {  | 
|                     var item = model.resolveItems[i];  | 
|                     if (item != null && model.CheckExist(item))  | 
|                     {  | 
|                         if (item.itemType == GatheringSoulModel.GATHERSOUL_ESSENCE_TYPE)  | 
|                         {  | 
|                             continue;  | 
|                         }  | 
|                         if (model.IsSamePropertyCompareHoles(-1, item) == -1)  | 
|                         {  | 
|                             existUnEquipBetterProperty = true;  | 
|                             break;  | 
|                         }  | 
|                     }  | 
|                 }  | 
|             }  | 
|   | 
|             if (existRareGatherSoul)  | 
|             {  | 
|                 ConfirmCancel.ShowPopConfirm(Language.Get("Mail101"), Language.Get("RareGatherSoulResolveConfirm"), (bool isOk) =>  | 
|                 {  | 
|                     if (isOk)  | 
|                     {  | 
|                         TryResolve();  | 
|                     }  | 
|                 });  | 
|             }  | 
|             else if (existUnEquipBetterProperty)  | 
|             {  | 
|                 ConfirmCancel.ShowPopConfirm(Language.Get("Mail101"), Language.Get("GatherSoulResolveConfirm"), (bool isOk) =>  | 
|                   {  | 
|                       if (isOk)  | 
|                       {  | 
|                           TryResolve();  | 
|                       }  | 
|                   });  | 
|             }  | 
|             else  | 
|             {  | 
|                 TryResolve();  | 
|             }  | 
|         }  | 
|   | 
|         void TryResolve()  | 
|         {  | 
|             if (model.resolveItems.Count == 0)  | 
|             {  | 
|                 return;  | 
|             }  | 
|   | 
|             List<ushort> list = new List<ushort>();  | 
|             for (int i = 0; i < model.resolveItems.Count; i++)  | 
|             {  | 
|                 if (model.resolveItems[i] != null &&  | 
|                     model.CheckExist(model.resolveItems[i]))  | 
|                 {  | 
|                     list.Add((ushort)model.resolveItems[i].index);  | 
|                 }  | 
|             }  | 
|   | 
|             var page = Mathf.CeilToInt((float)list.Count / 50);  | 
|             for (int i = 0; i < page; i++)  | 
|             {  | 
|                 ushort[] resolves = list.Skip(i * 50).Take((i < page - 1) ? 50 : (list.Count - i * 50)).ToArray();  | 
|                 model.SendResolvePack(resolves, false);  | 
|             }  | 
|         }  | 
|     }  | 
|   | 
| }  | 
|   | 
|   | 
|   | 
|   |