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

public enum role
{
    Cthulu,
    Radio,
    Scientist,
    Nothing,
}

public class HumanScript : MonoBehaviour
{
    [SerializeField]
    float p_mySpeed;
    [SerializeField]
    float p_myForwardTime;
    [SerializeField]
    float p_myIdleTime;
    [SerializeField]
    float p_myDistanceWithEarth;
    [SerializeField]
    GameObject p_myBuildingPrefab;

    [SerializeField]
    float p_myChanceToCthulu;
    [SerializeField]
    float p_myChanceToRadio;
    [SerializeField]
    float p_myChanceToScientist;
    [SerializeField]
    float p_myChanceToNothing;

    float p_myChanceTotal;

    role p_myRole;

    float p_myTimer;
    int angle;

    [SerializeField]
    GameObject p_myEarth;
    [SerializeField]
    AudioSource p_myAudioSource;
    
    GameObject p_myModel1;
    GameObject p_myModel2;
    GameObject p_myModel3;
    GameObject p_myModel4;

    GameObject p_myUfo;

    bool p_myInfinity;

    public void setUfo(GameObject ufo)
    {
        p_myUfo = ufo;
    }

    public 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);
    }

    public role getRole()
    {
        return p_myRole;
    }

    void Start()
    {
        p_myAudioSource = GameObject.FindGameObjectWithTag("Sound").transform.GetChild(0).GetComponent<AudioSource>();
    }

    public void Initialize(GameObject earth)
    {
        if (p_myAudioSource == null)
        {
            p_myAudioSource = GameObject.FindGameObjectWithTag("Sound").transform.GetChild(0).GetComponent<AudioSource>();
        }
        if (!p_myAudioSource.isPlaying)
        {
            p_myAudioSource.Play();
        }
        p_myInfinity = HumanManager.getInfinity();
        p_myChanceTotal = p_myChanceToCthulu + p_myChanceToRadio + p_myChanceToScientist;
        if (!p_myInfinity)
        {
            p_myChanceTotal += p_myChanceToNothing;
        }
        float rand = RandomManager.getFloatRandom(p_myChanceTotal);
        getModel();
        if (rand <= p_myChanceToCthulu)
        {
            p_myRole = role.Cthulu;
            p_myModel4.SetActive(true);
        }
        else
        {
            rand -= p_myChanceToCthulu;
            if (rand <= p_myChanceToRadio)
            {
                p_myRole = role.Radio;
                p_myModel3.SetActive(true);
            }
            else
            {
                rand -= p_myChanceToRadio;
                if (rand <= p_myChanceToScientist)
                {
                    p_myRole = role.Scientist;
                    p_myModel2.SetActive(true);
                }
                else
                {
                    p_myRole = role.Nothing;
                    p_myModel1.SetActive(true);
                }
            }
        }

        //HumanManager.addHuman();
        p_myEarth = earth;
        transform.parent = p_myEarth.transform;
        //transform.position = Vector3.zero;
        //transform.localEulerAngles = Vector3.zero;
        HumanManager.addHuman();
        StartCoroutine(IA());
    }

    IEnumerator IA()
    {
        while (p_myUfo == null)
        {
            angle = RandomManager.getIntRandom(0, 360);
            p_myTimer = 0;
            float forwardTime = RandomManager.getFloatRandom(p_myForwardTime);
            while (p_myTimer < forwardTime && p_myUfo == null)
            {
                transform.LookAt(p_myEarth.transform.position);
                transform.position = transform.forward * -p_myDistanceWithEarth;
                transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, angle);
                p_myTimer += Time.deltaTime;
                transform.position += transform.right * p_mySpeed * Time.deltaTime;
                yield return new WaitForEndOfFrame();
            }
            float idleTime = RandomManager.getFloatRandom(p_myIdleTime);
            yield return new WaitForSeconds(idleTime);
            GameObject newBuilding = PoolManager.Instantiation(p_myBuildingPrefab.tag, transform.position, Quaternion.identity);
            newBuilding.transform.LookAt(p_myEarth.transform.position);
            newBuilding.transform.position += -transform.forward * 0.2f;
            newBuilding.transform.parent = p_myEarth.transform;
            newBuilding.GetComponent<BuildingScript>().setRole(p_myRole);
        }
        while (p_myUfo != null)
        {
            transform.position = Vector3.Lerp(transform.position, p_myUfo.transform.position, p_mySpeed / 5 * Time.deltaTime);
            yield return new WaitForEndOfFrame();
        }
    }

    public void Death()
    {
        GameObject audio = GameObject.FindGameObjectWithTag("Sound").transform.GetChild(12).gameObject;
        int rand = RandomManager.getIntRandom(0, audio.transform.childCount);
        AudioSource sound = audio.transform.GetChild(rand).GetComponent<AudioSource>();
        int test = 0;
        while (sound.isPlaying && test < 10)
        {
            test++;
            sound = audio.transform.GetChild(rand).GetComponent<AudioSource>();
        }
        if (!sound.isPlaying)
        {
            sound.Play();
        }
        HumanManager.removeHuman();
        PoolManager.Death(this.gameObject);
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "UFO")
        {
            p_myUfo = null;
            Death();
        }
    }
}