﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using Baidu.VR.LitJson;

namespace Baidu.VR.DepthpanoEditor
{
	public class Vector3Info
	{
		public double x, y, z;

		public Vector3Info() { }

		public Vector3Info(double _x, double _y, double _z)
		{
			x = _x; y = _y; z = _z;
		}

		public Vector3 GetVector3()
		{
			return new Vector3((float)x, (float)y, (float)z);
		}

		public static Vector3Info FromVector3(Vector3 v3)
		{
			return new Vector3Info(v3.x, v3.y, v3.z);
		}
	}

	public class QuaternionInfo
	{
		public double x, y, z, w;

		public QuaternionInfo() { }

		public QuaternionInfo(double _x, double _y, double _z, double _w)
		{
			x = _x; y = _y; z = _z; w = _w;
		}

		public Quaternion GetQuaternion()
		{
			return new Quaternion((float)x, (float)y, (float)z, (float)w);
		}

		public static QuaternionInfo FromQuaternion(Quaternion q)
		{
			return new QuaternionInfo(q.x, q.y, q.z, q.w);
		}
	}

	public class ModelInfo
	{
		public string source;
	}

    public class ModelInfoWithMat : ModelInfo
    {
        public string material;
    }

    public class TransformInfo
	{
		public Vector3Info position;
	}

	public class ImageInfo
	{
		public string type;
		public string cube_file;
	}

	public class DepthpanoInfo
	{
		public string name;
		public TransformInfo transform;
		public ImageInfo image;
	}

    public class DepthpanoGroupInfo
    {
        public string id;
        public string name;
        public ModelInfo model;
        public DepthpanoInfo[] children;
    }

    public class DepthpanoConfig
	{
		public string version;
		public string type;
		public DepthpanoGroupInfo[] objects;
	}

    public class DepthpanoEditorGroupInfo
    {
        public string name;
        public DepthpanoCameraInfo[] cameras;
    }

    public class DepthpanoCameraInfo
	{
		public string cameraName;
		public Vector3Info cameraPos;
	}

	public class DepthpanoEditorConfig
	{
        public string version = "2.0.0";
		public string scenePath = null;
        public string highModel = null;
        public string lowModel = null;
		public DepthpanoEditorGroupInfo[] groups = null;

		public static DepthpanoEditorConfig GetConfigByFilePath(string configPath)
		{
			string newConfigPath = configPath.StartsWith("file://") ? configPath.Replace("file://", "") : configPath;
			if (!File.Exists(newConfigPath))
			{
				Debug.LogError("GetConfigByFilePath Parse failed! Path not exist! Path:" + newConfigPath);
				return null;
			}

			string info = File.ReadAllText(newConfigPath);
			try
			{
                DepthpanoEditorConfig ret = null;
                JsonData jd = JsonMapper.ToObject(info);

                if (jd.Keys.Contains("version"))
                {
                    ret = JsonMapper.ToObject<DepthpanoEditorConfig>(info);
                }
                else
                {
                    ret = new DepthpanoEditorConfig();
                    ret.scenePath = (string)jd["scenePath"];
                    ret.groups = new DepthpanoEditorGroupInfo[1];
                    ret.groups[0] = new DepthpanoEditorGroupInfo();
                    ret.groups[0].name = "group1";
                    ret.groups[0].cameras = JsonMapper.ToObject<DepthpanoCameraInfo[]>(jd["cameras"].ToJson());
                }

                return ret;
			}
			catch
			{
				Debug.LogError("Get Config Failed");
				return null;
			}
		}

		public static bool SaveConfigToPath(DepthpanoEditorConfig config, string configPath)
		{
			string newConfigPath = configPath.StartsWith("file://") ? configPath.Replace("file://", "") : configPath;
			try
			{
				File.WriteAllText(newConfigPath, JsonMapper.ToJson(config));
				return true;
			}
			catch
			{
				Debug.LogError("Save Config Failed");
				return false;
			}
		}
	}
}