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

namespace Hiperscan.GtkExtensions.Widgets
{
    public class OutLinedValidatedMonthYearBox
    {
        public static readonly DateTime INVALIDE_DATE = DateTime.MaxValue;

        public OutLinedValidatedMonthYearBox(Gtk.ComboBox year_box, int start_year, int years,
                                             Gtk.ComboBox month_box, CultureInfo culture)
        {
            this.MonthBox = new OutLinedValidatedMonthCombobox(month_box, culture);
            this.MonthBox.ErrorMessage = ValidatedWidget.DEFAULT_ERROR_MESSAGE;
            this.YearBox = new OutLinedValidatedYearCombobox(year_box, start_year, years);
            this.YearBox.ErrorMessage = ValidatedWidget.DEFAULT_ERROR_MESSAGE;
        }

        public bool SetDate(DateTime date)
        {
            bool success = this.YearBox.SetYear(date.Year);
            success = success & this.MonthBox.SetMonth(date.Month);

            if (success == false)
                this.Reset();

            return success;
        }

            //TODO
        public void Reset()
        {
            this.MonthBox.Reset();
            this.YearBox.Reset();
        }

        public bool Sensitive
        {
            set
            {
                this.MonthBox.Sensitive = value;
                this.YearBox.Sensitive = value;
            }
        }

        public DateTime CurrentDate
        {
            get
            {
                try
                {
                    int year = Int32.Parse(this.YearBox.GtkCombobox.ActiveText);

                    DateTime date = new DateTime(year, this.MonthBox.GtkCombobox.Active, 1);
                    date = date.AddMonths(1);
                    return date.AddTicks(-1);
                }
                catch(Exception)
                {
                    return OutLinedValidatedMonthYearBox.INVALIDE_DATE;
                }
            }
        }

        public bool IgnoreValidation
        {
            set
            {
                this.MonthBox.IgnoreValidation = value;
                this.YearBox.IgnoreValidation = value;
            }
        }

        public bool IsValid
        {
            get
            {
                return this.MonthBox.IsValid && this.YearBox.IsValid;
            }
        }
        public OutLinedValidatedMonthCombobox MonthBox { get; private set; }
        public OutLinedValidatedYearCombobox YearBox { get; private set; }
    }
}
