// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2021 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 Hiperscan.GtkExtensions.Widgets;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class GuiDesigner
    {
        public const string FONT_NAME = "Arial";

        public GuiDesigner()
        {
            ExtToolTip.SetExtToolTipDesign = GuiDesigner.SetSchema;

            this.LoadDefaultImageSurfaces();
            this.LoadResultPictures();
            this.LoadActiveIconImages();
            this.LoadInactiveIconImages();
            this.LoadSubstancePoints();
        }

        public static void SetSchema(Gtk.Window window)
        {
            GuiDesigner.SetSchema(window, false);
        }

        public static void SetSchema(Gtk.Window window, bool scale)
        {
            window.ShowAll();

            GuiDesigner.SetFont(window);

            if (GuiDesigner.IsWindows == false)
                return;

            double scale_factor = GuiDesigner.ScaleFactor;

            if (scale)
            {
                GuiDesigner.ModifyMessageDialog(window, scale_factor);
                GuiDesigner.SetWindowsStyle(window, scale_factor);
            }
        }

        private static void SetWindowsStyle(Gtk.Widget widget, double scale)
        {
            Pango.FontDescription fd = null;

            Gtk.Entry entry = widget as Gtk.Entry;
            if (entry != null)
            {
                fd = entry.Style.FontDesc.Copy();
                fd.Size = (int)(fd.Size * scale);
                entry.ModifyFont(fd);
            }

            Gtk.ComboBox combobox = widget as Gtk.ComboBox;
            if (combobox != null)
            {
                GuiDesigner.SetCombobox(combobox, scale);
            }

            FlexLabel flex_label = widget as FlexLabel;
            if (flex_label != null)
            {
                flex_label.FontSize *= scale;
            }

            Gtk.Container container = widget as Gtk.Container;
            if (container != null)
            {
                foreach (Gtk.Widget child in container.Children)
                {
                    GuiDesigner.SetWindowsStyle(child, scale);
                }
            }
        }

        private static void SetFont(Gtk.Window widget)
        {
            GtkExtensions.Widgets.Font.SetFont(widget, GuiDesigner.FONT_NAME);
        }

        private static void SetEntryCompletion(Gtk.EntryCompletion completion, double scale)
        {
            foreach (Gtk.CellRenderer cell in completion.Cells)
            {
                if (cell is Gtk.CellRendererText)
                    (cell as Gtk.CellRendererText).Scale *= scale;

                cell.Xpad *= (uint)(scale);
            }
        }

        private static void SetCombobox(Gtk.ComboBox combobox, double scale)
        {
            foreach (Gtk.CellRenderer cell in combobox.Cells)
            {
                if (cell is Gtk.CellRendererText)
                    (cell as Gtk.CellRendererText).Scale *= scale;

                cell.Xpad *= (uint)(cell.Xpad * scale);
            }
        }

        private static void SetTreeview(Gtk.TreeView treeview, double scale)
        {
            foreach (Gtk.TreeViewColumn column in treeview.Columns)
            {
                foreach (Gtk.CellRenderer cell in column.Cells)
                {
                    if (cell is Gtk.CellRendererText)
                        (cell as Gtk.CellRendererText).Scale *= scale;

                    cell.Xpad = (uint)(cell.Xpad * scale);
                }
            }

            foreach (Gtk.Widget child in treeview.AllChildren)
                GuiDesigner.SetWindowsStyle(child, scale);
        }

        private static bool IsWindows
        {
            get
            {
                if (GuiDesigner.SimulateWindows)
                    return true;

                return Environment.OSVersion.Platform == PlatformID.Win32NT;
            }
        }

        public static double ScaleFactor
        {
            get
            {
                if (GuiDesigner.IsWindows)
                    return 1.2;

                return 1;
            }
        }

        private static bool SimulateWindows { get; set; } = false; //for debugging and tests
    }
}
