// 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 partial class Configurations
    {
        public class GuiPreferences : Preferences<GuiPreferences>
        {
            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 BACK_GROUND_COLOR_TAG = "BackgroundColor";
            private const string CENTER_COLOR_TAG = "CenterColor";
            private const string CENTER_FONT_COLOR_TAG = "CenterFontColor";
            private const string CENTER_ADVICE_FONT_COLOR_TAG = "CenterAdviceFontColor";
            private const string CENTER_MENU_FONT_COLOR_TAG = "CenterMenuFontColor";
            private const string OUTER_COLOR_TAG = "OuterColor";
            private const string OUTER_FONT_COLOR_TAG = "OuterFontColor";

            private const string COLOR_RED_TAG = "R";
            private const string COLOR_GREEN_TAG = "G";
            private const string COLOR_BLUE_TAG = "B";

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

            public GuiPreferences() : base()
            {

            }

            public GuiPreferences(XmlNode gui_settings_node) : base(gui_settings_node)
            {
            }

            protected override void Init(XmlNode parent_node = null)
            {
                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;

                base.Init(parent_node);
            }

            protected override void Read(XmlNode gui_settings_node)
            {
                try
                {
                    XmlNode window_maximized_node = gui_settings_node.SelectNodes(GuiPreferences.WINDOW_MAXIMIZED_TAG)[0];
                    this.WindowMaximized = Boolean.Parse(window_maximized_node.InnerText);
                }
                catch (Exception) { }

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


                try
                {
                    XmlNode window_height_node = gui_settings_node.SelectNodes(GuiPreferences.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(GuiPreferences.DONT_SERVER_TAG)[0];
                    this.DontShowServerMessageAgain = Boolean.Parse(dont_server_node.InnerText);
                }
                catch (Exception) { }

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

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

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

                try
                {
                    XmlNode user_completion_node = gui_settings_node.SelectNodes(GuiPreferences.USER_COMPLETION_TAG)[0];
                    XmlNodeList user_completion_nodes = user_completion_node.SelectNodes(GuiPreferences.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(GuiPreferences.SUPPLIER_COMPLETION_TAG)[0];
                    XmlNodeList supplier_completion_nodes = supplier_completion_node.SelectNodes(GuiPreferences.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(GuiPreferences.LAST_QEXPORT_FOLDER_TAG)[0];
                    this.LastQueryExportFolder = last_qexport_node.InnerText;
                }
                catch (Exception) { }

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

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

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

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

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

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

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

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

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

                XmlNode user_completion_node = doc.CreateElement(GuiPreferences.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(GuiPreferences.COMPLETION_TAG);
                    completion_node.InnerText = completion_entry;
                    user_completion_node.AppendChild(completion_node);
                }

                XmlNode supplier_completion_node = doc.CreateElement(GuiPreferences.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(GuiPreferences.COMPLETION_TAG);
                    completion_node.InnerText = completion_entry;
                    supplier_completion_node.AppendChild(completion_node);
                }

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

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

            public void UpdateSupplierCompletion(string language)
            {
                List<string> default_suppliers = new List<string>();
                switch (language)
                {
                    case "de":
                        default_suppliers.Add("Caelo");
                        default_suppliers.Add("Fagron");
                        default_suppliers.Add("Euro-OTC");
                        default_suppliers.Add("Bombastus");
                        default_suppliers.Add("apomix");
                        default_suppliers.Add("Hedinger");
                        default_suppliers.Add("Henry Lamotte");
                        default_suppliers.Add("Ichthyol");

                        break;

                    default:
                        break;
                }

                foreach (string supplier in this.SupplierCompletion)
                {
                    bool found = false;
                   string  supplier_cleaned = supplier.Trim().ToLower().Replace("-", String.Empty).Replace(" ", String.Empty);

                    foreach (string default_supplier in default_suppliers)
                    {
                        string default_supplier_cleaned = default_supplier.Trim().ToLower().Replace("-", String.Empty).Replace(" ", String.Empty);
                        if (supplier_cleaned == default_supplier_cleaned)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (found == false)
                        default_suppliers.Add(supplier);
                }

                this.SupplierCompletion = default_suppliers;
            }

            public override GuiPreferences Clone()
            {
                return null;
            }

            protected override bool SettingsChanged()
            {
                return false;
            }

            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; }
        }
    }
}

