using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Serialization;

using System;
using System.Collections;
using System.Collections.Generic;

using Random = UnityEngine.Random;

public class ManagerLittleGame : MonoBehaviour 
{
    #region 变量

    private Text ScoreTitle;
    private Text ScoreLabel;
    private Text GameStatus;
    private Text RemainTime;
    private Button Ripping;
    private Button Watering;
    private Button Fertilizing;
    private Button GameBegin;
    private GameObject OperatePanel;

    private float GameTimer;
    private float PenaltyTimer;
    private float OpFrequencyTimer;
    private float _GameTime;
    private float _PenaltyTime;
    private float _OpFrequencyTime;
    private bool GameOnA;
    private List<Flower> IdleFlowerList;
    private List<Flower> OperateFlowerList;

    public static ManagerLittleGame Ins;
    
    #endregion

    private void Awake()
    {
        ScoreTitle = ManagerResource.Ins.ScoreTitle;
        ScoreLabel = ManagerResource.Ins.ScoreLabel;
        GameStatus = ManagerResource.Ins.GameStatusLabel;
        RemainTime = ManagerResource.Ins.RemainTimeLabel;
        Ripping = ManagerResource.Ins.RippingBtn;
        Watering = ManagerResource.Ins.WateringBtn;
        Fertilizing = ManagerResource.Ins.FertilizingBtn;
        GameBegin = ManagerResource.Ins.GameBeginBtn;
        OperatePanel = ManagerResource.Ins.OperatePanel;

        Ripping.onClick.AddListener(OnRipping);
        Watering.onClick.AddListener(OnWatering);
        Fertilizing.onClick.AddListener(OnFertilizing);
        GameBegin.onClick.AddListener(OnGameBeginA);

        Ins = this;

        _GameTime = 15;
        _PenaltyTime = 1;
        _OpFrequencyTime = 1;

        OpFrequencyTimer = _OpFrequencyTime;

        IdleFlowerList = new List<Flower>();
        OperateFlowerList = new List<Flower>();
    }

    private void FixedUpdate()
    {
        #region 小游戏逻辑A

        if (!GameOnA)
        {
            return;
        }

        GameTimer -= Time.fixedDeltaTime; //小游戏计时
        RemainTime.text = GameTimer.ToString("0.0");

        if (GameTimer <= 0)
        {
            OnGameOverA();

            return; //小游戏结束
        }

        if (PenaltyTimer > 0) //冻结时间计时
        {
            PenaltyTimer -= Time.fixedDeltaTime;
        }

        OpFrequencyTimer -= Time.fixedDeltaTime; //生成操作计时
        if (OpFrequencyTimer <= 0)
        {
            if (IdleFlowerList.Count > 0)
            {
                OpFrequencyTimer = _OpFrequencyTime;

                Flower flower = IdleFlowerList[Random.Range(0, IdleFlowerList.Count)];

                flower.NeedOperate(OperateFlowerList.Count);

                IdleFlowerList.Remove(flower);
                OperateFlowerList.Add(flower);
            }
        }

        #endregion
    }


    #region 小游戏函数A

    public void Scoring(int score)
    {
        ScoreLabel.text = (int.Parse(ScoreLabel.text) + score).ToString();
    }


    private void OnRipping()
    {
        if (PenaltyTimer > 0) //冻结操作
        {
            return;
        }

        if (OperateFlowerList.Count > 0)
        {
            if (OperateFlowerList[0].Operate(OperateType.Ripping)) //操作正确
            {
                IdleFlowerList.Add(OperateFlowerList[0]);
                OperateFlowerList.Remove(OperateFlowerList[0]);
                UpdateSequence();
            }
            else //操作错误
            {
                PenaltyTimer = _PenaltyTime;
                ManagerHint.Ins.Show("惩罚" + _PenaltyTime + "秒");
            }
        }
    }

    private void OnWatering()
    {
        if (PenaltyTimer > 0) //冻结操作
        {
            return;
        }

        if (OperateFlowerList.Count > 0)
        {
            if (OperateFlowerList[0].Operate(OperateType.Watering)) //操作正确
            {
                IdleFlowerList.Add(OperateFlowerList[0]);
                OperateFlowerList.Remove(OperateFlowerList[0]);
                UpdateSequence();
            }
            else //操作错误
            {
                PenaltyTimer = _PenaltyTime;
                ManagerHint.Ins.Show("惩罚" + _PenaltyTime + "秒");
            }
        }
    }

    private void OnFertilizing()
    {
        if (PenaltyTimer > 0) //冻结操作
        {
            return;
        }

        if (OperateFlowerList.Count > 0)
        {
            if (OperateFlowerList[0].Operate(OperateType.Fertilizing)) //操作正确
            {
                IdleFlowerList.Add(OperateFlowerList[0]);
                OperateFlowerList.Remove(OperateFlowerList[0]);
                UpdateSequence();
            }
            else //操作错误
            {
                PenaltyTimer = _PenaltyTime;
                ManagerHint.Ins.Show("惩罚" + _PenaltyTime + "秒");
            }
        }
    }

    private void OnGameOverA()
    {
        GameOnA = false;

        OperatePanel.SetActive(false);
        ScoreTitle.gameObject.SetActive(false);
        GameBegin.gameObject.SetActive(true);
        GameStatus.gameObject.SetActive(false);

        for (int i = 0; i < IdleFlowerList.Count; i++)
        {
            IdleFlowerList[i].OnGameOverA();
        }

        for (int i = 0; i < OperateFlowerList.Count; i++)
        {
            OperateFlowerList[i].OnGameOverA();
        }

        IdleFlowerList = new List<Flower>();
        OperateFlowerList = new List<Flower>();
    }

    private void OnGameBeginA()
    {
        GameOnA = true;
        GameTimer = _GameTime;

        ScoreLabel.text = "0";

        OperatePanel.SetActive(true);
        ScoreTitle.gameObject.SetActive(true);
        GameBegin.gameObject.SetActive(false);
        GameStatus.gameObject.SetActive(true);

        IdleFlowerList = new List<Flower>(ManagerResource.Ins.FlowerList);

        for (int i = 0; i < IdleFlowerList.Count; i++)
        {
            IdleFlowerList[i].OnGameBeginA();
        }
    }

    private void UpdateSequence()
    {
        if (OperateFlowerList.Count >= 2)
        {
            OperateFlowerList[0].MarkAsFirst();
            OperateFlowerList[1].MarkAsSecond();
        }
        else if (OperateFlowerList.Count >= 1)
        {
            OperateFlowerList[0].MarkAsFirst();
        }
    }

    #endregion
}