// 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.Collections.Generic;
using System.Xml;

namespace Hiperscan.ApoIdentCommon.ApoConfig
{
    public class GuiSettings : SettingsTemplate<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 PRODUCER_COMPLETION_TAG = "SupplerCompletion";
        private const string COMPLETION_TAG = "Completion";
        private const string LAST_USER_TAG = "LastUser";
        private const string LAST_PRODUCER_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 GuiSettings() : base()
        {

        }

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

        protected override void Init(XmlNode parent_node = null)
        {
            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.ProducerCompletion = new List<string>();
            this.LastProducer = 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(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 last_user_node = gui_settings_node.SelectNodes(GuiSettings.LAST_USER_TAG)[0];
                this.LastUser = last_user_node.InnerText;
            }
            catch (Exception) { }

            try
            {
                XmlNode last_producer_node = gui_settings_node.SelectNodes(GuiSettings.LAST_PRODUCER_TAG)[0];
                this.LastProducer = last_producer_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 producer_completion_node = gui_settings_node.SelectNodes(GuiSettings.PRODUCER_COMPLETION_TAG)[0];
                XmlNodeList producer_completion_nodes = producer_completion_node.SelectNodes(GuiSettings.COMPLETION_TAG);
                if (producer_completion_nodes != null && producer_completion_nodes.Count > 0)
                {
                    foreach (XmlNode completion_node in producer_completion_nodes)
                        this.ProducerCompletion.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 override 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 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_producer_node = doc.CreateElement(GuiSettings.LAST_PRODUCER_TAG);
            last_producer_node.InnerText = this.LastProducer;
            gui_settings_node_node.AppendChild(last_producer_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 producer_completion_node = doc.CreateElement(GuiSettings.PRODUCER_COMPLETION_TAG);
            gui_settings_node_node.AppendChild(producer_completion_node);
            foreach (string completion_entry in this.ProducerCompletion)
            {
                if (String.IsNullOrEmpty(completion_entry) == true)
                    continue;

                XmlNode completion_node = doc.CreateElement(GuiSettings.COMPLETION_TAG);
                completion_node.InnerText = completion_entry;
                producer_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 void UpdateProducerCompletion(string language)
        {
            List<string> default_producers = new List<string>();
            switch (language)
            {
                case "de":
                    default_producers = Common.Pharmacy.Producer.GetAsList();
                    break;

                default:
                    break;
            }

            foreach (string producer in this.ProducerCompletion)
            {
                bool found = false;
                string producer_cleaned = producer.Trim().ToLower().Replace("-", String.Empty).Replace(" ", String.Empty);

                foreach (string default_producer in default_producers)
                {
                    string default_producer_cleaned = default_producer.Trim().ToLower().Replace("-", String.Empty).Replace(" ", String.Empty);
                    if (producer_cleaned == default_producer_cleaned)
                    {
                        found = true;
                        break;
                    }
                }

                if (found == false)
                    default_producers.Add(producer);
            }

            this.ProducerCompletion = default_producers;
        }

        public override GuiSettings Clone()
        {
            return null;
        }

        protected override bool SettingsChanged()
        {
            return false;
        }

        public List<string> UserCompletion { get; set; }
        public List<string> ProducerCompletion { get; set; }
        public string LastUser { get; set; }
        public string LastProducer { 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; }
    }
}

