// ------------------------------------------------------------------------------
//  <autogenerated>
//      This code was generated by a tool.
//      Mono Runtime Version: 2.0.50727.1433
// 
//      Changes to this file may cause incorrect behavior and will be lost if 
//      the code is regenerated.
//  </autogenerated>
// ------------------------------------------------------------------------------

namespace QFramework.SaoLei
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class GameMainUIPanelData : QFramework.UIPanelData
    {
        public int mineNumber = 0;
    }
    
    public partial class GameMainUIPanel : QFramework.UIPanel
    {
        //行数
        private int row = 10;
        //列数
        private int col = 10;
        //总雷数
        private int totalMineNumber = 0;
        private int tempMineNumber = 0;
        //棋盘
        private GameMainMineCell[,] mineArray = new GameMainMineCell[10,10];

        private GameObject mineCellPrefab = null;
        private ResLoader mResLoader = ResLoader.Allocate();
        public bool isGameOver = false;
        protected override void ProcessMsg(int eventId, QFramework.QMsg msg)
        {
            throw new System.NotImplementedException ();
        }
        
        protected override void OnInit(QFramework.IUIData uiData)
        {
            mData = uiData as GameMainUIPanelData ?? new GameMainUIPanelData();
            mineCellPrefab = mResLoader.LoadSync<GameObject>("GameMainMineCell");
            // please add init code here
            TypeEventSystem.Global.Register<GameMainCellEvent>(OnClickMineCell);
            TypeEventSystem.Global.Register<GameMainResetEvent>(ResetGame);
            InitMineCells();
        }
        //重新开始
        void ResetGame(GameMainResetEvent resetEvent)
        {
            isGameOver = false;
            tempMineNumber = totalMineNumber;
            SetScoreText(tempMineNumber);
            SetMines(totalMineNumber);
        }

        void SetScoreText(int number)
        {
            GameMainScoreText.text = "剩余:" + number;
        }
        //初始化棋盘
        void InitMineCells()
        {
            for(int i = 0; i < row; i++)
            {
                for(int j = 0; j < col; j++)
                {
                    mineArray[i,j] = mineCellPrefab.Instantiate()
                        .transform
                        .Parent(GameMainPlayTrans)
                        .LocalIdentity()
                        .Name(i + "_" + j)
                        .gameObject
                        .GetComponent<GameMainMineCell>();
                    mineArray[i,j].SetInfo(i,j);

                }
            }
        }
        //设置地雷
         void SetMines (int count)
        {
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    mineArray [i, j].SetStatus(MineCellStatus.None);
                    mineArray [i, j].hasMine =false;
                }
            }
            for (int i = 0; i < count;) {
                GameMainMineCell mine = mineArray[UnityEngine.Random.Range (0, row), UnityEngine.Random.Range (0, col)];
                if(!mine.hasMine)
                {
                    mine.hasMine = true;
                    i++;
                }
                
            }
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    mineArray [i, j].mineNumbers = CountMines(i, j);
                }
            }
        }
        void OnClickMineCell(GameMainCellEvent gameMainCellEvent)
        {
            if(isGameOver)
                return;
            if(gameMainCellEvent.clickType == 0)//左击选择
                CheckMine(gameMainCellEvent.row,gameMainCellEvent.col);
            else if(gameMainCellEvent.clickType == 1)//右击标记
                MarkMine(gameMainCellEvent.row,gameMainCellEvent.col);
        }
        void MarkMine(int x,int y)
        {
            switch(mineArray[x,y].currentStatus)
            {
                case MineCellStatus.None:
                    if(tempMineNumber== 0)
                        return;
                    tempMineNumber --;
                    SetScoreText(tempMineNumber);
                    mineArray[x,y].SetStatus(MineCellStatus.MarkMine);
                break;
                case MineCellStatus.MarkMine:

                    tempMineNumber ++;
                    SetScoreText(tempMineNumber);
                    mineArray[x,y].SetStatus(MineCellStatus.MarkQuery);
                break;
                case MineCellStatus.MarkQuery:
                    mineArray[x,y].SetStatus(MineCellStatus.None);

                break;
            }

        }
        void CheckMine(int x,int y)
        {
            if(mineArray[x,y].currentStatus == MineCellStatus.MarkMine)
                return;
            mineArray[x,y].isClicked = true;
            if (!mineArray [x, y].hasMine) {
                if(mineArray[x,y].mineNumbers == 0)
                {
                    mineArray[x,y].SetStatus(MineCellStatus.Empty);
                    for (int i = -1; i <= 1; i++) {
                        for (int j = -1; j <= 1; j++) {
                            if (!(i == 0 && j == 0)) {
                                int n = x + i;
                                int m = y + j;
                                if (n >= 0 && n < row && m >= 0 && m < col && !mineArray [n, m].isClicked) {
                                    CheckMine(n, m);
                                }
                            }
                        }
                    }
                }else 
                {
                    
                    mineArray[x,y].SetStatus(MineCellStatus.Number);
                }
                
            }else
            {
                mineArray[x,y].SetStatus(MineCellStatus.ClickMine);
                GameOver(false);
            }
            int sum = 0;
            for (int i = 0; i < row; i++) { 
                for (int j = 0; j < col; j++) {
                    if (mineArray [i, j].isClicked) {
                        sum ++;
                    }
                    if (mineArray [i, j].hasMine) {
                        sum ++;
                    }
                    if (sum == row*col) {
                        GameOver(true);                 
                    }
    
    
                }
            }
        }
        //计算格子周围雷的总数
        int CountMines(int x,int y)
        {
            int count = 0;
            for (int i = -1; i <= 1; i++) {
                for (int j = -1; j <= 1; j++) {
                    if (!(i == 0 && j == 0)) {
                        int n = x + i;
                        int m = y + j;
                        if (n >= 0 && n < row && m >= 0 && m < col) {
                            if (mineArray [n, m].hasMine) {
                                count++;
                            }
                        }
                    }
                }
            }
            return count;
        }

        void GameOver(bool win)
        {
            isGameOver = true;
            if(!win)
            {
                for (int i = 0; i < row; i++) { 
                    for (int j = 0; j < col; j++) {
                        if (!mineArray [i, j].isClicked && mineArray [i, j].currentStatus != MineCellStatus.MarkMine && mineArray [i, j].hasMine ) 
                        {
                            mineArray [i, j].SetStatus(MineCellStatus.ShowMines);                            
                        }
                    }
                }
           }
            this.Delay(2.0f,()=>
                {
                    UIKit.OpenPanel<GameEndUIPanel>(new GameEndUIPanelData()
                    {
                        isWin = win
                    });
                });
            
        }
        protected override void OnOpen(QFramework.IUIData uiData)
        {
          
            mData = uiData as GameMainUIPanelData ?? new GameMainUIPanelData();
         
            totalMineNumber = mData.mineNumber;
            ResetGame(null);
        }
        
        protected override void OnShow()
        {

        }
        
        protected override void OnHide()
        {
        }
        
        protected override void OnClose()
        {			
            TypeEventSystem.Global.UnRegister<GameMainResetEvent>(ResetGame);
			TypeEventSystem.Global.UnRegister<GameMainCellEvent>(OnClickMineCell);

            mResLoader.Recycle2Cache();
            mResLoader = null;
        }
    }
}
