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

public class BearScript : MonoBehaviour {

    public AudioSource bearSource;
    public Canvas bearCanvas;
    public Text bearText;
    Game game;
    PlayerScript player;
    bool waveOver;
    bool sequenceStarted;
    string thingName;
    bool thingCleared;

    bool textWaiting;
    bool textWaiting2;

    bool gameOver;

    // Use this for initialization
    void Start ()
    {
        Init();
	}

    public void Init()
    {
        waveOver = false;
        //textShown = false;
        sequenceStarted = false;
        thingCleared = false;
        thingName = "";
        textWaiting = false;
        textWaiting2 = false;
        game = FindObjectOfType<Game>();
        player = FindObjectOfType<PlayerScript>();
        bearCanvas.enabled = false;
        gameOver = false;
    }

    public void PlayBearNoise()
    {
        //play audio
    }

    public void SetWaveOver(bool newBool)
    {
        waveOver = newBool;
    }

    public void SetThingName(string newName)
    {
        thingName = newName;
    }

    public void SetThingCleared(bool newBool)
    {
        thingCleared = newBool;
    }

    public string GetThingName()
    {
        return thingName;
    }

    void ThingYell()
    {
        bearCanvas.enabled = true;
        bearText.text = "Oi! Did you remember to tie down the " + thingName + "!?";
        textWaiting = true;
    }

    void LoseYell()
    {
        bearText.text = "YOU DIDN'T TIE IT DOWN! YOU'RE FIRED!!!";
        gameOver = true;
        //angry anim
    }

    void RoundWinYell()
    {
        bearText.text = "Ok fine. But you better not mess up tomorrow!";
        //anim
    }

    void GameWinYell()
    {
        bearText.text = "... That was fine... I guess.";
        //bearCanvas.enabled = false;
        gameOver = true;
        //disappointed anim
    }


    public void Sequence()
    {
        sequenceStarted = true;
        //disable player script
        player.GetComponent<Rigidbody>().velocity = Vector3.zero;
        player.enabled = false;
        //bust open door with animation
        //Determine what bear wants tied down i.e. thing name
        //Yell at player using speech bubble UI
        ThingYell();
        //wait for user interaction to push forward dialogue
        //test if thing is cleared
        //if cleared, yell at player and game over
        //else yell at player but with a warning
        //wait for user interaction to push forward dialogue
        //show game over screen if game over
        //else if completed final round player win
        //else continue to next round - call init func, re-enable player script

        waveOver = false;
        sequenceStarted = false;
    }

    public bool GetSequenceStarted()
    {
        return sequenceStarted;
    }

    // Update is called once per frame
    void Update ()
    {
        if (gameOver == false)
        {
            if (textWaiting == true)
            {
                if (Input.GetButtonDown("Use"))
                {
                    Debug.Log("Button Pressed");
                    Debug.Log("Thing Cleared: " + thingCleared);
                    if (thingCleared == true)
                    {
                        LoseYell();
                    }
                    else if (game.waveNo < (game.waveTimes.Length - 1))
                    {
                        RoundWinYell();
                    }
                    else //win
                    {
                        GameWinYell();
                    }

                    textWaiting = false;
                    textWaiting2 = true;
                    Debug.Log("Text Waiting: " + textWaiting);
                    Debug.Log("Text Waiting2: " + textWaiting2);
                }
            }
            else if (textWaiting2 == true)
            {
                if (Input.GetButtonDown("Use"))
                {
                    textWaiting2 = false;
                    bearCanvas.enabled = false;
                    game.isWaiting = false;
                    player.enabled = true;
                    game.speedUpMusic();
                }
            }
        }
        else
        {
            //show lose screen
            game.LoseScreen();
            Init();
        }
	}
}
