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

public class CowMooScript : MonoBehaviour {

    public GameObject steak;
    private AudioClip cowDeathSound;

    public AudioClip cowDeath1;
    public AudioClip cowDeath2;
    public AudioClip cowDeath3;

    public AudioClip cowNeutral1;
    public AudioClip cowNeutral2;
    public AudioClip cowNeutral3;
    public AudioClip cowNeutral4;

    private float elapsedTimer = 0f;
    private float mooTime;

	// Use this for initialization
	void Start () {
        mooTime = Random.Range(20f, 60f);
        int deathIndex = Random.Range(0, 3);

        switch (deathIndex)
        {
            case 0:
                cowDeathSound = cowDeath1;
                break;
            case 1:
                cowDeathSound = cowDeath2;
                break;
            case 2:
                cowDeathSound = cowDeath3;
                break;
            default:
                cowDeathSound = cowDeath1;
                break;
        }
	}

	void FixedUpdate()
	{
        elapsedTimer += Time.smoothDeltaTime;

        if (elapsedTimer >= mooTime)
        {
            elapsedTimer = 0f;
            PlayCowMooSound();
        }
	}

	void OnTriggerEnter(Collider other)
	{

        if(other.gameObject.name.ToLower().Equals("meat cleaver"))
        {
            GameObject.Instantiate(steak, gameObject.transform.position, gameObject.transform.rotation);
            GameObject.Instantiate(steak, gameObject.transform.position, gameObject.transform.rotation);
            GameObject.Instantiate(steak, gameObject.transform.position, gameObject.transform.rotation);
            GameObject.Instantiate(steak, gameObject.transform.position, gameObject.transform.rotation);
            GameObject.Instantiate(steak, gameObject.transform.position, gameObject.transform.rotation);
            GameObject.Instantiate(steak, gameObject.transform.position, gameObject.transform.rotation);
            GameObject.Instantiate(steak, gameObject.transform.position, gameObject.transform.rotation);
            GameObject.Instantiate(steak, gameObject.transform.position, gameObject.transform.rotation);
            GameObject.Instantiate(steak, gameObject.transform.position, gameObject.transform.rotation);
            GameObject.Instantiate(steak, gameObject.transform.position, gameObject.transform.rotation);

            GameObject.Destroy(this.gameObject.transform.parent.gameObject);

            GameObject.Find("[CameraRig]").GetComponent<AudioSource>().PlayOneShot(cowDeathSound);

            other.gameObject.GetComponent<CleaverSoundScript>().PlayMySound();
        }
	}


    private void PlayCowMooSound()
    {
        int soundIndex = Random.Range(0, 5);

        switch (soundIndex)
        {
            case 0:
                gameObject.GetComponent<AudioSource>().clip = cowNeutral1;
                break;
            case 1:
                gameObject.GetComponent<AudioSource>().clip = cowNeutral2;
                break;
            case 2:
                gameObject.GetComponent<AudioSource>().clip = cowNeutral3;
                break;
            case 3:
                gameObject.GetComponent<AudioSource>().clip = cowNeutral4;
                break;
            default:
                gameObject.GetComponent<AudioSource>().clip = cowNeutral1;
                break;
        }

        gameObject.GetComponent<AudioSource>().Play();
    }
}
