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

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        [Widget] Gtk.Label specimen_section_label = null;

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

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

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

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

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

        [Widget] Gtk.Label specimen_quantity_label = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.Entry specimen_quantity_entry = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ComboBox specimen_quantity_combobox = null;

        [Widget] Gtk.Label specimen_grace_label = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.Entry specimen_grace_entry = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.ComboBox specimen_grace_combobox = null;

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

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

        [Widget] Gtk.Label specimen_weighing_corr_label = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.Entry specimen_weighing_corr_entry = null;
        [Widget] Gtk.Alignment specimen_weighing_corr_name_alignment = null;
        [Widget] Gtk.Alignment speciment_weighing_corr_alignment = null;


        [Obsolete("Do not use this field")]
        [Widget] Gtk.Entry specimen_thc_entry = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.Entry specimen_cbd_entry = null;
        [Widget] Gtk.Alignment specimen_content_name_alignment = null;
        [Widget] Gtk.Alignment specimen_content_spacer_alignment = null;
        [Widget] Gtk.Alignment specimen_content_thc_alignment = null;
        [Widget] Gtk.Alignment specimen_content_cbd_alignment = null;
        [Widget] Gtk.Label specimen_thc_label = null;
        [Widget] Gtk.Label specimen_cbd_label = null;
        [Widget] Gtk.Label specimen_content_label = null;

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



        private void UpdateSpecimenSection(UpdateArgs update_arg)
        {
            if (update_arg.Specimen.HasFlag(UpdateCategory.Sensitives))
                this.SetSpecimenSensitives();

            if (update_arg.Specimen.HasFlag(UpdateCategory.ValidStates))
                this.SetSpecimenValids();

            if (update_arg.Specimen.HasFlag(UpdateCategory.SetValues))
                this.SetSpecimenValues();

            if (update_arg.Specimen.HasFlag(UpdateCategory.SpecialCases))
                this.SetSpecimenGracePeriodCombobox();

            if (update_arg.Specimen.HasFlag(UpdateCategory.Reset))
            {
                this.SetSpecimenValids();
                this.SetSpecimenSensitives();
                this.SetSpecimenGracePeriodCombobox();
                this.SetSpecimenValues();
                this.SetSpecimenVisibilities();
            }
        }

        private void SetSpecimenValids()
        {
            this.SpecimenTHCEntry.IsValid = this.Control.SpecimenManager.THC.IsEntryValid;
            this.SpecimenCBDEntry.IsValid = this.Control.SpecimenManager.CBD.IsEntryValid;
            this.SpecimenVarietyEntry.IsValid = this.Control.SpecimenManager.Variety.IsEntryValid;

            bool is_valid = this.Control.SubstanceManager.SubstancesCatalog.SelectionType == SubstanceSelectionType.ThirdParty;

            this.SpecimenPpnEntry.IsValid = this.Control.SpecimenManager.PPN.IsEntryValid || is_valid;
            this.SpecimenProducerEnntry.IsValid = this.Control.SpecimenManager.Producer.IsEntryValid || is_valid;
            this.SpecimenBatchEntry.IsValid = this.Control.SpecimenManager.Batch.IsEntryValid || is_valid;
            this.SpecimenQuantityEntry.IsValid = this.Control.SpecimenManager.ReferenceQuantity.IsQuantityValueValid || is_valid;
            this.SpecimenQuantityComboBox.IsValid = this.Control.SpecimenManager.ReferenceQuantity.IsQuantityUnitValid || is_valid;
            this.SpecimenDistributorEntry.IsValid = this.Control.SpecimenManager.Distributor.IsEntryValid || is_valid;
            this.SpecimenPriceEntry.IsValid = this.Control.SpecimenManager.Price.IsEntryValid || is_valid;
            this.SpecimenExpiryDateEntry.IsValid = this.Control.SpecimenManager.ExpiryDate.IsEntryValid || is_valid;
            this.SpecimenGracePeriodEntry.IsValid = this.Control.SpecimenManager.GracePeriod.IsQuantityValueValid || is_valid;
            this.SpecimenGracePeriodComboBox.IsValid = this.Control.SpecimenManager.GracePeriod.IsQuantityUnitValid || is_valid;
            this.SpecimenWeighingCorrEntry.IsValid = this.Control.SpecimenManager.CorrectionFactor.IsEntryValid || is_valid;
            this.SpecimenDeliveryDateEntry.IsValid = this.Control.SpecimenManager.DeliveryDate.IsEntryValid || is_valid;
        }

        private void SetSpecimenGracePeriodCombobox()
        {
            this.SpecimenGracePeriodComboBox.EnumComboBox.IsPlural = this.Control.SpecimenManager.GracePeriod.ValueWantsPlural;
        }

        private void SetSpecimenSensitives()
        {
            bool sensitive = this.Control.GlobalStates.QuitRequested == false &&
                                 this.HasVisibleSubstances &&
                                 this.Control.SubstanceManager.IsComplete &&
                                 this.Control.GlobalStates.Busy == false &&
                                 this.Control.MeasuringManager.IsReferencing == false;

            this.specimen_section_label.Sensitive = sensitive;
            this.specimen_ppn_label.Sensitive = sensitive;
            this.specimen_producer_label.Sensitive = sensitive;
            this.specimen_batch_label.Sensitive = sensitive;
            this.specimen_quantity_label.Sensitive = sensitive;
            this.specimen_distributor_label.Sensitive = sensitive;
            this.specimen_price_label.Sensitive = sensitive;
            this.specimen_grace_label.Sensitive = sensitive;
            this.specimen_weighing_corr_label.Sensitive = sensitive;
            this.specimen_delivery_date_label.Sensitive = sensitive;
            this.specimen_expiry_date_label.Sensitive = sensitive;
            this.specimen_content_label.Sensitive = sensitive;
            this.specimen_cbd_label.Sensitive = sensitive;
            this.specimen_thc_label.Sensitive = sensitive;
            this.specimen_variety_label.Sensitive = sensitive;

            bool third_party_unsaved = this.Control.ProtocolManager.DataExchangeSuccess == false;

            this.SpecimenTHCEntry.Sensitive = sensitive && third_party_unsaved;
            this.SpecimenCBDEntry.Sensitive = sensitive && third_party_unsaved;
            this.SpecimenVarietyEntry.Sensitive = sensitive && third_party_unsaved;

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

            this.SpecimenPpnEntry.Sensitive = sensitive;
            this.SpecimenProducerEnntry.Sensitive = sensitive;
            this.SpecimenBatchEntry.Sensitive = sensitive;
            this.SpecimenQuantityEntry.Sensitive = sensitive;
            this.SpecimenQuantityComboBox.Sensitive = sensitive;
            this.SpecimenDistributorEntry.Sensitive = sensitive;
            this.SpecimenPriceEntry.Sensitive = sensitive;
            this.SpecimenExpiryDateEntry.Sensitive = sensitive;
            this.SpecimenGracePeriodEntry.Sensitive = sensitive;
            this.SpecimenGracePeriodComboBox.Sensitive = sensitive;
            this.SpecimenWeighingCorrEntry.Sensitive = sensitive;
            this.SpecimenDeliveryDateEntry.Sensitive = sensitive;
        }

        private void SetSpecimenValues()
        {
            ListStore store = (ListStore)this.SpecimenProducerEnntry.GtkEntry.Completion.Model;
            store.Clear();
            this.Control.GuiSettings.ProducerCompletion.Sort();
            foreach (string producer in this.Control.GuiSettings.ProducerCompletion)
                store.AppendValues(producer);

            this.SpecimenPpnEntry.GtkEntry.Text = this.Control.SpecimenManager.PPN.GetEntryValue() ?? String.Empty;
            this.SpecimenProducerEnntry.GtkEntry.Text = this.Control.SpecimenManager.Producer.GetEntryValue() ?? String.Empty;
            this.SpecimenBatchEntry.GtkEntry.Text = this.Control.SpecimenManager.Batch.GetEntryValue() ?? String.Empty;
            this.SpecimenQuantityEntry.GtkEntry.Text = this.Control.SpecimenManager.ReferenceQuantity.GetEntryValue().QuantityValue ?? String.Empty;
            this.SpecimenQuantityComboBox.EnumComboBox.CurrentItem = this.Control.SpecimenManager.ReferenceQuantity.GetEntryValue().QuatityUnit;
            this.SpecimenDistributorEntry.GtkEntry.Text = this.Control.SpecimenManager.Distributor.GetEntryValue() ?? String.Empty;
            this.SpecimenPriceEntry.GtkEntry.Text = this.Control.SpecimenManager.Price.GetEntryValue() ?? String.Empty;
            this.SpecimenExpiryDateEntry.GtkEntry.Text = this.Control.SpecimenManager.ExpiryDate.GetEntryValue() ?? String.Empty;
            this.SpecimenGracePeriodEntry.GtkEntry.Text = this.Control.SpecimenManager.GracePeriod.GetEntryValue().QuantityValue ?? String.Empty;
            this.SpecimenGracePeriodComboBox.EnumComboBox.CurrentItem = this.Control.SpecimenManager.GracePeriod.GetEntryValue().QuantityUnit;
            this.SpecimenWeighingCorrEntry.GtkEntry.Text = this.Control.SpecimenManager.CorrectionFactor.GetEntryValue() ?? String.Empty;
            this.SpecimenDeliveryDateEntry.GtkEntry.Text = this.Control.SpecimenManager.DeliveryDate.GetEntryValue() ?? String.Empty;
            this.specimen_price_label.Text = this.Control.SpecimenManager.Price.Currency;
            this.SpecimenTHCEntry.GtkEntry.Text = this.Control.SpecimenManager.THC.GetEntryValue() ?? String.Empty;
            this.SpecimenCBDEntry.GtkEntry.Text = this.Control.SpecimenManager.CBD.GetEntryValue() ?? String.Empty;
            this.SpecimenVarietyEntry.GtkEntry.Text = this.Control.SpecimenManager.Variety.GetEntryValue() ?? String.Empty;
        }

        private void SetSpecimenVisibilities()
        {
            if (this.IsQuantifierMode)
            {
                this.specimen_content_name_alignment.Visible = true;
                this.specimen_content_spacer_alignment.Visible = true;
                this.specimen_content_cbd_alignment.Visible = true;
                this.specimen_content_thc_alignment.Visible = true;
                this.specimen_variety_label.Visible = true;
                this.SpecimenVarietyEntry.Visible = true;

                this.speciment_weighing_corr_alignment.Visible = false;
                this.specimen_weighing_corr_name_alignment.Visible = false;
            }
            else
            {
                this.specimen_content_name_alignment.Visible = false;
                this.specimen_content_spacer_alignment.Visible = false;
                this.specimen_content_cbd_alignment.Visible = false;
                this.specimen_content_thc_alignment.Visible = false;
                this.specimen_variety_label.Visible = false;
                this.SpecimenVarietyEntry.Visible = false;

                this.speciment_weighing_corr_alignment.Visible = true;
                this.specimen_weighing_corr_name_alignment.Visible = true;
            }
        }

        private OutLinedEntry SpecimenPpnEntry { get; set; }
        private OutLinedEntry SpecimenProducerEnntry { get; set; }
        private OutLinedCalendarEntry SpecimenExpiryDateEntry { get; set; }
        private OutLinedEntry SpecimenBatchEntry { get; set; }
        private OutLinedCalendarEntry SpecimenDeliveryDateEntry { get; set; }
        private OutLinedEntry SpecimenQuantityEntry { get; set; }
        private OutLinedEnumCombobox<ReferenceQuantityTypes> SpecimenQuantityComboBox { get; set; }
        private OutLinedEntry SpecimenGracePeriodEntry { get; set; }
        private OutLinedEnumCombobox<GracePeriodTypes> SpecimenGracePeriodComboBox { get; set; }
        private OutLinedEntry SpecimenDistributorEntry { get; set; }
        private OutLinedEntry SpecimenPriceEntry { get; set; }
        private OutLinedEntry SpecimenWeighingCorrEntry { get; set; }
        private OutLinedEntry SpecimenTHCEntry { get; set; }
        private OutLinedEntry SpecimenCBDEntry { get; set; }
        private OutLinedEntry SpecimenVarietyEntry { get; set; }
    }
}
