using System;  
 | 
using UnityEngine;  
 | 
using UnityEngine.UI;  
 | 
using System.Collections.Generic;  
 | 
using System.Collections;  
 | 
  
 | 
namespace vnxbqy.UI  
 | 
{  
 | 
      
 | 
    public class AutomaticTreasureWin : Window  
 | 
    {  
 | 
        [SerializeField] Button closeBtn;  
 | 
        [SerializeField] Button treasureBtn;  
 | 
        [SerializeField] Text treasureBtn_Text;  
 | 
        [SerializeField] Transform container_Awards;  
 | 
        [SerializeField] AutoTreasureAwardsRow awardsRow;  
 | 
        [SerializeField] RectTransform container;  
 | 
  
 | 
        PlayerDeadModel deadModel { get { return ModelCenter.Instance.GetModel<PlayerDeadModel>(); } }  
 | 
        LuckyTreasureModel luckyTreasureModel { get { return ModelCenter.Instance.GetModel<LuckyTreasureModel>(); } }  
 | 
          
 | 
        List<AutoTreasureAwardsRow> awardsRows = new List<AutoTreasureAwardsRow>();  
 | 
        Vector3 pos = Vector3.zero;  
 | 
        private bool isStart = true;  
 | 
        #region Built-in  
 | 
        protected override void BindController()  
 | 
        {  
 | 
            pos = container.anchoredPosition3D;  
 | 
        }  
 | 
  
 | 
        protected override void AddListeners()  
 | 
        {  
 | 
            closeBtn.AddListener(CloseClick);  
 | 
        }  
 | 
  
 | 
        protected override void OnPreOpen()  
 | 
        {  
 | 
            deadModel.playerDieEvent += UpdatePlayerDie;  
 | 
            luckyTreasureModel.UpdateLuckyResultEvent += UpdateLuckyResult;  
 | 
            SetDisplay();  
 | 
        }  
 | 
  
 | 
        protected override void OnAfterOpen()  
 | 
        {  
 | 
  
 | 
        }  
 | 
  
 | 
        protected override void OnPreClose()  
 | 
        {  
 | 
            isStart = false;  
 | 
            StopAllCoroutines();  
 | 
            luckyTreasureModel.isAutoLuckyTreasure = false;  
 | 
            deadModel.playerDieEvent -= UpdatePlayerDie;  
 | 
            luckyTreasureModel.UpdateLuckyResultEvent -= UpdateLuckyResult;  
 | 
            OnDestroyAwardsRow();  
 | 
            luckyTreasureModel.autoLuckyItems.Clear();  
 | 
            container.anchoredPosition3D = pos;  
 | 
        }  
 | 
        protected override void OnAfterClose()  
 | 
        {  
 | 
  
 | 
        }  
 | 
        #endregion  
 | 
  
 | 
        private void SetDisplay()  
 | 
        {  
 | 
            luckyTreasureModel.isAutoLuckyTreasure = true;  
 | 
            isStart = luckyTreasureModel.isAutoLuckyTreasure;  
 | 
            awardsRows.Clear();  
 | 
            UpdateAwardsItem();  
 | 
            UpdateLuckyTreasureState();  
 | 
            luckyTreasureModel.SendStartLuckyTreasure();  
 | 
        }  
 | 
  
 | 
        private void UpdatePlayerDie()  
 | 
        {  
 | 
            Close();  
 | 
        }  
 | 
  
 | 
        private void UpdateLuckyResult()  
 | 
        {  
 | 
            UpdateAwardsItem();  
 | 
            if(isStart)  
 | 
            {  
 | 
                if(luckyTreasureModel.IsBigLuckItem())  
 | 
                {  
 | 
                    Close();  
 | 
                }  
 | 
                else  
 | 
                {  
 | 
                    StartCoroutine(DelaySendLuckyTreasure());  
 | 
                }  
 | 
            }  
 | 
        }  
 | 
  
 | 
        IEnumerator DelaySendLuckyTreasure()  
 | 
        {  
 | 
            yield return new WaitForSeconds(0.3f);  
 | 
            if(!luckyTreasureModel.IsEnoughMoney())  
 | 
            {  
 | 
                Close();  
 | 
            }  
 | 
            else  
 | 
            {  
 | 
                if(luckyTreasureModel.IsSatifyLuckyTreasure())  
 | 
                {  
 | 
                    luckyTreasureModel.SendStartLuckyTreasure();  
 | 
                }  
 | 
                else  
 | 
                {  
 | 
                    isStart = false;  
 | 
                    UpdateLuckyTreasureState();  
 | 
                }  
 | 
            }            
 | 
        }  
 | 
  
 | 
        private void UpdateLuckyTreasureState()  
 | 
        {  
 | 
            treasureBtn.RemoveAllListeners();  
 | 
            if (isStart)  
 | 
            {  
 | 
                treasureBtn_Text.text = Language.Get("LuckyTreasure104");  
 | 
                treasureBtn.AddListener(ClickStopLuckyTreasure);  
 | 
            }  
 | 
            else  
 | 
            {  
 | 
                treasureBtn_Text.text = Language.Get("LuckyTreasure105");  
 | 
                treasureBtn.AddListener(ClickLuckyTreasure);  
 | 
            }  
 | 
        }  
 | 
  
 | 
        private void ClickStopLuckyTreasure()  
 | 
        {  
 | 
            isStart = false;  
 | 
            UpdateLuckyTreasureState();  
 | 
        }  
 | 
  
 | 
        private void ClickLuckyTreasure()  
 | 
        {  
 | 
            if(luckyTreasureModel.IsSatifyLuckyTreasure())  
 | 
            {  
 | 
                isStart = true;  
 | 
                luckyTreasureModel.SendStartLuckyTreasure();  
 | 
                UpdateLuckyTreasureState();  
 | 
            }  
 | 
        }  
 | 
  
 | 
        private void UpdateAwardsItem()  
 | 
        {  
 | 
            CreateAwardsRow();  
 | 
            for(int i = 0; i < awardsRows.Count; i++)  
 | 
            {  
 | 
                var treasureAwardsRow = awardsRows[i];  
 | 
                treasureAwardsRow.SetDisplay(i);  
 | 
            }  
 | 
        }  
 | 
  
 | 
        private void CreateAwardsRow()  
 | 
        {  
 | 
            var autoLuckyItems = luckyTreasureModel.autoLuckyItems;  
 | 
            int row = autoLuckyItems.Count / 10;  
 | 
            if(autoLuckyItems.Count % 10 > 0)  
 | 
            {  
 | 
                row += 1;  
 | 
            }  
 | 
            int startRow = awardsRows.Count;  
 | 
            for(int i = startRow; i < row; i++)  
 | 
            {  
 | 
                AutoTreasureAwardsRow treasureAwardsRow = Instantiate(awardsRow, Vector3.zero, Quaternion.identity, container_Awards);  
 | 
                treasureAwardsRow.transform.localScale = Vector3.one;  
 | 
                treasureAwardsRow.SetActive(true);  
 | 
                awardsRows.Add(treasureAwardsRow);  
 | 
            }  
 | 
            awardsRow.SetActive(false);  
 | 
        }  
 | 
  
 | 
        private void OnDestroyAwardsRow()  
 | 
        {  
 | 
            for(int i = 0; i < awardsRows.Count;i++)  
 | 
            {  
 | 
                DestroyObject(awardsRows[i].gameObject);  
 | 
            }  
 | 
        }  
 | 
  
 | 
    }  
 | 
}  
 |