using UnityEngine;
using System.Collections;

public class HungryWolfes : MonoBehaviour
{

	public float Hunger;
	public float grassGunger;
	public float decreaseSpeed;
	public GameObject player;
	public float PlayTime;
	public bool Dead;
	public Texture2D bg;
	public Texture2D fill;
	public float remain = 1f;
	public int SheepsKilled = 0;
	public int wolvesmore;
	public int SheepCounter;
	bool first = true;

	// Use this for initialization
	void Start ()
	{
		PlayTime = 0;
		Dead = false;
	}
	
	// Update is called once per frame
	void Update ()
	{
		if(SheepCounter > 2){
			SheepCounter =0;
			wolvesmore ++;
		}

		gameObject.GetComponent<respawnWolves>().requiredWolf = (7+wolvesmore);

		Hunger -= Time.deltaTime * decreaseSpeed * (1+wolvesmore)/6;

		PlayTime += Time.deltaTime;

		if (Hunger < 0 && !Dead) {
			player.tag = "FreeToKill";
			Hunger = 0;
		} else if (Hunger > 1) {
			Hunger = 1;
			player.tag = "Wolf";
		} else {
			player.tag = "Wolf";

		}
		if(Dead){
			remain -= Time.deltaTime;
		}

		if(remain < 0){
			
			if (first) {
				GameObject.Find ("ScoreHolder").GetComponent<ScoreHolder>().setScore(100*SheepsKilled+((int)PlayTime));
				//SceneManager.LoadScene ("Highscore", SceneManagement.LoadSceneMode.Single);
				Application.LoadLevel ("Highscore");
				first = false;
			}
		
		}

	}

	float map(float s, float a1, float a2, float b1, float b2)
	{
		return b1 + (s-a1)*(b2-b1)/(a2-a1);
	}



	void OnGUI () {
		// Make a background box
		GUI.Box(new Rect(10,10,100,20), (100*SheepsKilled+((int)PlayTime)).ToString());

		int h = Screen.height/5;
		int w = h*bg.width/bg.height;

		int x = 0;
		int y = Screen.height-h;

		float val = map (Hunger,0,1,0.38f,0.81f);

		GUI.BeginGroup( new Rect(x,y,w,h));
		
		// Draw the background image
		if (bg != null)
		{
			GUI.DrawTexture( new Rect(0,0,w,h), bg);
		}
		
		// Create a second Group which will be clipped
		// We want to clip the image and not scale it, which is why we need the second Group
		GUI.BeginGroup( new Rect(val * w,0,(int)((1-val) * w), h));
		
		// Draw the foreground image
		GUI.DrawTexture( new Rect(0-(val*w),0,w,h), fill);
		
		// End both Groups
		GUI.EndGroup();
		GUI.EndGroup();
		
		
	}
}
