/**
 * Skript ma na starost ukladani dat o modelu do textoveho souboru a jejich nacitani ze souboru do seznamu retezcu.
 */
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
/**
 * Author:			Karel Zavrel
 * 
 * Thesis:			An interactive application for development of creativity
 * Application:		Little Builder
 */
public class ModelSaveLoadScript{
	/**
	 * instance tridy jako jedina promenna - vzor Singleton
	 */
	private static ModelSaveLoadScript instance;
	/**
	 * promenne:
	 * 	- cesta k ulozeni souboru
	 * 	- jmeno souboru
	 * 	- pole vsech jmen souboru
	 * 	- seznam vsech kosticek v modelu
	 * 	- textove pole pro zpravy uzivateli
	 */
	private string pathToSaveFiles;
	private string fileName;
	private FileInfo[] allFilesNames;
	private List<GameObject> allBricks;
	private GameObject editorLogPanel;
	private Text editorLogText;
	/**
	 * soukromy konstruktor
	 */
	private ModelSaveLoadScript(){}
	/**
	 * funkce pro ziskani instance teto tridy
	 * 	- pokud jiz instance existuje, bude vracena
	 * 	- jinak vyuzije soukromy konstruktor k jeji tvorbe a tu preda
	 */
	public static ModelSaveLoadScript getInstance(){
		if (instance == null) {
			instance = new ModelSaveLoadScript ();
		}
		return instance;
	}
	/**
	 * setter pro promennou fileName
	 */
	public void setFileName(string name){
		fileName = name;
	}
	/**
	 * getter pro promennou fileName
	 */
	public string getFileName(){
		return fileName;
	}
	/**
	 * getter pro promennou pathToSaveFiles
	 */
	public string getPathToSaveFiles(){
		return pathToSaveFiles;
	}
	/**
	 * setter pro promennou pathToSaveFiles
	 */
	public void setPathToSaveFiles(string path){
		pathToSaveFiles = path;
	}
	/**
	 * funkce overi, zda zadany soubor jiz existuje
	 */
	public bool fileExists(){
		return File.Exists(pathToSaveFiles + fileName);
	}
	/**
	 * funkce pro ulozeni dat do souboru
	 */
	public bool saveData(){
		return saveDataToTxtFile();
	}
	/**
	 * funkce pro ulozeni textovych dat o modelu do souboru
	 * 	- format dat:
	 * 		- gameobject.name#gameobject.position#gameobject.rotation#gameobject.material
	 * 	
	 * 	- data jsou brana ze seznamu kosticek modelu z tridy InnerStoringScript a jsou ukladana od druheho prvku daneho seznamu
	 * 	-	(root, emptyobject neni zahrnut)
	 */
	private bool saveDataToTxtFile(){
		allBricks = InnerStoringScript.getAllBricksInModel();
		if (allBricks.Count > 1) {
			StreamWriter sw = new StreamWriter (pathToSaveFiles + fileName, false);
			for (int i = 1; i < allBricks.Count; i++) {
				string line = allBricks [i].name + "#"
				    + allBricks [i].transform.position.ToString ("F10") + "#"
				    + allBricks [i].transform.rotation.ToString ("F10") + "#"
				    + allBricks [i].GetComponentsInChildren<Transform> () [1].gameObject.GetComponent<MeshRenderer> ().material.name;
				// musim potomka od 1 dal, protoze 0 reprezentuje groupovaci objekt z mayi a ja prenastavuju az podobjekty
				sw.WriteLine (line);
			}
			sw.Close ();
			return true;
		} else {
			return false;
		}
	}
	/**
	 * funkce pro nacteni dat o modelu
	 */
	public List<string> loadData(){
		return loadDataFromTxtFile();
	}
	/**
	 * funkce pro cteni souboru, kazdy radek ulozi do listu jako novy prvek
	 * 	-	radky maji tvar:
	 * 		jmeno#pozice#otoceni#material (instance)
	 */
	private List<string> loadDataFromTxtFile(){
		string dir = pathToSaveFiles.Substring (0, pathToSaveFiles.Length - 1);
		if (Directory.Exists (dir) && File.Exists (pathToSaveFiles + fileName)) {
			StreamReader sr = new StreamReader (pathToSaveFiles + fileName);
			string line = "";
			List<string> lines = new List<string> ();
			while ((line = sr.ReadLine ()) != null) {
				lines.Add (line);
			}
			sr.Close ();
			return lines;
		} else {
			return null;
		}
	}
	/**
	 * funkce pro precteni modelu z galerie
	 */
	public List<string> loadDataFromGallery(){
		TextAsset ta = Resources.Load("GalleryModels/" + fileName) as TextAsset;
		if (ta != null) {
			StringReader sr = new StringReader (ta.text);
			string line = "";
			List<string> lines = new List<string> ();
			while ((line = sr.ReadLine ()) != null) {
				lines.Add (line);
			}
			sr.Close ();
			return lines;
		} else {
			return null;
		}
	}
	/**
	 * getter pro promennou allFilesNames
	 */
	public FileInfo[] getAllFilesNames(){
		return getAllFilesInDirectory();
	}
	/**
	 * funkce pro ziskani pole informaci o vsech souborech v danem adresari
	 */
	private FileInfo[] getAllFilesInDirectory(){
		DirectoryInfo di = new DirectoryInfo(pathToSaveFiles);
		if (di.Exists) {
			allFilesNames = di.GetFiles ();
			return allFilesNames;
		}
		return null;
	}
	/**
	 * setter pro promennou editorLogPanel
	 */
	public void setEditorLogLabelPanel(GameObject panel){
		editorLogPanel = panel;
	}
	/**
	 * getter pro promennou editorLogPanel
	 */
	public GameObject getEditorLogLabelPanel(){
		return editorLogPanel;
	}
	/**
	 * setter pro promennou editorLogText
	 */
	public void setEditorLogLabelText(Text text){
		editorLogText = text;
	}
	/**
	 * getter pro promennou editorLogText
	 */
	public Text getEditorLogLabelText(){
		return editorLogText;
	}
}