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

public class BuildingScript : MonoBehaviour
{
    [SerializeField]
    float p_myTimeToSpawnMin;
    [SerializeField]
    float p_myTimeToSpawnMax;
    [SerializeField]
    GameObject p_myHumanPrefab;
    [SerializeField]
    GameObject p_myEarth;
    [SerializeField]
    GameObject p_myGaz;
    [SerializeField]
    GameObject p_myCthulhu;
    [SerializeField]
    GameObject p_myUFO;
    [SerializeField]
    AudioSource p_myAudioSource;

    GameObject p_myModel1;
    GameObject p_myModel2;
    GameObject p_myModel3;
    GameObject p_myModel4;

    float p_mySpawnTime;

    role p_myRole;

    public void setRole(role _role)
    {
        if (p_myModel1 == null)
        {
            getModel();
        }
        p_myRole = _role;
        switch (_role)
        {
            case role.Nothing:
                p_myModel1.SetActive(true);
                break;
            case role.Cthulu:
                p_myModel3.SetActive(true);
                break;
            case role.Radio:
                p_myModel2.SetActive(true);
                break;
            case role.Scientist:
                p_myModel4.SetActive(true);
                break;
        }
    }

    void getModel()
    {
        p_myModel1 = transform.GetChild(0).gameObject;
        p_myModel2 = transform.GetChild(1).gameObject;
        p_myModel3 = transform.GetChild(2).gameObject;
        p_myModel4 = transform.GetChild(3).gameObject;
        p_myModel1.SetActive(false);
        p_myModel2.SetActive(false);
        p_myModel3.SetActive(false);
        p_myModel4.SetActive(false);
    }

    void Start()
    {
        p_myAudioSource = GameObject.FindGameObjectWithTag("Sound").transform.GetChild(1).GetComponent<AudioSource>();
        if (tag != "Building")
        {
            p_myEarth = GameObject.FindGameObjectWithTag("Earth");
            StartCoroutine(SpawnHuman());
        }
    }

    public void Initialize(GameObject earth)
    {
        if (p_myAudioSource == null)
        {
            p_myAudioSource = GameObject.FindGameObjectWithTag("Sound").transform.GetChild(1).GetComponent<AudioSource>();
        }
        if (!p_myAudioSource.isPlaying)
        {
            p_myAudioSource.Play();
        }
        p_mySpawnTime = Time.time;
        p_myEarth = earth;
        StartCoroutine(SpawnHuman());
    }

    public float getSpawnTime()
    {
        return p_mySpawnTime;
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Building")
        {
            if (collision.gameObject.GetComponent<BuildingScript>().getSpawnTime() < getSpawnTime())
            {
                PoolManager.Death(this.gameObject);
            }
        }
        if (collision.gameObject.tag == "Gaz")
        {
            Death();
        }
    }

    IEnumerator SpawnHuman()
    {
        while (true)
        {
            float timeToSpawn = RandomManager.getFloatRandom(p_myTimeToSpawnMax - p_myTimeToSpawnMin) + p_myTimeToSpawnMin;
            yield return new WaitForSeconds(timeToSpawn);
            Spawn();
        }
    }

    void Spawn()
    {
        GameObject newHuman = PoolManager.Instantiation(p_myHumanPrefab.tag, transform.position, Quaternion.identity);
        newHuman.transform.parent = p_myEarth.transform;
    }

    public void Death()
    {
        if (tag == "Building")
        {
            switch (p_myRole)
            {
                case role.Cthulu:
                    CthulhuSpawn();
                    break;
                case role.Nothing:
                    break;
                case role.Scientist:
                    ScientistSpawn();
                    break;
                case role.Radio:
                    UFOSpawn();
                    break;
            }
            p_myModel1.SetActive(false);
            p_myModel2.SetActive(false);
            p_myModel3.SetActive(false);
            p_myModel4.SetActive(false);
            PoolManager.Death(this.gameObject);
        }
    }
    
    void ScientistSpawn()
    {
        GameObject newItem = Instantiate(p_myGaz, transform.position, Quaternion.identity) as GameObject;
        newItem.transform.parent = p_myEarth.transform;
        newItem.transform.LookAt(p_myEarth.transform.position);
    }

    void CthulhuSpawn()
    {
        if (GameObject.FindGameObjectWithTag("Cthulhu") == null || HumanManager.getInfinity())
        {
            GameObject newItem = Instantiate(p_myCthulhu, transform.position, Quaternion.identity) as GameObject;
            newItem.transform.parent = p_myEarth.transform;
            newItem.transform.LookAt(p_myEarth.transform.position);
        }
    }

    void UFOSpawn()
    {
        GameObject newItem = Instantiate(p_myUFO, transform.position, Quaternion.identity) as GameObject;
        newItem.transform.position = p_myEarth.transform.position + transform.forward * 20;
    }
}