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

public class Game : MonoBehaviour {

    public Item[] items;
    public Text timerTxt;
    public Image warning;
    public GameObject TitleScreen;
    public GameObject LosePage;
    PlayerScript player;
    BearScript bear;
    public int waveNo;
    float waveTime;
    int seconds;
    bool waveOver;
    public AudioSource song;
    List<Item> unclearedItems = new List<Item>();
    Vector3 playerPos;
    Vector3 playerRot;

    //public float[] waveTimes = { 5, 5, 5, 5, 5, 5 };
    public float[] waveTimes = { 60, 45, 30, 15, 10, 5 };

    public bool isWaiting;

    public ParticleSystem waterFX;

    bool waveInProgress;

    bool waveDone;

    // Use this for initialization
    void Start ()
    {
        player = FindObjectOfType<PlayerScript>();
        bear = FindObjectOfType<BearScript>();
        playerPos = player.transform.position;
        playerRot = player.transform.rotation.eulerAngles;
        Restart();
        BearWants();
    }
    
    void Washout()
    {
        //spawn wave
        if (waveInProgress == false)
        {
            warning.enabled = true;
            waterFX.Play();
            waveInProgress = true;
            Invoke("HideNotTiedDown", waterFX.duration); //just in case
            Invoke("ResetWaveInProgress", waterFX.duration * 2.01f); //time for final particle to disappear
            waveOver = true;
        }
    }

    void HideNotTiedDown()
    {
        for (int i = 0; i < items.Length; i++)
        {
            if (items[i].isTied == false)
            {
                //wash away item
                items[i].SetIsCleared(true);
                items[i].gameObject.SetActive(false);
            }
        }
    }

    void ResetWaveInProgress()
    {
        waveInProgress = false;
        warning.enabled = false;
    }

    void BearWants()
    {
        int id;
        List<Item> uncleared = new List<Item>();
        for (int i = 0; i < items.Length; i++)
        {
            if(items[i].GetIsCleared() == false)
            {
                uncleared.Add(items[i]);
            }
        }
        id = (int)Random.Range(0, uncleared.Count);
        Debug.Log(id + " " + uncleared[id].itemName);
        bear.SetThingName(uncleared[id].itemName);
    }

    public void NewWave()
    {
        // reset items
        for (int i = 0; i < items.Length; i++)
        {
            items[i].SetIsTied(false);
        }
        // reset timer
        waveNo++;
        waveTime = waveTimes[waveNo];
        waveOver = false;
        BearWants();
        Debug.Log(waveTime);
    }

    public void Restart()
    {
        waveNo = 0;
        waveTime = waveTimes[waveNo];
        waveOver = true;
        unclearedItems.Clear();
        for (int i = 0; i < items.Length; i++)
        {
            unclearedItems.Add(items[i]);
            items[i].gameObject.SetActive(true);
        }
        isWaiting = false;
        TitleScreen.SetActive(true);
        LosePage.SetActive(false);
        player.enabled = true;
        player.transform.position = playerPos;
        player.transform.rotation = Quaternion.Euler(playerRot);
        song.pitch = 1;
        waveInProgress = false;
        waveDone = false;
        warning.enabled = false;
    }

    void BearComes()
    {
        //start bear sequence
        isWaiting = true;
        bear.Sequence();
    }

    bool TestGameOver()
    {
        bool gameOver = false;

        for (int i = 0; i < items.Length; i++)
        {
            if (items[i].itemName == bear.GetThingName())
            {
                if (items[i].isCleared == true)
                {
                    //game over
                    gameOver = true;
                    Debug.Log("GameOver");
                    break;
                }
            }
        }

        return gameOver;
    }

    bool TestIfWin()
    {
        bool win = false;

        if (!(waveNo < (waveTimes.Length - 1)))
        {
            //win
            Debug.Log("Win");
            win = true;
            song.pitch = 1;
        }
        else
        {
            Debug.Log("Next Round");
        }

        return win;
    }

    public void LoseScreen()
    {
        LosePage.SetActive(true);
    }

    // Update is called once per frame
    void Update ()
    {
        if (waveTime < 0)
        {
            player.enabled = false;
            player.GetComponent<Rigidbody>().velocity = Vector3.zero;
            waveOver = true;
            if(waveInProgress == false)
            {
                if (waveDone == false)
                {
                    waveDone = true;
                    Washout();
                }
                else if (isWaiting == false)
                {
                    isWaiting = true;
                    Debug.Log("Bear Coming");
                    BearComes();
                    if (TestGameOver() == true)
                    {
                        bear.SetThingCleared(true);
                    }
                    else
                    {
                        if (TestIfWin() == true)
                        {
                            //win
                            song.pitch = 1;
                        }
                        else
                        {
                            waveDone = false;
                            NewWave();
                            Debug.Log("New Wave");
                        }
                    }
                }
            }
        }
        else if (waveOver == false && isWaiting == false)
        {
            waveTime -= Time.deltaTime;
            seconds = (int)waveTime;
            timerTxt.text = seconds.ToString();
        }
	}

    public void speedUpMusic()
    {
        song.pitch += 0.25f;
    }





    //UI Functions
    public void Exit()
    {
        Application.Quit();
    }

    public void StartGame()
    {
        TitleScreen.SetActive(false);
        player.enabled = true;
        waveOver = false;
    }
}
