// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2018 Thomas Klose & 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 System.IO;
using Glade;

using Hiperscan.Spectroscopy.IdentModule;


namespace Hiperscan.Extensions.ApoIdent
{
    public partial class FinderWindow
    {
        [Widget] Gtk.Alignment report_align1 = null;
        [Widget] Gtk.Alignment report_align2 = null;

        [Widget] Gtk.Button save_report_button = null;
        [Widget] Gtk.Button print_label_button = null;

        [Widget] Gtk.Image missing_labelprinter_image       = null;
        [Widget] Gtk.Image missing_archivedirectory_image   = null;

        [Widget] Gtk.Image summarized_image                 = null;
        [Widget] Gtk.LinkButton summarized_info_linkbutton  = null;


        private void UpdateReportSection(bool set_cursor_to_search)
        {
            if (ApoIdentEnvs.DebugOn)
                Console.WriteLine((DetailSectionStates)this.DetailSectionState);

            bool section_active = this.DetailSectionState.IsSet(DetailSectionStates.ReportActive);
            bool create_report = this.DetailSectionState.IsSet(DetailSectionStates.CreateReport);
            bool save = this.DetailSectionState.IsSet(DetailSectionStates.SaveReport);
            bool show_multi = this.DetailSectionState.IsSet(DetailSectionStates.MultipleSubstanceActive);

            bool label_print = this.DetailSectionState.IsSet(DetailSectionStates.LabelPrintActive);
            bool show_missing_archive = section_active && this.DetailSectionState.IsSet(DetailSectionStates.ArchiveExists) == false;
            bool show_missing_printer = create_report && this.DetailSectionState.IsSet(DetailSectionStates.LabelPrintActive) == false;

            this.report_align1.Sensitive = section_active;
            this.report_align2.Sensitive = create_report;

            this.save_report_button.Sensitive = save;
            this.print_label_button.Sensitive = label_print;

            this.missing_archivedirectory_image.Visible = show_missing_archive;
            this.missing_labelprinter_image.Visible = show_missing_printer;

            this.summarized_image.Visible = show_multi;
            this.summarized_info_linkbutton.Visible = show_multi;

            if (set_cursor_to_search == true && section_active == true)
                this.SetFocusAfterSaving();
                
        }

        private void InitReportSectionState()
        {
            this.DetailSectionState.Reset();
            this.DetailSectionState.Set(DetailSectionStates.UserFieldCorrect, this.user_regexentry.IsTextValid);
            this.DetailSectionState.Set(DetailSectionStates.SupplierFieldCorrect, this.supplier_regexentry.IsTextValid);
            this.DetailSectionState.Set(DetailSectionStates.BatchNumberFieldCorrect, this.batch_regexentry.IsTextValid);
            this.DetailSectionState.Set(DetailSectionStates.PznFieldCorrect, this.pzn_regexentry.IsTextValid);
            this.DetailSectionState.Set(DetailSectionStates.ExpiryDateMonthlyFieldCorrect, this.expiry_date_month_combo.Active > 0);
            this.DetailSectionState.Set(DetailSectionStates.ExpiryDateYearFieldCorrect, this.expiry_date_year_combo.Active > 0);
            this.DetailSectionState.Set(DetailSectionStates.HasResult, false);
            this.DetailSectionState.Set(DetailSectionStates.IsMutipleSubstamce, this.SubstanceManagement.CurrentSubstance != null &&
                                        this.SubstanceManagement.CurrentClassification.ClassificationType == ClassificationType.Multiple);
            
            this.DetailSectionState.Set(DetailSectionStates.ArchiveExists, Directory.Exists(this.Config.Profiles.Current.Archive));
            this.DetailSectionState.Set(DetailSectionStates.HasLabelPrinter, this.Config.PrinterSettings.HasPrinter);
            this.DetailSectionState.Set(DetailSectionStates.ReportNotSaved, true);
        }

        private void FocusSpecimenSearchEntry()
        {
            if (this.substance_search_entry.Text.Length > 0)
                this.substance_search_entry.SelectRegion(0, this.substance_search_entry.Text.Length);
            this.substance_search_entry.HasFocus = true;
        }

        private void SetFocusAfterSaving()
        {
            this.substance_search_entry.GrabFocus();
        }

        public DetailSectionUpdateState DetailSectionState { get; private set; } = new DetailSectionUpdateState();
    }
}
