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

namespace Hiperscan.Extensions.ApoIdent
{
    public class DesignerDialog
    {
        [Widget] Gtk.Window main_window = null;
        [Widget] Gtk.SpinButton bg_y_spinbutton = null;
        [Widget] Gtk.SpinButton measuring_buttonsize_spinbutton = null;
        [Widget] Gtk.SpinButton measuring_alignment_spinbutton = null;
        [Widget] Gtk.SpinButton outer_lines_width_spinbutton = null;
        [Widget] Gtk.SpinButton menu_line_width_spinbutton = null;
        [Widget] Gtk.SpinButton top_sb_line_width_spinbutton = null;
        [Widget] Gtk.SpinButton left_sb_line_width_spinbutton = null;
        [Widget] Gtk.SpinButton right_sb_line_width_spinbutton = null;



        public static DesignerDialog Start(ApoIdentApplication application)
        {
            return new DesignerDialog(application);
        }

        private DesignerDialog(ApoIdentApplication application)
        {
            this.DesingerData = application.GuiDesignerData;
            //this.Animation = application.Animation2;
            this.Init();
        }

        private void Init()
        {
            Glade.XML gui = new Glade.XML(null, "Hiperscan.Extensions.ApoIdent.GuiDesigner.gui_designer.glade", "main_window", "Hiperscan");
            gui.Autoconnect(this);

            this.bg_y_spinbutton.ValueChanged += this.OnBgYPositionSpinbuttonValueChanged;

            this.measuring_buttonsize_spinbutton.ValueChanged += this.OnMeasuringButtonSizeSpinbuttonValueChanged;
            this.measuring_buttonsize_spinbutton.Value = this.DesingerData.MeasuringButtonSize;

            this.measuring_alignment_spinbutton.ValueChanged += this.OnMeasuringAlignmentSpinbuttonValueChanged;
            this.measuring_alignment_spinbutton.Value = this.DesingerData.MeasuringAlignment;

            this.outer_lines_width_spinbutton.ValueChanged += this.OnOuterLineWidthSpinbuttonValueChanged;
            this.outer_lines_width_spinbutton.Value = this.DesingerData.OuterLineWidth;

            this.menu_line_width_spinbutton.ValueChanged += this.OnMenuLineWidthSpinbuttonValueChanged;
            this.menu_line_width_spinbutton.Value = this.DesingerData.MenuLineeWidth;

            this.top_sb_line_width_spinbutton.ValueChanged += this.OnStatebarTopWidthSpinbuttonValueChanged;
            this.top_sb_line_width_spinbutton.Value = this.DesingerData.StateBarTopLineWidth;

            this.left_sb_line_width_spinbutton.ValueChanged += this.OnStatebarLeftWidthSpinbuttonValueChanged;
            this.left_sb_line_width_spinbutton.Value = this.DesingerData.StateBarLeftLineWidth;

            this.right_sb_line_width_spinbutton.ValueChanged += this.OnStatebarRightWidthSpinbuttonValueChanged;
            this.right_sb_line_width_spinbutton.Value = this.DesingerData.StateBarRightLineWidth;



            this.main_window.WidthRequest = 500;
            this.main_window.HeightRequest = 500;
            this.main_window.WindowPosition = WindowPosition.Center;
            this.main_window.ShowAll();
        }

        private void OnBackgroundColorExposeEvent(object sender, ExposeEventArgs e)
        {
            /*
            Cairo.Color color = ColorSettings.ConvertToCairoColor(this.Settings.BackGroundColor, null);

            Cairo.Context ctx = Gdk.CairoHelper.Create(e.Event.Window);
            ctx.Rectangle(new Cairo.Rectangle(0, 0, this.background_drawingarea.Allocation.Width, this.background_drawingarea.Allocation.Height));
            ctx.SetSourceRGBA(color.R, color.G, color.B, color.A);
            ctx.Fill();
            ((IDisposable)ctx.GetTarget()).Dispose();
            ((IDisposable)ctx).Dispose();
            */           
        }

        private void OnMenuIconColorButtonClicked(object sender, EventArgs e)
        {
            Gdk.Color? color = this.ShowColorDialog(Gdk.Color.Zero);
            if (color == null)
                return;

            this.DesingerData.SetIconColor(color ?? Gdk.Color.Zero, true);
        }

        private void OnFormularIconColorButtonClicked(object sender, EventArgs e)
        {
            Gdk.Color? color = this.ShowColorDialog(Gdk.Color.Zero);
            if (color == null)
                return;

            this.DesingerData.SetIconColor(color ?? Gdk.Color.Zero, false);
        }

        private void OnSatusIconColorButtonClicked(object sender, EventArgs e)
        {
            Gdk.Color? color = this.ShowColorDialog(Gdk.Color.Zero);
            if (color == null)
                return;

            this.DesingerData.SetLogoColor(color ?? Gdk.Color.Zero);

            Application.Invoke((sender2, e2) => this.Animation.ApoIdentLogo = this.DesingerData.Logo);
        }

        private Gdk.Color? ShowColorDialog(Gdk.Color pre_color)
        {
            Gtk.ColorSelectionDialog cdlg = new Gtk.ColorSelectionDialog(Catalog.GetString("Color selection"));
            cdlg.ColorSelection.CurrentColor = pre_color;

            Gdk.Color? color = null;

            cdlg.SetPosition(WindowPosition.CenterAlways);

            if (cdlg.Run() == (int)Gtk.ResponseType.Ok)
                color = cdlg.ColorSelection.CurrentColor;

            cdlg.Destroy();
            ;
            return color;
        }

        private void OnBgYPositionSpinbuttonValueChanged(object sender, EventArgs e)
        {
            this.DesingerData.BgYPosition = (sender as Gtk.SpinButton).Value;

        }

        private void OnMeasuringButtonSizeSpinbuttonValueChanged(object sender, EventArgs e)
        {
            this.DesingerData.MeasuringButtonSize = (int)(sender as Gtk.SpinButton).Value;
        }

        private void OnMeasuringAlignmentSpinbuttonValueChanged(object sender, EventArgs e)
        {
            this.DesingerData.MeasuringAlignment = (sender as Gtk.SpinButton).Value;
        }

        private void OnOuterLineWidthSpinbuttonValueChanged(object sender, EventArgs e)
        {
            this.DesingerData.OuterLineWidth = (int)(sender as Gtk.SpinButton).Value;
        }

        private void OnMenuLineWidthSpinbuttonValueChanged(object sender, EventArgs e)
        {
            this.DesingerData.MenuLineeWidth = (int)(sender as Gtk.SpinButton).Value;
        }

        private void OnStatebarTopWidthSpinbuttonValueChanged(object sender, EventArgs e)
        {
            this.DesingerData.StateBarTopLineWidth = (int)(sender as Gtk.SpinButton).Value;
        }

        private void OnStatebarLeftWidthSpinbuttonValueChanged(object sender, EventArgs e)
        {
            this.DesingerData.StateBarLeftLineWidth = (int)(sender as Gtk.SpinButton).Value;
        }

        private void OnStatebarRightWidthSpinbuttonValueChanged(object sender, EventArgs e)
        {
            this.DesingerData.StateBarRightLineWidth = (int)(sender as Gtk.SpinButton).Value;
        }



        private GuiDesigner DesingerData { get; set; }
        private Animation Animation { get; set; }
    }
}
