
using UnityEngine;
using CompassNavigatorPro;
using System.Collections.Generic;

namespace ORKFramework.CompassNavigatorPro
{
	public class Settings : CorePlugin, ISaveData
	{
		[ORKEditorHelp("Save POIs", "Save the state of points of interest in save games.", "")]
		[ORKEditorInfo("Compass Navigator Pro Settings", "The settings for Compass Navigator Pro integration.", "")]
		public bool savePOIs = false;
		
		[ORKEditorHelp("Hide During Scene Change", "Hide the Compass Navigator Pro HUD while changing scenes.", "")]
		[ORKEditorInfo(separator=true)]
		public bool hideDuringSceneChange = false;

		[ORKEditorHelp("Fade Out Duration", "The time in seconds used to fade out the HUD.", "")]
		[ORKEditorInfo(indent=true)]
		[ORKEditorLimit(0.0f, false)]
		[ORKEditorLayout("hideDuringSceneChange", true)]
		public float sceneChangeFadeOutDuration = 1.0f;

		[ORKEditorHelp("Fade In Duration", "The time in seconds used to fade in the HUD.", "")]
		[ORKEditorInfo(indent=true, endFoldout=true)]
		[ORKEditorLimit(0.0f, false)]
		[ORKEditorLayout(endCheckGroup=true)]
		public float sceneChangeFadeInDuration = 1.0f;


		// in-game
		private Dictionary<int, bool> poiState = new Dictionary<int, bool>();

		private CompassPro compassProInstance;


		/*
		============================================================================
		Instance functions
		============================================================================
		*/
		private static Settings instance;

		public Settings()
		{

		}

		public static Settings Instance
		{
			get
			{
				if(instance == null)
				{
					instance = ORK.Plugins.Get(typeof(Settings)) as Settings;
				}
				return instance;
			}
		}

		public override string Version
		{
			get { return "1.0.0"; }
		}


		/*
		============================================================================
		ORK Framework callback functions
		============================================================================
		*/
		public override void OnInitializeORK()
		{
			// check compass pro instance
			this.CheckCompassProInstance();

			// register save game
			if(this.savePOIs)
			{
				ORK.Core.Tick += this.RegisterSaveTick;
			}

			// register scene change
			if(this.hideDuringSceneChange)
			{
				ORK.Control.SceneChange += this.ChangingScene;
			}
		}

		public override void OnNewGame()
		{
			this.poiState.Clear();
		}

		public override void OnLoadGame()
		{
			this.poiState.Clear();
		}

		public override void Tick()
		{

		}

		public override void GUITick()
		{

		}

		public override void SceneLoaded()
		{
			// check compass pro instance
			this.CheckCompassProInstance();

			// set POI states
			CompassProPOI[] poi = GameObject.FindObjectsOfType<CompassProPOI>();
			if(poi != null)
			{
				for(int i = 0; i < poi.Length; i++)
				{
					if(poi[i] != null &&
						this.poiState.ContainsKey(poi[i].id))
					{
						poi[i].isVisited = this.poiState[poi[i].id];
					}
				}
			}
		}

		public override bool Call(string info)
		{
			return false;
		}


		/*
		============================================================================
		Special callback functions
		============================================================================
		*/
		public void RegisterSaveTick()
		{
			if(this.savePOIs &&
				ORK.Instantiated)
			{
				ORK.SaveGame.RegisterCustomData("CompassNavigatorPro", this, true);
				ORK.Core.Tick -= this.RegisterSaveTick;
			}
		}

		public void CheckCompassProInstance()
		{
			if(this.compassProInstance == null)
			{
				this.compassProInstance = CompassPro.instance;
				if(this.compassProInstance != null)
				{
					this.compassProInstance.OnPOIVisited += this.StorePOIState;
				}
			}
		}

		public void ChangingScene(bool start)
		{
			if(this.hideDuringSceneChange &&
				this.compassProInstance != null)
			{
				if(start)
				{
					this.compassProInstance.FadeOut(this.sceneChangeFadeOutDuration);
				}
				else
				{
					this.compassProInstance.FadeIn(this.sceneChangeFadeInDuration);
				}
			}
		}


		/*
		============================================================================
		POI functions
		============================================================================
		*/
		public void StorePOIState(CompassProPOI poi)
		{
			if(this.poiState.ContainsKey(poi.id))
			{
				this.poiState[poi.id] = poi.isVisited;
			}
			else
			{
				this.poiState.Add(poi.id, poi.isVisited);
			}
		}

		public void VisitPOI(CompassProPOI poi, bool visit)
		{
			poi.isVisited = visit;
			this.StorePOIState(poi);
		}

		public void ClearPOIStates()
		{
			this.poiState.Clear();
		}


		/*
		============================================================================
		Save game functions
		============================================================================
		*/
		public ORKFramework.DataObject SaveGame()
		{
			DataObject data = new DataObject();

			foreach(KeyValuePair<int, bool> pair in this.poiState)
			{
				data.Set(pair.Key.ToString(), pair.Value);
			}

			return data;
		}

		public void LoadGame(ORKFramework.DataObject data)
		{
			this.poiState.Clear();

			if(data != null)
			{
				Dictionary<string, bool> states = data.GetData<bool>(typeof(bool));
				if(states != null)
				{
					int tmpID = 0;
					foreach(KeyValuePair<string, bool> pair in states)
					{
						if(int.TryParse(pair.Key, out tmpID))
						{
							this.poiState.Add(tmpID, pair.Value);
						}
					}
				}
			}
		}
	}
}
