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

public class GameScript : MonoBehaviour {

	public Image Water;
	float depth = 95;
	public GameObject gameOver, gameWin;
	public Text resText, winTxt, loseTxt, startText;
	string restart = "restart", start = "start";
	char letter;
	int letterNumber;
	public bool GAMEOVER, GAMESTARTED;
	AudioSource GameOverSound, GameWinSound, MusicSound, LlamaSound;
	Camera cam;
	public bool introMode = true;
	public Sprite[] fishes;
	public AudioClip[] fishSounds;
	public Animation fishAnim;
	Text depthGuage;
	GameObject seaFloor;
	Animator seaFloorWindow;
	// Use this for initialization
	void Start () {
		seaFloorWindow = GameObject.Find("SeaFloorWindow").GetComponent<Animator>();
		seaFloor = GameObject.Find("SeaFloor");
		depthGuage = GameObject.Find("DEPTH").GetComponent<Text>();
		cam = FindObjectOfType<Camera>();
		letter = restart[0];
		Sink();
		GameOverSound = transform.Find("GameOver").GetComponent<AudioSource>();
		GameWinSound = transform.Find("GameWin").GetComponent<AudioSource>();
	}

	public void Rise(int amount = 5) {
		//print("Fish:"+(int)Math.Round(depth/10,1));
		GameObject.Find("Fish").GetComponent<Image>().sprite = fishes[(int)Math.Round(depth/10,1)-1];
		GameObject.Find("Fish").GetComponent<AudioSource>().clip = fishSounds[(int)Math.Round(depth/10,1)-1];
		GameObject.Find("Fish").GetComponent<AudioSource>().Play();
		if ((int)Math.Round(depth/10,1)-1 == 9) {
			GameObject.Find("Fish").GetComponent<Animator>().SetBool("Big",true);
		} else {
			GameObject.Find("Fish").GetComponent<Animator>().SetBool("Big",false);
		}
		depth -= amount; //Rise speed is decided in TypingHandler!
	}

	public void Sink() {
		if (depth < 95)
			depth += 0.5f; //Sink speed, higher numbers sink faster.
		Invoke("Sink",2);
	}

	public void GameOver() {
		if (!GAMEOVER) {
			print("Game Over");
			letterNumber = 0;
			GameOverSound.Play();
			GAMEOVER = true;
			gameOver.SetActive(true);
			resText = loseTxt;
		}
	}

	public void Win() {
		if (!GAMEOVER) {
			letterNumber = 0;
			GameWinSound.Play();
			GAMEOVER = true;
			gameWin.SetActive(true);
			resText = winTxt;
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (!GAMESTARTED) {
			if (letterNumber < start.Length) {
				if (Input.inputString == start[letterNumber].ToString()) {
					letterNumber++;
					startText.text += Input.inputString;
				}
			} else {
				GameObject.Find("StartScreen").SetActive(false);
				GAMESTARTED = true;
				FindObjectOfType<TypingHandler>().FirstTimeInit();
				letterNumber = 0;
			}
		} else if (GAMEOVER && Input.anyKeyDown) {
			if (letterNumber < restart.Length) {
				if (Input.inputString == restart[letterNumber].ToString()) {
					letterNumber++;
					resText.text += Input.inputString;
				}
			} else {
				SceneManager.LoadScene(SceneManager.GetActiveScene().name);
			}
		} else if (!GAMEOVER && GAMESTARTED) {
			depthGuage.text = depth+" FT";
			depthGuage.color = new Color((depth/100),1-(depth/100),0);
			seaFloorWindow.SetFloat("Depth",depth);
			if (depth > 94)
				seaFloor.SetActive(true);
			else
				seaFloor.SetActive(false);
			cam.backgroundColor = Color.Lerp(cam.backgroundColor, new Color(0,(1-0.01f*depth),(1-0.0075f*depth)),1);
		}
	}
}
