using FMODUnity;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Manager : MonoBehaviour
{
    //singleton
    public static Manager Instance => GetInstance();
    static Manager m_Instance;
    public Selector m_Selector;
    public GameObject m_tileBlink;
    GameObject[] tileBlinklist;
    public PlantsController m_actualPlant;
    public CuadradoRaices lastRootSelected;
    //public bool m_rootSelectionMode;

    public GameObject[] m_plants { get; private set; }

    public bool readyToPlay = false;
    public bool lost { get; private set; }
    public bool win { get; private set; }


    private void Awake()
    {
        tileBlinklist = new GameObject[3];
        m_plants = GameObject.FindGameObjectsWithTag("Plant");
        PlantsController.OnDie += SetLost;

        win = false;
        lost = false;
    }

    private void OnDisable()
    {
        PlantsController.OnDie -= SetLost;
    }

    static Manager GetInstance()
    {
        if (m_Instance == null)
        {
            m_Instance = new GameObject("GameController").AddComponent<Manager>();
        }
        return m_Instance;
    }

    internal void StartPlaying()
    {
        readyToPlay = true;
        lastRootSelected.PutTileBlink();
        m_Selector.Init();
    }

    public void SetPlantMode(PlantsController plant)
    {
        m_actualPlant = plant;
    }

    public void SetSelectionMode(CuadradoRaices root)
    {
        if (!win)
        {
            lastRootSelected = root;
            m_Selector.transform.position = lastRootSelected.transform.position;
        }
    }
    public void SetTilesBlink(GameObject a, GameObject b, GameObject c)
    {
        foreach (GameObject tb in tileBlinklist)
        {
            Destroy(tb);
        }
        if (a != null)
        {
            tileBlinklist[0] = Instantiate(m_tileBlink, a.transform.position, Quaternion.identity);
        }
        if (b != null)
        {
            tileBlinklist[1] = Instantiate(m_tileBlink, b.transform.position, Quaternion.identity);

        }
        if (c != null)
        {
            tileBlinklist[2] = Instantiate(m_tileBlink, c.transform.position, Quaternion.identity);

        }
    }
    public void DestroyTileBlink()
    {
        foreach (GameObject tb in tileBlinklist)
        {
            Destroy(tb);
        }
    }

    public void SetLost()
    {
        lost = true;
        m_Selector.Hide();
        m_Selector.levelController.EndGame(false);

        //level lost sound effects
        StudioEventEmitter musicreference;
        musicreference = (StudioEventEmitter)FindObjectOfType(typeof(StudioEventEmitter));
        musicreference.Stop();
        FMODUnity.RuntimeManager.PlayOneShot("event:/Plant/Plant_die");
    }

    public void SetWin()
    {
        bool l_win = true;
        foreach (GameObject go in m_plants)
        {
            if (!go.GetComponent<PlantsController>().thisPlantWin)
            {
                l_win = false;
            }
        }
        if (l_win)
        {
            Debug.Log("you win");
            win = true;
            DestroyTileBlink();
            m_Selector.Hide();
            m_Selector.levelController.EndGame(true);

            //level win sound effects
            StudioEventEmitter musicreference;
            musicreference = (StudioEventEmitter)FindObjectOfType(typeof(StudioEventEmitter));
            if(musicreference != null)
            {
                musicreference.Stop();
            }
            FMODUnity.RuntimeManager.PlayOneShot("event:/Plant/Plant_levelWin");
        }

    }
    public bool GetWin()
    {
        return win;
    }
    public bool GetLost()
    {
        return lost;
    }
    public bool StillPlaying()
    {
        return readyToPlay && !(lost || win);
    }

    public bool GetCanStartGeneringAliments()
    {
        bool canStartGeneringAliments = true;
        foreach (GameObject pc in m_plants)
        {
            if (!pc.GetComponent<PlantsController>().needsSatisfied)
            {
                canStartGeneringAliments = false;
            }
        }
        return canStartGeneringAliments;
    }


    // Update is called once per frame
    void Update()
    {
        if (win || lost)
        {
            DestroyTileBlink();
        }
    }
}
