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

namespace Hiperscan.GtkExtensions.Widgets
{
    public abstract class OutLinedWidget
    {
        public event EventHandler ValueChanged = null;
        public static readonly string DEFAULT_ERROR_MESSAGE = Catalog.GetString("This input field is mandatory.");

        protected OutLinedWidget(Gtk.Widget widget)
        {
            this.Widget = widget;
            this.Init();
        }

        protected void Init()
        {
            Gtk.Container parent = this.Widget.Parent as Gtk.Container;
            parent.Remove(this.Widget);

            if ((parent is Gtk.Alignment) == false)
                throw new Exception("OutLinedValidatedWidget: use for the widget a Gtk.Alignment as parent object.");

            Gtk.Alignment alignment = new Gtk.Alignment(0.5f, 0.5f, 1.0f, 1.0f);
            this.BackGround.Add(this.Widget);
            alignment.Add(this.BackGround);
            this.Border.Add(alignment);
            parent.Add(this.Border);

            this.BorderWidth = 2;

            parent.ShowAll();
            this.Border.ShowAll();
            alignment.ShowAll();
            this.Widget.ShowAll();
            this.Refresh(false);
        }


        protected void ActivateOkState()
        {
            if (this.WidgetIsFocused == false)
                this.Border.ModifyBg(Gtk.StateType.Normal, this.ok_color);
            else
                this.Border.ModifyBg(Gtk.StateType.Normal, this.ok_focused_color);

            this.Widget.HasTooltip = false;
        }

        protected void ActivateErrorState()
        {
            if (this.WidgetIsFocused == false)
                this.Border.ModifyBg(Gtk.StateType.Normal, this.error_color);
            else
                this.Border.ModifyBg(Gtk.StateType.Normal, this.error_focused_color);

            this.Widget.HasTooltip = string.IsNullOrEmpty(this.Widget.TooltipText) == false;
        }

        protected void Refresh(bool only_redraw)
        {
            if (only_redraw == false)
            {
                if (this.IsValid)
                    this.ActivateOkState();
                else
                    this.ActivateErrorState();
            }

            this.Widget.QueueDraw();
        }

        protected void CallValueChangedEvent()
        {
            this.ValueChanged?.Invoke(this, EventArgs.Empty);
        }

        public string ErrorMessage
        {
            set
            {
                this.Widget.TooltipText = value;
            }
        }

        private bool is_valid = false;
        public bool IsValid
        {
            protected get
            {
                return this.is_valid;
            }
            set
            {
                if (this.is_valid == value)
                    return;

                this.is_valid = value;
                this.Refresh(false);
            }
        }

        public Gdk.Color BackGroundColer
        {
            set
            {
                this.BackGround.ModifyBg(Gtk.StateType.Normal, value);
            }
        }

        private Gdk.Color ok_color = new Gdk.Color(192, 192, 255);
        public Gdk.Color OkColor
        {
            set
            {
                this.ok_color = value;
                this.Refresh(true);
            }
        }

        private Gdk.Color ok_focused_color = new Gdk.Color(137, 137, 231);
        public Gdk.Color OkFocusedColor
        {
            set
            {
                this.ok_focused_color = value;
                this.Refresh(true);
            }
        }

        private Gdk.Color error_color = new Gdk.Color(255, 128, 128);
        public Gdk.Color ErrorColor
        {
            set
            {
                this.error_color = value;
                this.Refresh(true);
            }
        }

        private Gdk.Color error_focused_color = new Gdk.Color(255, 0, 0);
        public Gdk.Color ErrorFocusedColor
        {
            set
            {
                this.error_focused_color = value;
                this.Refresh(true);
            }
        }

        public uint BorderWidth
        {
            set
            {
                (this.BackGround.Parent as Gtk.Alignment).BorderWidth = value;
                this.Refresh(true);
            }
        }

        protected virtual bool WidgetIsFocused
        {
            get
            {
                return this.Widget.IsFocus;
            }
        }

        public bool Sensitive
        {
            set
            {
                this.Border.Sensitive = value;
            }
        }

        public bool Visible
        {
            set
            {
                this.Border.Visible = value;
            }
        }

        protected Gtk.EventBox BackGround { get; set; } = new Gtk.EventBox();
        protected Gtk.EventBox Border { get; set; } = new Gtk.EventBox();
        protected Gtk.Widget Widget { get; set; }
    }
}
