using UnityEngine;
using System.Collections;
using System.IO;
using com.deeperbeige.lib.runtime;

// DevPanel system, for convenient debugging on devices
// Initialise by simply using the class! Ideally log something
// to the panel on startup, such as the project version number.
//
// Make the panel appear with any of these shortcuts:
//     Press Ctrl+/
//     Hold the android hardware settings key for a few seconds
//     Hold four fingers on the display for a few seconds
//
// Usage:
//     DevPanel.Log("This code ran!");
//     DevPanel.Log(myObject, someValue, 10, "This is my message");
//     DevPanel.Show();
//
// Can also configure cheats here:
//     DevPanel.allowCheats = true;
//
namespace com.deeperbeige.lib
{
    public static class DevPanel
    {
		// Set to true/false to enable/disable cheat system yourself
		public  static bool              allowCheats = false;
		
		private static bool              initialised = false;
		private static GameObject        runtime;
		private static DevPanelComponent component;

		private static void Init()
		{
			initialised = true;
			if (Application.isEditor) allowCheats = true;

			Events.Listen(Events.EVENT_SCENE_CHANGED, DevPanelOnSceneChanged, false);
			DevPanelOnSceneChanged();
			component.Init();
		}
		
		// Reconnect to new listener on scene change
		// Can be called twice in the same scene without harm
		private static void DevPanelOnSceneChanged()
		{
			if (! initialised) Init ();
			
			runtime = GameObjects.FindAnywhere("DevPanelRuntime", true);
			if (runtime == null) runtime = new GameObject("DevPanelRuntime");

			component = runtime.GetComponent<DevPanelComponent>();
			if (component == null) component = runtime.AddComponent<DevPanelComponent>();

			UnityEngine.Object.DontDestroyOnLoad(runtime);
		}

        // Log anything to the dev panel
        // Can send multiple things of different types
        public static void Log(params object[] msg)
        {
			if (! initialised) Init();

            string message = "";
            string comma   = "";
            for (int j=0; j < msg.Length; j++)
            {
				if (msg[j] == null)
				{
					message += comma + "null";
				} else {
                	message += comma + msg[j].ToString();
				}
                comma = ", ";
            }

			component.messages += component.line.ToString("0000") + " ";
			component.messages += message;
			component.messages += "\n";
			component.line++;

            Debug.Log(message);
        }

        public static void DeleteAllSaveFiles()
        {
            Log("Removing all save-game files:");

            DirectoryInfo dirInfo  = new DirectoryInfo(Application.persistentDataPath);
            FileInfo[]    fileInfo = dirInfo.GetFiles();
            foreach (FileInfo file in fileInfo)
            {
                Log("    " + file.FullName);
                File.Delete(file.FullName);
            }
        }

		public static void SetWidthFraction(float fraction)
		{
			fraction = Mathf.Clamp(fraction, 0.1f, 1.0f);
			component.widthPercent = fraction;
		}

        // Show the panel
        public static void Show()
        {
            component.visible = true;
        }
    }
}
