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

public class MonsterManager : MonoBehaviour {
	
	public GameObject inventory;
	public Item item;

	public Inventory inv;

	void Start() 
	{
		loadMonster();
	}

	public void showItems()
	{
		if (inventory.activeSelf == false)
			inventory.SetActive (true);
		else inventory.SetActive (false);
	}

	public void saveMonster()
	{
		GameObject[] monsterParts = GameObject.FindGameObjectsWithTag("MonsterPart");

		StringBuilder pos = new StringBuilder ();
		string savedString;
		string equippedString = "";

		for (int i = 0; i < monsterParts.Length; i++) {
			pos = new StringBuilder ();
			pos.Append(GameObject.Find(monsterParts[i].name).transform.position.x).Append(" ").Append(GameObject.Find(monsterParts[i].name).transform.position.y).Append(" ").Append(GameObject.Find(monsterParts[i].name).transform.position.z);
			equippedString = equippedString + monsterParts [i].name + " ";
			savedString = pos.ToString ();
			PlayerPrefs.SetString(/*SceneManager.GetActiveScene().name + */monsterParts[i].name, savedString);
		}
		PlayerPrefs.SetString ("Equipped", equippedString);
		/*
		StringBuilder eyePos = new StringBuilder();

		eyePos.Append(GameObject.Find("circle_eye").transform.position.x).Append(" ").Append(GameObject.Find("circle_eye").transform.position.y).Append(" ").Append(GameObject.Find("circle_eye").transform.position.z);
		string savedString = eyePos.ToString();
		PlayerPrefs.SetString(SceneManager.GetActiveScene().name + "eyePosition", savedString);

		StringBuilder hairPos = new StringBuilder();

		hairPos.Append(GameObject.Find("crazy_hair").transform.position.x).Append(" ").Append(GameObject.Find("crazy_hair").transform.position.y).Append(" ").Append(GameObject.Find("crazy_hair").transform.position.z);
		savedString = hairPos.ToString();
		PlayerPrefs.SetString(SceneManager.GetActiveScene().name + "hairPosition", savedString);

		StringBuilder headPos = new StringBuilder();

		headPos.Append(GameObject.Find("circle_head").transform.position.x).Append(" ").Append(GameObject.Find("circle_head").transform.position.y).Append(" ").Append(GameObject.Find("circle_head").transform.position.z);
		savedString = headPos.ToString();
		PlayerPrefs.SetString(SceneManager.GetActiveScene().name + "headPosition", savedString);

		StringBuilder hatPos = new StringBuilder();

		hatPos.Append(GameObject.Find("crown").transform.position.x).Append(" ").Append(GameObject.Find("crown").transform.position.y).Append(" ").Append(GameObject.Find("crown").transform.position.z);
		savedString = hatPos.ToString();
		PlayerPrefs.SetString(SceneManager.GetActiveScene().name + "hatPosition", savedString);

		StringBuilder mouthPos = new StringBuilder();

		mouthPos.Append(GameObject.Find("gap_mouth").transform.position.x).Append(" ").Append(GameObject.Find("gap_mouth").transform.position.y).Append(" ").Append(GameObject.Find("gap_mouth").transform.position.z);
		savedString = mouthPos.ToString();
		PlayerPrefs.SetString(SceneManager.GetActiveScene().name + "mouthPosition", savedString);

		StringBuilder nosePos = new StringBuilder();

		nosePos.Append(GameObject.Find("pig_nose").transform.position.x).Append(" ").Append(GameObject.Find("pig_nose").transform.position.y).Append(" ").Append(GameObject.Find("pig_nose").transform.position.z);
		savedString = nosePos.ToString();
		PlayerPrefs.SetString(SceneManager.GetActiveScene().name + "nosePosition", savedString);
		*/
	}

	public void loadMonster () 
	{
		
		string equippedString = PlayerPrefs.GetString("Equipped");
		string[] equipped = equippedString.Split (' ');

		for (int i = 0; i < equipped.Length - 1; i++) {
			string savedString = PlayerPrefs.GetString(/*SceneManager.GetActiveScene().name + */equipped[i]);
			string[] values = savedString.Split ();
			GameObject instance = (GameObject)Instantiate(Resources.Load(equipped[i]));
			instance.name = equipped [i];
			instance.GetComponent<DragAndDrop> ().inventory = inv;
			instance.transform.position = new Vector3(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]));
		}

		/*
		string savedString = PlayerPrefs.GetString(SceneManager.GetActiveScene().name + "eyePosition");
		string[] values = savedString.Split(' ');

		GameObject.Find("circle_eye").transform.position = new Vector3(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]));

		savedString = PlayerPrefs.GetString(SceneManager.GetActiveScene().name + "hairPosition");
		values = savedString.Split(' ');

		GameObject.Find("crazy_hair").transform.position = new Vector3(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]));

		savedString = PlayerPrefs.GetString(SceneManager.GetActiveScene().name + "headPosition");
		values = savedString.Split(' ');

		GameObject.Find("circle_head").transform.position = new Vector3(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]));
	
		savedString = PlayerPrefs.GetString(SceneManager.GetActiveScene().name + "hatPosition");
		values = savedString.Split(' ');

		GameObject.Find("crown").transform.position = new Vector3(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]));

		savedString = PlayerPrefs.GetString(SceneManager.GetActiveScene().name + "mouthPosition");
		values = savedString.Split(' ');

		GameObject.Find("gap_mouth").transform.position = new Vector3(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]));

		savedString = PlayerPrefs.GetString(SceneManager.GetActiveScene().name + "nosePosition");
		values = savedString.Split(' ');

		GameObject.Find("pig_nose").transform.position = new Vector3(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]));
		*/
	}
}
