// 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.GtkExtensions.Widgets;
using Hiperscan.ApoIdentCore.GuiControl;
using Hiperscan.ApoIdentCore.SectionManager.Substance.SubstanceSelection;
using Hiperscan.ApoIdentCommon.ApoConfig;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        [Widget] Gtk.Label pharmacy_section_label = null;
        [Widget] Gtk.Label pharmacy_operator_label = null;
        [Widget] Gtk.ComboBox pharmacy_profile_combobox = null;

        [Obsolete("Do not use this field")]
        [Widget] Gtk.Entry pharmacy_operator_entry = null;


        private void UpdatePharmacySection(UpdateArgs update_arg)
        {
            if (update_arg.Pharmacy.HasFlag(UpdateCategory.ValidStates))
            {
                this.SetPharmacyValids();
            }

            if (update_arg.Pharmacy.HasFlag(UpdateCategory.Sensitives))
            {
                this.SetPharmacySensitives();
            }

            if (update_arg.Pharmacy.HasFlag(UpdateCategory.SetValues))
            {
                this.SetPharmacyProfileComboIndex();
                this.SetPharmacyOperatorEntry();
            }

            if (update_arg.Pharmacy.HasFlag(UpdateCategory.ReloadValues))
            {
                this.SetPharmacyProfileCombo();
            }
        }

        private void SetPharmacyValids()
        {
            this.PharmacyOperatorEntry.IsValid = this.Control.PharmacyManager.Operator.IsEntryValid ||
                this.Control.SubstanceManager.SubstancesCatalog.SelectionType == SubstanceSelectionType.ThirdParty; ;
        }

        private void SetPharmacySensitives()
        {
            bool sensitive = this.Control.GlobalStates.QuitRequested == false &&
                              this.Control.GlobalStates.Busy == false &&
                                 this.HasSubstances == true &&
                                 this.Control.MeasuringManager.IsReferencing == false;

            this.pharmacy_section_label.Sensitive = sensitive;
            this.pharmacy_operator_label.Sensitive = sensitive;

            sensitive &= this.Control.SubstanceManager.SubstancesCatalog.SelectionType != SubstanceSelectionType.ThirdParty; 

            this.pharmacy_profile_combobox.Sensitive = this.Control.ProfileManager.AllProfiles.Count > 1 && sensitive;
            this.PharmacyOperatorEntry.Sensitive = sensitive && this.HasVisibleSubstances;
        }

        private void SetPharmacyOperatorEntry()
        {
            ListStore store = (ListStore)this.PharmacyOperatorEntry.GtkEntry.Completion.Model;
            store.Clear();

            this.Control.GuiSettings.UserCompletion.Sort();
            foreach (string user in this.Control.GuiSettings.UserCompletion)
                store.AppendValues(user);

            this.PharmacyOperatorEntry.GtkEntry.Text = this.Control.PharmacyManager.Operator.GetEntryValue() ?? String.Empty;
        }

        private void SetPharmacyProfileComboIndex()
        {
            this.pharmacy_profile_combobox.Active = this.Control.ProfileManager.CurrentIndex(this.Control.ProfileManager.CurrentProfile);
        }

        private void SetPharmacyProfileCombo()
        {
            Gtk.ListStore store = (Gtk.ListStore)this.pharmacy_profile_combobox.Model;
            store.Clear();

            if (this.Control.ProfileManager.AllProfiles.Count > 0)
            {
                foreach (ProfileSettings item in this.Control.ProfileManager.AllProfiles.Values)
                    this.pharmacy_profile_combobox.AppendText(item.Name);

                this.SetPharmacyProfileComboIndex();
            }

            this.pharmacy_profile_combobox.Sensitive &= this.Control.ProfileManager.AllProfiles.Count > 1;
        }

        private OutLinedEntry PharmacyOperatorEntry { get; set; }
    }
}
