// 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.Globalization;
using Hiperscan.ApoIdentCommon.ApoConfig.Enums;

namespace Hiperscan.ApoIdentCommon.ApoConfig
{
    public class LabelPrinterSettings : SettingsTemplate<LabelPrinterSettings>
    {
        public const string PRINTER_SETTINGS_TAG = "PrinterSettings";
        private const string NAME_TAG = "Name";
        private const string SIZE_TAG = "Size";
        private const string ORIENTATION_TAG = "Orientation";
        private const string WIDTH_TAG = "Width";
        private const string HEIGHT_TAG = "Height";
        private const string X_INDENT_TAG = "X_Indent";
        private const string Y_INDENT_TAG = "Y_Indent";
        private const string SCALE_TAG = "Scale";
        private const string HAS_PRINTER_TAG = "HasPrinter";

        public LabelPrinterSettings() : base()
        {

        }

        public LabelPrinterSettings(XmlNode printer_settings_node) : base(printer_settings_node)
        {

        }

        protected override void Init(XmlNode parent_node = null)
        {
            this.Culture = new CultureInfo("en-US");

            this.PrinterName = String.Empty;
            this.PaperSize = String.Empty;
            this.Orientation = PrinterOrientation.Degree_0;
            this.PaperWidthMM = 0;
            this.PaperHeightMM = 0;
            this.X_IndentMM = 0;
            this.Y_IndentMM = 0;
            this.Scale = 1;
            this.HasPrinter = false;

            base.Init(parent_node);
        }

        protected override void Read(XmlNode printer_settings_node)
        {
            try
            {
                XmlNode printer_name_node = printer_settings_node.SelectNodes(LabelPrinterSettings.NAME_TAG)[0];
                this.PrinterName = printer_name_node.InnerText;
            }
            catch (Exception) { return; }

            try
            {
                XmlNode size_node = printer_settings_node.SelectNodes(LabelPrinterSettings.SIZE_TAG)[0];
                this.PaperSize = size_node.InnerText;
            }
            catch (Exception) { }

            try
            {
                XmlNode orientation_node = printer_settings_node.SelectNodes(LabelPrinterSettings.ORIENTATION_TAG)[0];
                this.Orientation = (PrinterOrientation)Enum.Parse(typeof(PrinterOrientation), orientation_node.InnerText, true);
            }
            catch (Exception) { }

            try
            {
                XmlNode width_node = printer_settings_node.SelectNodes(LabelPrinterSettings.WIDTH_TAG)[0];
                this.PaperWidthMM = Double.Parse(width_node.InnerText, this.Culture);
            }
            catch (Exception) { }

            try
            {
                XmlNode height_node = printer_settings_node.SelectNodes(LabelPrinterSettings.HEIGHT_TAG)[0];
                this.PaperHeightMM = Double.Parse(height_node.InnerText, this.Culture);
            }
            catch (Exception) { }

            try
            {
                XmlNode x_node = printer_settings_node.SelectNodes(LabelPrinterSettings.X_INDENT_TAG)[0];
                this.X_IndentMM = Double.Parse(x_node.InnerText, this.Culture);
            }
            catch (Exception) { }

            try
            {
                XmlNode y_node = printer_settings_node.SelectNodes(LabelPrinterSettings.Y_INDENT_TAG)[0];
                this.Y_IndentMM = Double.Parse(y_node.InnerText, this.Culture);
            }
            catch (Exception) { }

            try
            {
                XmlNode scale_node = printer_settings_node.SelectNodes(LabelPrinterSettings.SCALE_TAG)[0];
                this.Scale = Double.Parse(scale_node.InnerText, this.Culture);
            }
            catch (Exception) { }

            try
            {
                XmlNode has_printer_node = printer_settings_node.SelectNodes(LabelPrinterSettings.HAS_PRINTER_TAG)[0];
                this.HasPrinter = Boolean.Parse(has_printer_node.InnerText);
            }
            catch (Exception)
            {
                //Work around for older version than 2.0
                if (this.PrinterName != "None" && this.PrinterName != "Kein" && String.IsNullOrEmpty(this.PrinterName) == false)
                    this.HasPrinter = true;
            }
        }

        public override void WriteToXml(XmlDocument doc, XmlNode parent)
        {
            XmlNode printer_settings_node_node = doc.CreateElement(LabelPrinterSettings.PRINTER_SETTINGS_TAG);
            parent.AppendChild(printer_settings_node_node);

            XmlNode printer_name_node = doc.CreateElement(LabelPrinterSettings.NAME_TAG);
            printer_name_node.InnerText = this.PrinterName;
            printer_settings_node_node.AppendChild(printer_name_node);

            XmlNode size_node = doc.CreateElement(LabelPrinterSettings.SIZE_TAG);
            size_node.InnerText = this.PaperSize;
            printer_settings_node_node.AppendChild(size_node);

            XmlNode orientation_node = doc.CreateElement(LabelPrinterSettings.ORIENTATION_TAG);
            orientation_node.InnerText = this.Orientation.ToString();
            printer_settings_node_node.AppendChild(orientation_node);

            XmlNode width_node = doc.CreateElement(LabelPrinterSettings.WIDTH_TAG);
            width_node.InnerText = this.PaperWidthMM.ToString(this.Culture);
            printer_settings_node_node.AppendChild(width_node);

            XmlNode height_node = doc.CreateElement(LabelPrinterSettings.HEIGHT_TAG);
            height_node.InnerText = this.PaperHeightMM.ToString(this.Culture);
            printer_settings_node_node.AppendChild(height_node);

            XmlNode x_node = doc.CreateElement(LabelPrinterSettings.X_INDENT_TAG);
            x_node.InnerText = this.X_IndentMM.ToString(this.Culture);
            printer_settings_node_node.AppendChild(x_node);

            XmlNode y_node = doc.CreateElement(LabelPrinterSettings.Y_INDENT_TAG);
            y_node.InnerText = this.Y_IndentMM.ToString(this.Culture);
            printer_settings_node_node.AppendChild(y_node);

            XmlNode scale_node = doc.CreateElement(LabelPrinterSettings.SCALE_TAG);
            scale_node.InnerText = this.Scale.ToString(this.Culture);
            printer_settings_node_node.AppendChild(scale_node);

            XmlNode has_printer_node = doc.CreateElement(LabelPrinterSettings.HAS_PRINTER_TAG);
            has_printer_node.InnerText = this.HasPrinter.ToString();
            printer_settings_node_node.AppendChild(has_printer_node);
        }

        public override LabelPrinterSettings Clone()
        {
            LabelPrinterSettings tmp = new LabelPrinterSettings();

            tmp.PrinterName = this.PrinterName;
            tmp.PaperSize = this.PaperSize;
            tmp.Orientation = this.Orientation;
            tmp.PaperWidthMM = this.PaperWidthMM;
            tmp.PaperHeightMM = this.PaperHeightMM;
            tmp.X_IndentMM = this.X_IndentMM;
            tmp.Y_IndentMM = this.Y_IndentMM;
            tmp.Scale = this.Scale;
            tmp.HasPrinter = this.HasPrinter;
            tmp.Culture = new CultureInfo(this.Culture.ToString());

            return tmp;
        }

        protected override bool SettingsChanged()
        {
            if (this.Stashed == null)
                return false;

            if (this.Stashed.PrinterName != this.PrinterName)
                return true;

            return false;
        }

        public string PrinterName { get; set; }
        public string PaperSize { get; set; }
        public PrinterOrientation Orientation { get; set; }
        public double PaperWidthMM { get; set; }
        public double PaperHeightMM { get; set; }
        public double X_IndentMM { get; set; }
        public double Y_IndentMM { get; set; }
        public double Scale { get; set; }
        public bool HasPrinter { get; set; }
        private CultureInfo Culture { get; set; }
    }
}

