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

using System.Collections;

public enum OperateType
{
    Ripping,
    Watering,
    Fertilizing,
}

public class Flower : MonoBehaviour 
{
    #region 变量

    #region 引用变量

    private Transform ScoreHudTra;
    private Transform PhaseHudTra;
    private Transform OperateHudTra;
    private GameObject GameA;
    private SpriteRenderer FlagSr;
    private SpriteRenderer OperateSr;

    #endregion

    private int _Phase;
    private Text PhaseLabel;
    private Text OperateLabel;
    private OperateType OperateType;

    public int Phase
    {
        get { return _Phase; }
        set
        {
            _Phase = value;
            OnPhaseChange();
        }
    }

    #endregion

    private void Awake()
    {
        #region 引用变量赋值

        Hashtable hashtable = new Hashtable();
        Transform[] transforms = GetComponentsInChildren<Transform>(true);

        for (int i = 0; i < transforms.Length; i++)
        {
            hashtable.Add(transforms[i].name, transforms[i]);
        }

        ScoreHudTra = ((Transform) hashtable["ScoreHudTra"]);
        PhaseHudTra = ((Transform)hashtable["PhaseHudTra"]);
        OperateHudTra = ((Transform) hashtable["OperateHudTra"]);
        GameA = ((Transform)hashtable["GameA"]).gameObject;
        FlagSr = ((Transform) hashtable["FlagSr"]).GetComponent<SpriteRenderer>();
        OperateSr = ((Transform) hashtable["OperateSr"]).GetComponent<SpriteRenderer>();

        #endregion

        GameA.SetActive(false);
    }

    private void FixedUpdate()
    {

    }


    public void MarkAsFirst()
    {
        FlagSr.color = ChangeAlpha(FlagSr.color, 1);
        OperateSr.color = ChangeAlpha(OperateSr.color, 1);
        OperateLabel.color = ChangeAlpha(OperateLabel.color, 1);

        FlagSr.gameObject.SetActive(true);
        OperateSr.gameObject.SetActive(true);
        OperateLabel.gameObject.SetActive(true);
    }

    public void MarkAsSecond()
    {
        FlagSr.color = ChangeAlpha(FlagSr.color, 1);
        OperateSr.color = ChangeAlpha(OperateSr.color, 1);
        OperateLabel.color = ChangeAlpha(OperateLabel.color, 1);

        FlagSr.gameObject.SetActive(false);
        OperateSr.gameObject.SetActive(true);
        OperateLabel.gameObject.SetActive(true);
    }

    public void MarkAsStandby()
    {
        FlagSr.color = ChangeAlpha(FlagSr.color, 0.5f);
        OperateSr.color = ChangeAlpha(OperateSr.color, 0.5f);
        OperateLabel.color = ChangeAlpha(OperateLabel.color, 0.5f);

        FlagSr.gameObject.SetActive(false);
        OperateSr.gameObject.SetActive(true);
        OperateLabel.gameObject.SetActive(true);
    }

    public void OnGameOverA()
    {
        GameA.SetActive(false);

        ManagerResource.Ins.Save(PhaseLabel.gameObject);
        ManagerResource.Ins.Save(OperateLabel.gameObject);
    }

    public void OnGameBeginA()
    {
        GameA.SetActive(true);

        PhaseLabel = ManagerResource.Ins.GetHUD(Color.black, Camera.main.WorldToScreenPoint(PhaseHudTra.position), true);
        OperateLabel = ManagerResource.Ins.GetHUD(Color.black, Camera.main.WorldToScreenPoint(OperateHudTra.position), true);

        Plant();
    }

    public void NeedOperate(int sequence)
    {
        float random = Random.Range(0f, 1f);

        if (random <= 0.3333333f)
        {
            NeedWatering();
        }
        else if (random <= 0.6666666f)
        {
            NeedRipping();
        }
        else
        {
            NeedFertilizing();
        }

        if (sequence == 0)
        {
            MarkAsFirst();
        }
        else if (sequence == 1)
        {
            MarkAsSecond();
        }
        else
        {
            MarkAsStandby();
        }
    }

    public bool Operate(OperateType operateType)
    {
        if (operateType != OperateType) //错误的操作
        {
            return false;
        }
        else //操作正确 植物升级
        {
            Phase++;
            FlagSr.gameObject.SetActive(false);
            OperateSr.gameObject.SetActive(false);
            OperateLabel.gameObject.SetActive(false);

            return true;
        }
    }


    private void Plant()
    {
        Phase = 0;

        FlagSr.gameObject.SetActive(false);
        OperateSr.gameObject.SetActive(false);
        OperateLabel.gameObject.SetActive(false);
    }

    private void Harvest()
    {
        ManagerResource.Ins.GetHUD(Color.yellow, Camera.main.WorldToScreenPoint(ScoreHudTra.position), false, "得分+1");
        ManagerLittleGame.Ins.Scoring(1);

        Plant();
    }

    private void NeedRipping()
    {
        OperateType = OperateType.Ripping;
        OperateLabel.text = "松土";
    }

    private void NeedWatering()
    {
        OperateType = OperateType.Watering;
        OperateLabel.text = "浇水";
    }

    private void NeedFertilizing()
    {
        OperateType = OperateType.Fertilizing;
        OperateLabel.text = "施肥";
    }

    private void OnPhaseChange()
    {
        if (Phase == 0)
        {
            PhaseLabel.text = "阶段0";
        }
        else if (Phase == 1)
        {
            PhaseLabel.text = "阶段1";
        }
        else if (Phase == 2)
        {
            PhaseLabel.text = "阶段2";
        }
        else if (Phase == 3)
        {
            Harvest();
        }
    }

    private Color ChangeAlpha(Color color, float alpha)
    {
        return new Color(color.r, color.g, color.b, alpha);
    }
}