﻿using System.IO;
using System.Text;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace GLogUnity
{
	[CreateAssetMenu(fileName = "GLogChannelGURaaS", menuName = "Log Channel/Remote (GURaaS)", order = 303)]
	public class GLogChannelGURaaS : GLogChannel
	{
		public string backupFolderName = Path.Combine("Output", "guraasChannel");
		public float updateFrequency = 0;

		private float updateNext = 0;
		private GLogChannelGURaaSLogger logger;

		/// <summary>Open is called when ever a channel is initialized</summary>
		public override void Open()
		{
			if (IsOpen())
			{
				Shutdown();
			}

			base.Open();

			updateNext = updateFrequency;
			logger = new GLogChannelGURaaSLogger(Path.Combine(Application.persistentDataPath, backupFolderName, name + "_logFile.log"), Path.Combine(Application.persistentDataPath, backupFolderName, name + "_cacheFile.log"));
		}

		/// <summary>Shutdown is called when ever a channel is closed and cleans up any references and dependencies</summary>
		public override void Shutdown()
		{
			logger?.Shutdown();
			logger = null;
		}

		/// <summary>Check if the channel is open</summary>
		public override bool IsOpen()
		{
			return logger != null;
		}

		/// <summary>Adds a log to the channel</summary>
		public override void Log(LogInfo log)
		{
			logger?.Log(log);
		}

		/// <summary>Starts a new game session. 
		/// <remarks>If a new game session is already active, it closes that session. See <see cref="CloseSession"/></remarks>
		/// </summary>
		public override void StartSession(SessionInfo session)
		{
			base.StartSession(session);
			logger?.StartSession(session);
		}

		/// <summary>Close the current game session, and pushes/flushed all the cached logs</summary>
		public override void CloseSession(SessionInfo session)
		{
			if (activeSession == null) return;
			activeSession = session;

			logger?.CloseSession(session);
		}

		/// <summary>Flush send session to remote server</summary>
		public override void Flush()
		{
			if (activeSession == null || activeSession.gameId == null || activeSession.gameId.Equals(""))
			{
				return;
			}

			logger?.Flush();
		}

		/// <summary>Update the log system, and if required flush the cached values</summary>        
		public override void UpdateChannel(float deltaTime)
		{
			if (updateFrequency == 0)
			{
				Flush();
			}
			else
			{
				updateNext -= deltaTime;
				if (updateNext < 0)
				{
					updateNext = updateFrequency;
					Flush();
				}
			}
		}
	}

#if UNITY_EDITOR
	[CustomEditor(typeof(GLogChannelGURaaS))]
	public class GLogChannelFileGURaaS : GLogChannelEditor
	{
		public override void OnInspectorGUI()
		{
			base.OnInspectorGUI();

			GLogChannelGURaaS guraasChannel = (GLogChannelGURaaS) targetChannel;
			EditorGUILayout.LabelField("Submit Frequency Seconds");
			guraasChannel.updateFrequency = EditorGUILayout.Slider(guraasChannel.updateFrequency, 2, 600);

			EditorGUILayout.LabelField("Backup folder name (starting at PersistentDataPath, no extension)");

			EditorGUILayout.BeginHorizontal();
			guraasChannel.backupFolderName = EditorGUILayout.TextField(guraasChannel.backupFolderName);

			if (GUILayout.Button("Open...", GUILayout.MaxWidth(60)))
			{
				Debug.Log(Path.Combine(Application.persistentDataPath, guraasChannel.backupFolderName));
				Application.OpenURL(Path.Combine(Application.persistentDataPath, guraasChannel.backupFolderName));
			}

			EditorGUILayout.EndHorizontal();
		}
	}
#endif
}