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

using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{
    public partial class Configurations
    {
        public enum PrinterOrientation
        {
            Degree_0,
            Degree_90,
            Degree_180,
            Degree_270,
        }

        public class LabelPrinterPreferences : Preferences<LabelPrinterPreferences>
        {
            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 LabelPrinterPreferences() : base()
            {

            }

            public LabelPrinterPreferences(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(LabelPrinterPreferences.NAME_TAG)[0];
                    this.PrinterName = printer_name_node.InnerText;
                }
                catch (Exception) { return; }

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

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

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

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

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

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

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

                try
                {
                    XmlNode has_printer_node = printer_settings_node.SelectNodes(LabelPrinterPreferences.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(LabelPrinterPreferences.PRINTER_SETTINGS_TAG);
                parent.AppendChild(printer_settings_node_node);

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

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

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

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

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

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

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

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

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

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

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

