// 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.Extensions.ApoIdentCommon.Scanner;
using Hiperscan.GtkExtensions.Widgets;
using Hiperscan.Spectroscopy.IdentModule;
using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        [Widget] Gtk.Label big_specimen_label = null;
        [Widget] Gtk.Label specimen_ppn_label = null;
        [Widget] Gtk.Label specimen_supplier_label = null;
        [Widget] Gtk.Label specimen_batch_label = null;
        [Widget] Gtk.Label specimen_expiry_date_label = null;
        [Widget] Gtk.Label specimen_weighing_corr_label = null;

        [Widget] Gtk.Entry specimen_ppn_entry = null;
        [Widget] Gtk.Entry specimen_supplier_entry = null;
        [Widget] Gtk.Entry specimen_batch_entry = null;
        [Widget] Gtk.Entry specimen_weighing_corr_entry = null;

        [Widget] Gtk.ComboBox specimen_month_combobox = null;
        [Widget] Gtk.ComboBox specimen_year_combobox = null;


        private void UpdateSpecimenSection(bool init_specimen_section)
        {
            bool active = this.Control.QuitRequested == false &&
                this.Control.ConnectionSuccess &&
                              this.Control.Busy == false &&
                              this.Control.MeasuringProgressState.IsProcessing == false &&
                              this.SubstanceManagement.CurrentSubstance != null;

            this.big_specimen_label.Sensitive = active;
            this.specimen_ppn_label.Sensitive = active;
            this.specimen_batch_label.Sensitive = active;
            this.specimen_weighing_corr_label.Sensitive = active;
            this.specimen_supplier_label.Sensitive = active;
            this.specimen_expiry_date_label.Sensitive = active;

            if (init_specimen_section == true)
            {
                this.SpecimenSupplierEntry.GtkEntry.Text = String.Empty;
                this.SpecimenBatchEntry.GtkEntry.Text = String.Empty;
                this.SpecimenPpnEntry.GtkEntry.Text = String.Empty;
                this.SpecimenWeighingCorrEntry.GtkEntry.Text = String.Empty;
                this.SpecimenExpiryDateBox.Reset();
            }

            this.SetSpecimentSupplierEntry(active);
            this.SetSpecimenBatchEntry(active);
            this.SetSpecimenPpnEntry(active);
            this.SetSpecimenCorrFacEntry(active);
            this.SetSpecimentExpiryDateLinkLabel(active);
            this.SetSpecimenExpiryDate(active);
        }

        private void SetSpecimentSupplierEntry(bool active)
        {
            switch (this.SubstanceManagement.ProtocolCompletion)
            {
                case ProtocolAutoCompletionType.Scanner:
                    string text = (this.Scanner.ScannerInfo.SupplierName ?? String.Empty).Trim();
                    if (string.IsNullOrEmpty(text) == false)
                        this.SpecimenSupplierEntry.GtkEntry.Text = text;
                    this.SpecimenSupplierEntry.Sensitive = active && String.IsNullOrEmpty(text);
                    this.SpecimenSupplierEntry.IgnoreValidation = false;
                    break;
                     
                case ProtocolAutoCompletionType.ThirdParty:                   
                    string key = this.Config.Profiles.Current.ExchangeData.CurrentDataSetKey;
                    this.SpecimenSupplierEntry.GtkEntry.Text = this.Config.Profiles.Current.ExchangeData[key].Supplier;
                    this.SpecimenSupplierEntry.Sensitive = false;
                    this.SpecimenSupplierEntry.IgnoreValidation = true;
                    break;

                default:
                    if (this.SubstanceManagement?.CurrentModelClass != null &&
                        String.IsNullOrEmpty(this.SubstanceManagement.CurrentModelClass.DefaultSupplierName) == false)
                    {
                        this.SpecimenSupplierEntry.GtkEntry.Text = this.SubstanceManagement.CurrentModelClass.DefaultSupplierName;
                        this.SpecimenSupplierEntry.Sensitive = false;
                    }
                    else
                    {
                        this.SpecimenSupplierEntry.Sensitive = active;
                    }
                    this.SpecimenSupplierEntry.IgnoreValidation = false;
                    break;
            }
        }

        private void SetSpecimenBatchEntry(bool active)
        {
            switch (this.SubstanceManagement.ProtocolCompletion)
            {
                case ProtocolAutoCompletionType.Scanner:
                    string text = (this.Scanner.ScannerInfo.BatchNumber ?? String.Empty).Trim();
                    if (string.IsNullOrEmpty(text) == false)
                        this.SpecimenBatchEntry.GtkEntry.Text = text;
                    this.SpecimenBatchEntry.Sensitive = active && String.IsNullOrEmpty(text);
                    this.SpecimenBatchEntry.IgnoreValidation = false;
                    break;

                case ProtocolAutoCompletionType.ThirdParty:                    
                    string key = this.Config.Profiles.Current.ExchangeData?.CurrentDataSetKey;
                    if (string.IsNullOrEmpty(key) == false)
                        this.SpecimenBatchEntry.GtkEntry.Text = this.Config.Profiles.Current.ExchangeData[key].BatchNumber;
                    else
                        this.SpecimenBatchEntry.GtkEntry.Text = String.Empty;
                    this.SpecimenBatchEntry.Sensitive = false;
                    this.SpecimenBatchEntry.IgnoreValidation = false;
                    break;

                default:
                    bool is_non_multiple = true;
                    if (this.SubstanceManagement == null || this.SubstanceManagement.CurrentClassification == null)
                        is_non_multiple = false;
                    else
                        is_non_multiple = this.SubstanceManagement.CurrentClassification.ClassificationType == ClassificationType.NonMultiple;

                    if (is_non_multiple &&
                        String.IsNullOrEmpty(this.SubstanceManagement?.CurrentBatchNumber) == false)
                    {
                        this.SpecimenBatchEntry.GtkEntry.Text = this.SubstanceManagement.CurrentBatchNumber;
                        this.SpecimenBatchEntry.Sensitive = false;
                    }
                    else
                    {
                        this.SpecimenBatchEntry.Sensitive = active;
                    }
                    this.SpecimenBatchEntry.IgnoreValidation = false;
                    break;
            }
        }

        private void SetSpecimenPpnEntry(bool active)
        {
            switch (this.SubstanceManagement.ProtocolCompletion)
            {
                case ProtocolAutoCompletionType.Scanner:
                    string text = (this.Scanner.ScannerInfo.Ppn ?? String.Empty).Trim();
                    if (string.IsNullOrEmpty(text) == false)
                        this.SpecimenPpnEntry.GtkEntry.Text = text;
                    this.SpecimenPpnEntry.Sensitive = active && String.IsNullOrEmpty(text);
                    this.SpecimenPpnEntry.IgnoreValidation = false;
                    break;

                case ProtocolAutoCompletionType.ThirdParty:
                    string key = this.Config.Profiles.Current.ExchangeData.CurrentDataSetKey;
                    this.SpecimenPpnEntry.GtkEntry.Text = this.Config.Profiles.Current.ExchangeData[key].PZN;
                    this.SpecimenPpnEntry.Sensitive = false;
                    this.SpecimenPpnEntry.IgnoreValidation = true;
                    break;

                default:
                    this.SpecimenPpnEntry.Sensitive = active;
                    this.SpecimenPpnEntry.IgnoreValidation = false;
                    break;
            }
        }

        private void SetSpecimenCorrFacEntry(bool active)
        {
            switch (this.SubstanceManagement.ProtocolCompletion)
            {
                case ProtocolAutoCompletionType.Scanner:
                    string text = (this.Scanner.ScannerInfo.WeightingCorrection ?? String.Empty).Trim();
                    if (string.IsNullOrEmpty(text) == false)
                        this.SpecimenWeighingCorrEntry.GtkEntry.Text = text;
                    this.SpecimenWeighingCorrEntry.Sensitive = active && String.IsNullOrEmpty(text);
                    this.SpecimenWeighingCorrEntry.IgnoreValidation = false;
                    break;

                case ProtocolAutoCompletionType.ThirdParty:
                    string key = this.Config.Profiles.Current.ExchangeData.CurrentDataSetKey;
                    this.SpecimenWeighingCorrEntry.GtkEntry.Text = this.Config.Profiles.Current.ExchangeData[key].WeightCorrFactor ?? String.Empty;
                    this.SpecimenWeighingCorrEntry.Sensitive = false;
                    this.SpecimenWeighingCorrEntry.IgnoreValidation = false;
                    break;

                default:
                    this.SpecimenWeighingCorrEntry.Sensitive = active;
                    this.SpecimenWeighingCorrEntry.IgnoreValidation = false;
                    break;
            }
        }

        private void SetSpecimentExpiryDateLinkLabel(bool active)
        {
            if (this.SubstanceManagement.ProtocolCompletion == ProtocolAutoCompletionType.ThirdParty)
            {
                this.specimen_expiry_date_label.Sensitive = false;

                string key = this.Config.Profiles.Current.ExchangeData.CurrentDataSetKey;
                if (this.Config.Profiles.Current.ExchangeData[key].TypeOfDate == "Verfallsdatum")
                    BatchDeadLine.CurrentDeadLineType = BatchDeadLine.DeadLineType.UseByDate;
                else
                    BatchDeadLine.CurrentDeadLineType = BatchDeadLine.DeadLineType.ShelfLife;
            }
            else
            {
                this.specimen_expiry_date_label.Sensitive = active;
            }

            this.SpecimenExpiryDateinklabel.Text = BatchDeadLine.GetCurrentLabelText;
        }

        private void SetSpecimenExpiryDate(bool active)
        {
            switch (this.SubstanceManagement.ProtocolCompletion)
            {
                case ProtocolAutoCompletionType.Scanner:
                    bool success = this.SpecimenExpiryDateBox.SetDate(this.Scanner.ScannerInfo.ExpirationDate);
                    this.SpecimenExpiryDateBox.Sensitive = success == false && active;
                    this.SpecimenExpiryDateBox.IgnoreValidation = success == false;
                    break;

                case ProtocolAutoCompletionType.ThirdParty:
                    string key = this.Config.Profiles.Current.ExchangeData.CurrentDataSetKey;
                    if (this.Config.Profiles.Current.ExchangeData[key].TypeOfDate == "Verfallsdatum")
                    {
                        this.SpecimenExpiryDateinklabel.Text = Catalog.GetString("Use-by date:");
                        BatchDeadLine.CurrentDeadLineType = BatchDeadLine.DeadLineType.UseByDate;
                    }
                    else
                    {
                        this.SpecimenExpiryDateinklabel.Text = Catalog.GetString("Shelf life:");
                        BatchDeadLine.CurrentDeadLineType = BatchDeadLine.DeadLineType.ShelfLife;
                    }

                    this.SpecimenExpiryDateBox.SetDate(this.Config.Profiles.Current.ExchangeData[key].ExpiryDate);
                    this.SpecimenExpiryDateBox.Sensitive = false;
                    this.specimen_expiry_date_label.Sensitive = false;
                    this.SpecimenExpiryDateBox.IgnoreValidation = true;
                    break;

                default:
                    this.SpecimenExpiryDateBox.Sensitive = active;
                    this.specimen_expiry_date_label.Sensitive = active;
                    this.SpecimenExpiryDateBox.IgnoreValidation = false;
                    break;
            }
        }

        private LinkLabel SpecimenExpiryDateinklabel { get; set; }
        private OutLinedRegexEntry SpecimenPpnEntry { get; set; }
        private OutLinedRegexEntry SpecimenBatchEntry { get; set; }
        private OutLinedRegexEntry SpecimenSupplierEntry { get; set; }
        private OutLinedRegexEntry SpecimenWeighingCorrEntry { get; set; }
        private OutLinedValidatedMonthYearBox SpecimenExpiryDateBox { get; set; }
    }
}
