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

public class Player : MonoBehaviour {

	public static Player Instance;
	public Texture LifeBar1;
	public Texture LifeBar2;
	public Texture Background;
	public Texture Title;
	Camera Cam;
	UnityStandardAssets.Characters.FirstPerson.FirstPersonController Controller;
	UnityStandardAssets.ImageEffects.ScreenOverlay Overlay;

	public string SpawnText = "Welcome";
	float SpawnDuration = 5.0f;
	public bool LetSpawnText = false;

	public Pickable InHand;
	Transform HandTarget;

	public float MaxLife = 100.0f;
	public float CurrentLife;

	public float HurtTimer = 0.0f;

	string TargetMessage;

	// Use this for initialization
	void Start () {
		TargetMessage="";
		CurrentLife = MaxLife;
		Instance = this;
		Cam = GetComponentInChildren<Camera>();
		HandTarget = transform.FindChild("HandTarget");
		Controller = GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>();
		Overlay = GetComponentInChildren<UnityStandardAssets.ImageEffects.ScreenOverlay>();
	}
	
	// Update is called once per frame
	void Update () {

		if(UnityStandardAssets.CrossPlatformInput.CrossPlatformInputManager.GetButtonDown("Cancel")) {
			Application.Quit();
		}

		if(LetSpawnText && UnityStandardAssets.CrossPlatformInput.CrossPlatformInputManager.GetButtonDown("Submit")) {
			SceneManager.LoadScene("Level_Intro2");
		}

		if(!LetSpawnText && SpawnDuration>0.0f) {
			SpawnDuration -= Time.deltaTime;
		}

		if(HurtTimer>0.0f) {
			HurtTimer -= Time.deltaTime;
		}
		Overlay.intensity = Mathf.Clamp01(HurtTimer);

		if(LetSpawnText) {
			return;
		}

		if(CurrentLife<=0.0f) {
			Overlay.intensity = 1.0f;
			if(!Controller.IsFixed) {
				Controller.IsFixed = true;
				Controller.transform.position -= Vector3.up * 1.2f;
				Controller.transform.Rotate(-20,0,30);
			}
			if(UnityStandardAssets.CrossPlatformInput.CrossPlatformInputManager.GetButtonDown("Submit")) {
				Scene Act = SceneManager.GetActiveScene();
				SceneManager.LoadScene(Act.name);
			}
		}

		TargetMessage = "";

		bool Fire1 = UnityStandardAssets.CrossPlatformInput.CrossPlatformInputManager.GetButtonDown("Fire1");
		bool Fire1IsDown = UnityStandardAssets.CrossPlatformInput.CrossPlatformInputManager.GetButton("Fire1");

		Cranck SelectedCran = null;
		Pickable SelectedPick = null;
		PushButton SelectedBut = null;
		Socket SelectedSlot = null;

		//RaycastHit[] Hits = Physics.RaycastAll(Cam.transform.position, Cam.transform.forward, 3.0f);
		RaycastHit[] Hits = Physics.SphereCastAll(Cam.transform.position, 0.5f, Cam.transform.forward, 1.4f);
		foreach(var Hit in Hits) {
			PushButton But = Hit.collider.GetComponent<PushButton>();
			Pickable Pick = Hit.collider.GetComponent<Pickable>();
			Cranck Cran = Hit.collider.GetComponent<Cranck>();
			Socket Slot = Hit.collider.GetComponent<Socket>();
			if(But) SelectedBut = But;
			if(Pick) SelectedPick = Pick;
			if(Cran) SelectedCran = Cran;
			if(Slot) SelectedSlot = Slot;
		}

		if(InHand) {
			TargetMessage = SelectedSlot ? "Insert" : "Drop";
			if(Fire1) {
				InHand.Drop(SelectedSlot);
				InHand = null;
			}
		} else {
			if(SelectedBut) {
				if(SelectedBut.AutoStop) {
					TargetMessage = "Recharge";
					if(Fire1) {
						SelectedBut.SetState(true);
					}
				} else {
					TargetMessage = SelectedBut.IsOn ? "Turn off" : "Turn on";
					if(Fire1) {
						SelectedBut.SetState(!SelectedBut.IsOn);
					}
				}
			} else if(SelectedPick) {
				TargetMessage = "Pick";
				if(Fire1) {
					InHand = SelectedPick;
					InHand.Pick();
				}
			} else if(SelectedCran) {
				TargetMessage = "Cranck";
				if(Fire1IsDown) {
					SelectedCran.transform.Rotate(new Vector3(0,Time.deltaTime * 300.0f, 0));
					if(SelectedCran.Slot && SelectedCran.Slot.Slot) {
						Batery Bat = SelectedCran.Slot.Slot.GetComponent<Batery>();
						if(Bat) {
							Bat.Gauge = Mathf.Clamp01(Bat.Gauge + Time.deltaTime / SelectedCran.ChargingDuration);
						}
					}
				}
			} else if(SelectedSlot) {
				TargetMessage = "Missing Battery";
			}
		}

		if(InHand) {
			InHand.transform.position = HandTarget.position;
			InHand.transform.rotation = HandTarget.rotation;
		}
	}


	void OnGUI()
	{
		//GUI.skin.font = TextFont;
		GUIStyle Style = GUI.skin.GetStyle("Label");
		Style.fontSize = 50;
		Style.alignment = TextAnchor.MiddleCenter;

		Style.normal.textColor = Color.white;

		Rect TextLocation = new Rect(Screen.width * 0.5f - 200, Screen.height * 0.5f - 50, 400, 100);
		if (TargetMessage.Length>0)
		{
			GUI.DrawTexture(TextLocation, Background);
			GUI.Label(TextLocation, TargetMessage);
		}

		if(!LetSpawnText) {
			float LifePercent = Mathf.Clamp01(CurrentLife/MaxLife);
			float PX = Screen.width * 0.5f - 100;
			float PY = Screen.height * 0.9f;
			GUI.DrawTexture(new Rect(PX - 2,PY - 2,204,34), LifeBar2);
			GUI.DrawTexture(new Rect(PX,PY,200 * LifePercent,30), LifeBar1);
		}

		if(SpawnDuration>0.0f && SpawnDuration<8.0f) {
			Rect TextLocation2 = new Rect(Screen.width * 0.5f - 200, Screen.height * (LetSpawnText ? 0.75f : 0.25f) - 50, 400, 100);
			GUI.DrawTexture(TextLocation2, Background);
			GUI.Label(TextLocation2, SpawnText);
		}

		if(CurrentLife<=0.0f) {
			Rect TextLocation2 = new Rect(Screen.width * 0.75f - 232, Screen.height * 0.25f - 80, 463, 176);
			GUI.DrawTexture(TextLocation2, Background);
			GUI.Label(TextLocation2, "You died");
		}

		if(LetSpawnText) {
			Rect TextLocation2 = new Rect(Screen.width * 0.75f - 200, Screen.height * 0.25f - 50, 400, 100);
			GUI.DrawTexture(TextLocation2, Title);
		}
	}
}
