﻿#if !UNITY_WP8

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using ProBuilder2.Common;

namespace ProBuilder2.Serialization
{

	/**
	 * Proof of concept for serialization of pb_Objects.
	 */
	public class pb_SaveLoad : Editor
	{
		[MenuItem("Tools/" + pb_Constant.PRODUCT_NAME + "/Experimental/Load ProBuilder Object", false, pb_Constant.MENU_EXPERIMENTAL + 40)]
		public static void MenuLoadPbObject()
		{
			string path = EditorUtility.OpenFilePanel("Load Serialized ProBuilder Object", Application.dataPath, "pbo");

			pb_SerializableObject obj = null;

			BinaryFormatter formatter = new BinaryFormatter();
			Stream stream = File.Open(path, FileMode.Open);

			obj = (pb_SerializableObject)formatter.Deserialize(stream);

			stream.Close();

			Selection.activeTransform = pb_Object.InitWithSerializableObject(obj).transform;
		}

		[MenuItem("Tools/" + pb_Constant.PRODUCT_NAME + "/Experimental/Save ProBuilder Object to File", false, pb_Constant.MENU_EXPERIMENTAL + 40)]
		public static void MenuSavePbObject()
		{
			pb_Object[] selection = pbUtil.GetComponents<pb_Object>(Selection.transforms);
			int len = selection.Length;

			if(len < 1) return;

			string path = "";

			if(len == 1)
				path = EditorUtility.SaveFilePanel("Save ProBuilder Object", "", selection[0].name, "pbo");// "Save ProBuilder Object to File.");
			else
				path = EditorUtility.SaveFolderPanel("Save ProBuilder Objects to Folder", "", "");

			if(path == "" || !path.EndsWith(".pbo"))
				return;

			foreach(pb_Object pb in selection)
			{
				// Creates a new pb_SerializableObject object.
				pb_SerializableObject obj = new pb_SerializableObject(pb);

				// Opens a file and serializes the object into it in binary format.
				Stream stream = File.Open( len == 1 ? path : path + pb.name + ".pbo", FileMode.Create);

				BinaryFormatter formatter = new BinaryFormatter();

				formatter.Serialize(stream, obj);
				
				stream.Close();			
			}
		}
	}
}

#endif