// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2017 Thomas Klose & Torsten Schache, Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Xml;
using System.Collections.Generic;


namespace Hiperscan.Extensions.ApoIdent
{
	public class GuiSettings
	{
		public const string GUI_SETTINGS_TAG 			= "GuiSettings";
		private const string WINDOW_MAXIMIZED_TAG 		= "WindowMaximized";
		private const string WINDOW_HEIGHT_TAG	 		= "WindowHeight";
		private const string WINDOW_WIDTH_TAG			= "WindowWidth";
		private const string USER_COMPLETION_TAG 		= "UserCompletion";
		private const string SUPPLIER_COMPLETION_TAG 	= "SupplerCompletion";
		private const string COMPLETION_TAG 			= "Completion";
		private const string LAST_USER_TAG 				= "LastUser";
		private const string LAST_SUPPLIER_TAG 			= "LastSupplier";
		private const string LAST_QEXPORT_FOLDER_TAG 	= "LastQueryExportFolder";
		private const string LAST_QCOPY_FOLDER_TAG 		= "LastQueryCopyFolder";

		private const string DONT_SERVER_TAG 			= "DontShowServerMessageAgain";
		private const string DONT_QS_TAG 				= "DontShowQuickStepMessageAgain";
		private const string DONT_HINT_TAG 				= "DontShowHintMessageAgain";



		public GuiSettings ()
		{
			this.DontShowHintMessageAgain		= false;
			this.DontShowQuickStepMessageAgain	= false;
			this.DontShowServerMessageAgain		= false;

			this.WindowWidth					= -1;
			this.WindowHeight					= -1;
			this.WindowMaximized				= true;

			this.UserCompletion					= new List<string>();
			this.LastUser						= String.Empty;
			this.SupplierCompletion				= new List<string>();
			this.LastSupplier					= String.Empty;

			this.LastQueryCopyFolder			= String.Empty;
			this.LastQueryExportFolder			= String.Empty;
		}

		public GuiSettings (XmlNode gui_settings_node) : this()
		{
			try
			{
				XmlNode window_maximized_node = gui_settings_node.SelectNodes(GuiSettings.WINDOW_MAXIMIZED_TAG)[0];
				this.WindowMaximized = Boolean.Parse(window_maximized_node.InnerText);
			}
			catch(Exception){}

			try
			{
				XmlNode window_width_node = gui_settings_node.SelectNodes(GuiSettings.WINDOW_WIDTH_TAG)[0];
				this.WindowWidth = Int32.Parse(window_width_node.InnerText);
			}
			catch(Exception){}


			try
			{
				XmlNode window_height_node = gui_settings_node.SelectNodes(GuiSettings.WINDOW_HEIGHT_TAG)[0];
				this.WindowHeight = Int32.Parse(window_height_node.InnerText);
			}
			catch(Exception){}

			if ((this.WindowHeight == -1 || this.WindowWidth == -1) && this.WindowMaximized == false)
				this.WindowMaximized = true;

			try
			{
				XmlNode dont_server_node = gui_settings_node.SelectNodes(GuiSettings.DONT_SERVER_TAG)[0];
				this.DontShowServerMessageAgain = Boolean.Parse (dont_server_node.InnerText);
			}
			catch(Exception){}

			try
			{
				XmlNode dont_qs_node = gui_settings_node.SelectNodes(GuiSettings.DONT_QS_TAG)[0];
				this.DontShowQuickStepMessageAgain = Boolean.Parse (dont_qs_node.InnerText);
			}
			catch(Exception){}

			try
			{
				XmlNode dont_hint_node = gui_settings_node.SelectNodes(GuiSettings.DONT_HINT_TAG)[0];
				this.DontShowHintMessageAgain = Boolean.Parse (dont_hint_node.InnerText);
			}
			catch(Exception){}

			try
			{
				XmlNode last_user_node = gui_settings_node.SelectNodes(GuiSettings.LAST_USER_TAG)[0];
				this.LastUser = last_user_node.InnerText;
			}
			catch(Exception){}

			try
			{
				XmlNode last_supplier_node = gui_settings_node.SelectNodes(GuiSettings.LAST_SUPPLIER_TAG)[0];
				this.LastSupplier = last_supplier_node.InnerText;
			}
			catch(Exception){}

			try
			{
				XmlNode user_completion_node = gui_settings_node.SelectNodes(GuiSettings.USER_COMPLETION_TAG)[0];
				XmlNodeList user_completion_nodes = user_completion_node.SelectNodes(GuiSettings.COMPLETION_TAG);
				if (user_completion_nodes != null && user_completion_nodes.Count > 0)
				{
					foreach (XmlNode completion_node in user_completion_nodes)
						this.UserCompletion.Add(completion_node.InnerText);
				}
			}
			catch(Exception){}

			try
			{
				XmlNode supplier_completion_node = gui_settings_node.SelectNodes(GuiSettings.SUPPLIER_COMPLETION_TAG)[0];
				XmlNodeList supplier_completion_nodes = supplier_completion_node.SelectNodes(GuiSettings.COMPLETION_TAG);
				if (supplier_completion_nodes != null && supplier_completion_nodes.Count > 0)
				{
					foreach (XmlNode completion_node in supplier_completion_nodes)
						this.SupplierCompletion.Add(completion_node.InnerText);
				}
			}
			catch(Exception){}

			try
			{
				XmlNode last_qexport_node = gui_settings_node.SelectNodes(GuiSettings.LAST_QEXPORT_FOLDER_TAG)[0];
				this.LastQueryExportFolder = last_qexport_node.InnerText;
			}
			catch(Exception){}

			try
			{
				XmlNode last_qcopy_node = gui_settings_node.SelectNodes(GuiSettings.LAST_QCOPY_FOLDER_TAG)[0];
				this.LastQueryCopyFolder = last_qcopy_node.InnerText;
			}
			catch(Exception){}
			}

		public void WriteToXml(XmlDocument doc, XmlNode parent)
		{
			XmlNode gui_settings_node_node = doc.CreateElement(GuiSettings.GUI_SETTINGS_TAG);
			parent.AppendChild(gui_settings_node_node);

			XmlNode window_maximized_node = doc.CreateElement(GuiSettings.WINDOW_MAXIMIZED_TAG);
			window_maximized_node.InnerText = this.WindowMaximized.ToString();
			gui_settings_node_node.AppendChild(window_maximized_node);

			XmlNode window_width_node = doc.CreateElement(GuiSettings.WINDOW_WIDTH_TAG);
			window_width_node.InnerText = this.WindowWidth.ToString();
			gui_settings_node_node.AppendChild(window_width_node);

			XmlNode window_height_node = doc.CreateElement(GuiSettings.WINDOW_HEIGHT_TAG);
			window_height_node.InnerText = this.WindowHeight.ToString();
			gui_settings_node_node.AppendChild(window_height_node);

			XmlNode dont_server_node = doc.CreateElement(GuiSettings.DONT_SERVER_TAG);
			dont_server_node.InnerText = this.DontShowServerMessageAgain.ToString();
			gui_settings_node_node.AppendChild(dont_server_node);

			XmlNode dont_qs_node = doc.CreateElement(GuiSettings.DONT_QS_TAG);
			dont_qs_node.InnerText = this.DontShowQuickStepMessageAgain.ToString();
			gui_settings_node_node.AppendChild(dont_qs_node);

			XmlNode dont_hint_node = doc.CreateElement(GuiSettings.DONT_HINT_TAG);
			dont_hint_node.InnerText = this.DontShowHintMessageAgain.ToString();
			gui_settings_node_node.AppendChild(dont_hint_node);

			XmlNode last_user_node = doc.CreateElement(GuiSettings.LAST_USER_TAG);
			last_user_node.InnerText = this.LastUser;
			gui_settings_node_node.AppendChild(last_user_node);

			XmlNode last_supplier_node = doc.CreateElement(GuiSettings.LAST_SUPPLIER_TAG);
			last_supplier_node.InnerText = this.LastSupplier;
			gui_settings_node_node.AppendChild(last_supplier_node);

			XmlNode user_completion_node = doc.CreateElement(GuiSettings.USER_COMPLETION_TAG);
			gui_settings_node_node.AppendChild(user_completion_node);
			foreach (string completion_entry in this.UserCompletion)
			{
				if (String.IsNullOrEmpty(completion_entry) == true)
					continue;
				
				XmlNode completion_node = doc.CreateElement(GuiSettings.COMPLETION_TAG);
				completion_node.InnerText = completion_entry;
				user_completion_node.AppendChild(completion_node);
			}

			XmlNode supplier_completion_node = doc.CreateElement(GuiSettings.SUPPLIER_COMPLETION_TAG);
			gui_settings_node_node.AppendChild(supplier_completion_node);
			foreach (string completion_entry in this.SupplierCompletion)
			{
				if (String.IsNullOrEmpty(completion_entry) == true)
					continue;
				
				XmlNode completion_node = doc.CreateElement(GuiSettings.COMPLETION_TAG);
				completion_node.InnerText = completion_entry;
				supplier_completion_node.AppendChild(completion_node);
			}

			XmlNode last_qexport_node = doc.CreateElement(GuiSettings.LAST_QEXPORT_FOLDER_TAG);
			last_qexport_node.InnerText = this.LastQueryExportFolder;
			gui_settings_node_node.AppendChild(last_qexport_node);

			XmlNode last_qcopy__node = doc.CreateElement(GuiSettings.LAST_QCOPY_FOLDER_TAG);
			last_qcopy__node.InnerText = this.LastQueryCopyFolder;
			gui_settings_node_node.AppendChild(last_qcopy__node);
		}

		public List<string> UserCompletion 			{get; set;}
		public List<string> SupplierCompletion 		{get; set;}
		public string LastUser						{get; set;}
		public string LastSupplier					{get; set;}
		public string LastQueryExportFolder			{get; set;}
		public string LastQueryCopyFolder			{get; set;}

		public int WindowHeight						{get; set;}
		public int WindowWidth						{get; set;}
		public bool WindowMaximized					{get; set;}

		public bool DontShowServerMessageAgain		{get; set;}
		public bool DontShowQuickStepMessageAgain	{get; set;}
		public bool DontShowHintMessageAgain		{get; set;}
	}
}

