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

namespace Hiperscan.GtkExtensions.Widgets
{
    public class OutLinedLabel : OutLinedWidget
    {
        public OutLinedLabel(Gtk.Label label)  : base(label)
        {
            this.GtkLabel.UseMarkup = false;
            this.XPadding = 5;
        }

        public void ScaleFont(double factor)
        {
            Pango.FontDescription fd = this.GtkLabel.Style.FontDesc.Copy();
            fd.Size = (int)(fd.Size * factor);
            this.GtkLabel.ModifyFont(fd);
        }

        private void SetEntryText()
        {
            string text = this.PlaceHolderText;

            if (String.IsNullOrEmpty(this.Text) == false)
                text = this.Text;

            if (this.GtkLabel.UseMarkup)
                this.GtkLabel.Markup = text;
            else
                this.GtkLabel.Text = text;
        }


        protected override bool WidgetIsFocused
        {
            get
            {
                return false;
            }
        }

        public int Height
        {
            set
            {
                this.GtkLabel.HeightRequest = value;
            }
        }

        public int XPadding
        {
            set
            {
                this.GtkLabel.Xpad = value;
            }
        }

        private string place_holder_text = String.Empty;                                     
        public string PlaceHolderText
        { 
            private get
            {
                return this.place_holder_text;
            }
            set
            {
                value = value ?? String.Empty;
                if (this.place_holder_text == value)
                    return;

                this.place_holder_text = value;
                this.SetEntryText();
            }
        }

        private string text = String.Empty;
        public string Text 
        { 
            private get
            {
                return this.text;
            }
            set
            {
                value = (value ?? String.Empty).Trim();

                if (this.text == value)
                    return;

                this.text = value;
                this.SetEntryText();
                this.CallValueChangedEvent();

            }
        }

        public bool UseMarkup
        {
            set
            {
                this.GtkLabel.UseMarkup = value;
                this.SetEntryText();
            }
        }

        private Gtk.Label GtkLabel
        {
            get
            {
                return this.Widget as Gtk.Label;
            }
        }
    }
}
