/**
 * Skript ma na starost zobrazeni pohledu shora na scenu (tzv. mini mapy), nebo zobrazeni detailniho pohledu na vybrany objekt.
 */
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
/**
 * Author:			Karel Zavrel
 * 
 * Thesis:			An interactive application for development of creativity
 * Application:		Little Builder
 */
public class DetailOfPartRotationScript : MonoBehaviour {
	/**
	 * promenne:
	 * 	- panel s mini mapou
	 * 	- panel reprezentujici paletu barev
	 * 	- objekt pro posun palety barev ve scene
	 * 	- prvek GUI obrazek, kam je promitnut udaj z minimapCamery
	 * 	- prvek GUI obrazek, kam je promitnut udaj z detailCamery
	 * 	- kamery: kamera pro pohled shora, kamera pro detailni pohled na objekt
	 * 	- pocatecni pozice kamery pro pohled shora
	 * 	- pocatecni velikost zorneho pole kamery pro pohled shora
	 * 	- podminka zda bylo stisknuto priblizeni kamery pro mini mapu
	 * 	- podminka zda bylo stisknuto oddaleni kamery pro mini mapu
	 * 	- detailni objekt, kam je docasne vytvorena instance vybrane kosticky, pro jeji detailni zobrazeni uzivateli jeste pred samotnym vyberem
	 * 	- nadrazeny prvek pro detailni objekt
	 * 	- jmeno objektu pro zobrazeni jeho detailu
	 * 	- rychlost otaceni daneho objektu
	 */
	private GameObject minimapPanel;
	private GameObject colorPalettePanel;
	private RectTransform colorPalettePanelTransform;
	private RawImage minimapRawImage;
	private RawImage detailRawImage;
	private Camera minimapCamera, detailCamera;
	private Vector3 startingMinimapCamsPosition;
	private float startingMinimapCamsSize;
	private bool plusMinimapPressed = false, minusMinimapPressed = false;
	private GameObject detailObject, detailObjectParent;
	private string nameOfObject;
	private float rotationSpeed = 20.0f;
	/**
	 * funkce pro inicializaci promennych
	 */
	void Start () {
		detailCamera = GameObject.Find("DetailCamera").GetComponent<Camera>();
		minimapCamera = GameObject.Find("MinimapCamera").GetComponent<Camera>();
		startingMinimapCamsPosition = minimapCamera.transform.position;
		startingMinimapCamsSize = minimapCamera.orthographicSize;
		minimapPanel = GameObject.Find ("MinimapPanel");
		detailRawImage = GameObject.Find ("DetailRawImage").GetComponent<RawImage> ();
		detailRawImage.enabled = false;
		minimapRawImage = GameObject.Find ("MinimapRawImage").GetComponent<RawImage> ();
		minimapPanel.SetActive (false);
		colorPalettePanel = GameObject.Find ("ColorPalettePanel");
		colorPalettePanelTransform = colorPalettePanel.GetComponent<RectTransform> ();
		colorPalettePanel.SetActive (false);
		
		detailObjectParent = GameObject.Find ("DetailObjectParent");
		detailObject = null;
	}
	/**
	 * funkce pro nastaveni pozice palety barev
	 * 	-	pokud je aktivni okno s minimapou, pak se pozice palety barev soupne vedle minimapy, jinak k levemu kraji
	 */
	private void setPositionOfColorPalette(){
		if (minimapPanel.activeSelf) {
			colorPalettePanelTransform.anchoredPosition = new Vector2(-228.0f, 368.0f);
		} else {
			colorPalettePanelTransform.anchoredPosition = new Vector2(-740.0f, 368.0f);
		}
	}
	/**
	 * funkce volana jednou pro kazdy frame
	 * 	- pokud neni je zobrazeno okno pro detail objektu, vytvori jeho instanci a spusti jeho otaceni
	 * 	- prubezne kontroluje, zda nedochazi k posunu kosticky ve scene
	 * 	- pokud dochazi k posunu kosticky ve scene, promita do okna pohled shora
	 * 		- zaistuje i pohyb kamery spolu s vkladanym objektem a jeji priblizovani a oddalovani
	 */
	void Update () {
		if(detailRawImage.enabled){
			if (InnerStoringScript.getButtonForDetailObject () != "") {
				setDetailObjectChild (InnerStoringScript.getButtonForDetailObject ());
			}
			detailObjectParent.transform.Rotate (detailObjectParent.transform.up, rotationSpeed * Time.deltaTime);
		}
		checkMinimapImageVisibility();
		if (minimapRawImage.enabled && (InnerStoringScript.getNewlyInsertedBrick() != null)) {
			minimapCamera.transform.position = new Vector3 (InnerStoringScript.getNewlyInsertedBrick ().transform.position.x, 
				35.0f, InnerStoringScript.getNewlyInsertedBrick ().transform.position.z);
			if (minusMinimapPressed || plusMinimapPressed) {
				lerpSizeMinimapCamera ();
			}
		}
	}
	/**
	 * funkce pro kontrolu, zda je videt minimapa nebo detail objektu
	 */
	private void checkMinimapImageVisibility(){
		if (InnerStoringScript.getIsMinimapVisible () && !minimapRawImage.enabled) {
			detailRawImage.enabled = false;
			minimapRawImage.enabled = true;
		} else if (!InnerStoringScript.getIsMinimapVisible () && minimapRawImage.enabled) {
			detailRawImage.enabled = true;
			minimapRawImage.enabled = false;
			minimapCamera.transform.position = startingMinimapCamsPosition;
		}
	}
	/**
	 * funkce pro vytvoreni instance pro detailni pohled na dany objekt
	 * 	- z jmena tlacitka na stredu posuvneho panelu vytvori nazev kosticky
	 * 	- pokud existuje objekt, jehoz nazev neni totozny s vytvorenym, tak je znicen
	 *  - vytvori instanci objektu pro detailni pohled a prepocita velikost kamery
	 */
	private void setDetailObjectChild(string buttonName){
		if(!buttonName.Contains("Category")){
			string[] fourParts = buttonName.Split(new char[]{'_'}, 4); 			// Bricks_1_Button_2	-> Bricks, 1, Button, 2
			nameOfObject = fourParts[0].Substring(0, fourParts[0].Length - 1);	// Bricks 				-> Brick
			nameOfObject = nameOfObject + "_" + fourParts[3] + "x" + fourParts[1];
			if (nameOfObject == "BevelledCornerBrick_3x2") {
				nameOfObject = "BevelledCornerBrick_3x3";
			} else if (nameOfObject == "BevelledCornerBrick_4x2") {
				nameOfObject = "BevelledCornerBrick_4x4";
			}
			string path = "Models/" + fourParts[0] + "_" + fourParts[1] + "/BrickPrefabs/" + nameOfObject;
			if ((detailObject != null) && (detailObject.name != (nameOfObject + "(Clone)"))) {
				Destroy (detailObject);
			}
			/**
			 * pokud objekt neexistuje - tak jej vytvorim
			 * nebo
			 * pokud existuje, ale nesedi jeho jmeno se jmenem vytvorenym z nejblizsiho tlacitka
			 */
			if ((detailObject == null) || ((detailObject != null) && (detailObject.name != (nameOfObject + "(Clone)")))) {
				detailObject = (GameObject)Instantiate (Resources.Load (path), detailObjectParent.transform.position, detailObjectParent.transform.rotation, detailObjectParent.transform);
				detailObject.transform.localScale = new Vector3 (99.99f, 99.99f, 99.99f);
				//setMaterialToDetailObject ();
				/**
				 * ortographic scale = (1 ; 4) .. tedy rozmezi 3 a to (3 / 16) = 0.1875
				 * pak velikost pro kazdou kosticku se prepocita jako 1 + (pocet spuntiku * 0.1875)
				 */
				detailCamera.orthographicSize = 1.0f + (float.Parse (fourParts [3]) * 0.1875f);
			}
		}
	}
	/**
	 * funkce pro nastaveni materialu detailni kosticce
	 */
	private void setMaterialToDetailObject(){
		Transform[] allChildren = detailObject.GetComponentsInChildren<Transform>();
		for (int i = 1; i < allChildren.Length; i++) {
			allChildren [i].gameObject.GetComponent<MeshRenderer> ().material = Resources.Load ("Models/Materials/YellowColor") as Material;
		}
	}
	/**
	 * funkce pro stisk tlacitka HOME
	 */
	public void clickOnMinimapHomeButton(){
		minimapCamera.transform.position = startingMinimapCamsPosition;
	}
	/**
	 * funkce pro nastaveni plusMinimapPressed na true
	 */
	public void pressPlusMinimapButton(){
		if (!minusMinimapPressed) {
			plusMinimapPressed = true;
		}
	}
	/**
	 * funkce pro nastaveni plusMinimapPressed na false
	 */
	public void releasePlusMinimapButton(){
		if (!minusMinimapPressed) {
			plusMinimapPressed = false;
		}
	}
	/**
	 * funkce pro nastaveni minusMinimapPressed na true
	 */
	public void pressMinusMinimapButton(){
		if (!plusMinimapPressed) {
			minusMinimapPressed = true;
		}
	}
	/**
	 * funkce pro nastaveni minusMinimapPressed na false
	 */
	public void releaseMinusMinimapButton(){
		if (!plusMinimapPressed) {
			minusMinimapPressed = false;
		}
	}
	/**
	 * funkce pro priblizovani a oddalovani minimap kamery
	 */
	private void lerpSizeMinimapCamera(){
		float size = minimapCamera.orthographicSize;
		if (plusMinimapPressed) {
			size = Mathf.Lerp (minimapCamera.orthographicSize, 0.1f, Time.deltaTime * 2.0f);
		} else if (minusMinimapPressed) {
			size = Mathf.Lerp (minimapCamera.orthographicSize, 20.0f, Time.deltaTime * 2.0f);
		}
		minimapCamera.orthographicSize = size;
	}
	/**
	 * funkce vrati kameru minimapy do puvodni velikosti
	 */
	public void pressHomeMinimapButton(){
		minimapCamera.orthographicSize = startingMinimapCamsSize;
	}
	/**
	 * funkce pro skryti a odkryti minimapy a detailniho panelu
	 */
	public void showOrHideMinimapPanel(){
		minimapPanel.SetActive (!minimapPanel.activeSelf);
		if (colorPalettePanel.activeSelf) {
			setPositionOfColorPalette();
		}
	}
	/**
	 * funkce pro skryti a odkryti palety barev
	 */
	public void showOrHideColorPalettePanel(){
		setPositionOfColorPalette ();
		colorPalettePanel.SetActive (!colorPalettePanel.activeSelf);
	}
}