// 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 System.Linq;
using Hiperscan.GtkExtensions.Widgets;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class GuiDesigner
    {
        private class OriginalMessageDialogData
        {
            public bool UseMarkup { get; set; }
            public Gtk.Label Label { get; set; }
            public Gtk.Image Image { get; set; }
        }

        private static void ModifyMessageDialog(Gtk.Window window, double scale)
        {
            Gtk.MessageDialog mdlg = window as Gtk.MessageDialog;
            if (mdlg == null)
                return;

            mdlg.VBox.Children.First().Visible = false;
            OriginalMessageDialogData origin = GuiDesigner.FindOrigingMessageData(mdlg);

            Gtk.HBox hbox = new Gtk.HBox();
            Gtk.Label label_copy = GuiDesigner.CopyMessageLabel(origin.Label);
            Gtk.Image image_copy = GuiDesigner.CopyMessageImage(origin.Image);

            Gtk.Alignment image_alignment = new Gtk.Alignment(0, 0, 0, 0);
            image_alignment.Add(image_copy);
            hbox.PackStart(image_alignment);

            Gtk.Alignment label_alignment = new Gtk.Alignment(0, 0, 0, 0);
            label_alignment.Add(label_copy);
            hbox.PackStart(label_alignment);

            Gtk.Alignment space_alignment = new Gtk.Alignment(0, 0, 0, 0);
            space_alignment.WidthRequest = 1;
            hbox.PackStart(space_alignment);

            mdlg.VBox.PackStart(hbox);
            hbox.ShowAll();
            hbox.Spacing = (int)(10 * scale);
            mdlg.VBox.ReorderChild(hbox, 2);
        }

        private static Gtk.Image CopyMessageImage(Gtk.Image original)
        {
            string stock = original.Stock;
            Gtk.IconSize size = (Gtk.IconSize)original.IconSize;
            Gtk.Image copy = new Gtk.Image(stock, size);
            return copy;
        }

        private static Gtk.Label CopyMessageLabel(Gtk.Label original)
        {
            Gtk.Label copy = new Gtk.Label();
            copy.UseMarkup = original.UseMarkup;
            if (copy.UseMarkup)
                copy.Markup = original.LabelProp;
            else
                copy.Text = original.Text;

            copy.Wrap = original.Wrap;
            copy.LineWrapMode = original.LineWrapMode;
            copy.LineWrap = original.LineWrap;

            return copy;
        }

        private static OriginalMessageDialogData FindOrigingMessageData(Gtk.MessageDialog mdlg)
        {
            OriginalMessageDialogData tmp = new OriginalMessageDialogData();
            tmp.UseMarkup = mdlg.UseMarkup;
            tmp.Label = GuiDesigner.FindMessageDialogLabel(mdlg.VBox.Children.First(), mdlg.UseMarkup); ;
            tmp.Image = GuiDesigner.FindMessageDialogImage(mdlg.VBox.Children.First());

            return tmp;
        }

        private static Gtk.Label FindMessageDialogLabel(Gtk.Widget widget, bool use_markup)
        {
            Gtk.Label label = widget as Gtk.Label;
            if (label != null)
            {
                if (label.UseMarkup == use_markup)
                    return label;
                else
                    return null;
            }

            Gtk.Container container = widget as Gtk.Container;
            if (container == null)
                return null;

            foreach (Gtk.Widget child in container.Children)
            {
                label = GuiDesigner.FindMessageDialogLabel(child, use_markup);
                if (label != null)
                    return label;
            }

            return null;
        }

        private static Gtk.Image FindMessageDialogImage(Gtk.Widget widget)
        {
            Gtk.Image image = widget as Gtk.Image;
            if (image != null)
                return image;

            Gtk.Container container = widget as Gtk.Container;
            if (container == null)
                return null;

            foreach (Gtk.Widget child in container.Children)
            {
                image = GuiDesigner.FindMessageDialogImage(child);
                if (image != null)
                    return image;
            }

            return null;
        }
    }
}
