// 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.Common.I18n;
using Hiperscan.GtkExtensions.Widgets;
using Hiperscan.Unix;

//TODO additional test


namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        [Widget] Gtk.Label big_result_label = null;
        [Widget] Gtk.Label result_classification_label = null;
        [Widget] Gtk.Label result_nir_label = null;
        [Widget] Gtk.Label result_valuation_label = null;
        [Widget] Gtk.Label result_suggestion_label = null;
        [Widget] Gtk.Label result_comment_label = null;
        [Widget] Gtk.Label result_additional_test_label = null;

        [Widget] Gtk.Label result_classification_output_label = null;
        [Widget] Gtk.Label result_nir_output_label = null;
        [Widget] Gtk.Label result_valuation_output_label = null;
        [Widget] Gtk.Label result_additional_test_output_label = null;

        [Widget] Gtk.Entry result_comment_entry = null;
        [Widget] Gtk.Image result_ok_image = null;
        [Widget] Gtk.Image result_fail_image = null;


        private void UpdateResultSection()
        {
            bool active = this.Control.QuitRequested == false &&
                                this.Control.ConnectionSuccess &&
                                this.Control.Busy == false;

            this.result_suggestion_label.Sensitive = active;

            active &= this.Control.ProtocolState.HasFlag(ProtocolStates.IdentificationResultExists);

            this.big_result_label.Sensitive = active;
            this.result_classification_label.Sensitive = active;
            this.ResultClassificationOutputLabel.Sensitive = active;
            this.result_nir_label.Sensitive = active;
            this.result_nir_output_label.Sensitive = active;
            this.result_valuation_label.Sensitive = active;
            this.result_valuation_output_label.Sensitive = active;
            this.result_comment_label.Sensitive = active;
            this.result_comment_entry.Sensitive = active;
            this.result_additional_test_label.Sensitive = active;
            this.ResultAdditionlTestOutputLabel.Sensitive = active;

            bool clear = this.Control.ProtocolState.HasFlag(ProtocolStates.IdentificationResultExists) == false;
            if (clear == true)
            {
                string empty = "<b></b>";
                this.ResultClassificationOutputLabel.GtkLabel.Markup = empty;
                this.result_nir_output_label.Markup = empty;
                this.result_valuation_output_label.Markup = empty;

                if (this.Control.NoMisIdentificationYet == true)
                    this.ResultCommentEntry.GtkEntry.Text = String.Empty;
            }

            this.SetResultClassification(active);
            this.SetResultNir(active);
            this.SetResultValuation(active);
            this.SetResultAdditionalTestOutput(active);
            this.SetResultAdditionalTestLinkLabel();
            this.SetResultSuggestionLinkLabel();
            this.SetResultImage(active);
        }

        private void SetResultClassification(bool active)
        {
            this.ResultClassificationOutputLabel.BgColor = ApoIdentColors.WHITE;
            this.ResultClassificationOutputLabel.OkColor = ApoIdentColors.WHITE;

            if (active == false)
                return;

            string begin = "<b>";
            string end = "</b>";
            this.ResultClassificationOutputLabel.GtkLabel.Markup = begin + String.Empty + end;

            if (this.SubstanceManagement.CurrentSubstanceCategory == SubstanceCategory.Classifiaction)
            {
                this.ResultClassificationOutputLabel.GtkLabel.Markup = begin + this.SubstanceManagement.CurrentClassification.ClassificationName + end;
                this.ResultClassificationOutputLabel.BgColor = ApoIdentColors.SEARCH_ENTRY_YELLOW_BG;
                this.ResultClassificationOutputLabel.OkColor = ApoIdentColors.SEARCH_ENTRY_YELLOW_BG;
            }
            else
            {
                this.ResultClassificationOutputLabel.GtkLabel.Markup = begin + this.SubstanceManagement.CurrentSelectedName + end;
            }
        }

        private void SetResultNir(bool active)
        {
            if (active == false)
                return;

            string begin = "<b>";
            string end = "</b>";

            if (this.Control.CreateNotMeasurableReport == true)
            {
                this.result_nir_output_label.Markup = begin + Catalog.GetString("Not NIR active") + end;
                return;
            }

            if (this.IdentServerClient.IsTruePositive == true)
                this.result_nir_output_label.Markup = begin + Catalog.GetString("Comply") + end;
            else
                this.result_nir_output_label.Markup = begin + Catalog.GetString("Does not comply") + end;
        }

        private void SetResultValuation(bool active)
        {
            if (active == false)
                return;

            string begin = "<b>";
            string end = "</b>";

            if (this.Control.CreateNotMeasurableReport == true)
            {
                this.result_valuation_output_label.Markup = begin + Catalog.GetString("n/a") + end;
                return;
            }

            string rating = (this.IdentServerClient.Result.Rating * 100).ToString("f1", Locale.CurrentCulture);
            string min_rating = (this.IdentServerClient.Result.MinRating * 100).ToString("f0");

            if (this.IdentServerClient.IsTruePositive == false)
            {
                if (rating == (this.IdentServerClient.Result.MinRating * 100).ToString("f1", Locale.CurrentCulture))
                    rating = (this.IdentServerClient.Result.MinRating * 100 - 0.1).ToString("f1", Locale.CurrentCulture);
            }

            this.result_valuation_output_label.Markup = String.Format(Catalog.GetString("{0}{1}%{2} (set point {3}% to 100%)"), begin, rating, end, min_rating);
        }


        private void SetResultAdditionalTestOutput(bool active)
        {
            this.ResultAdditionlTestOutputLabel.IgnoreValidation = AdditionalTestEntries.IsMandatory(this) == false;

            string text = String.Empty;

            if (this.Config.Profiles.Current == null)
            {
                text = this.ResultAdditionlTestOutputLabel.PlaceHolderText;
            }
            else if (String.IsNullOrEmpty(this.Config.Profiles.Current.AdditionalCompletionEntries?.CustomerText))
            {
                text = this.ResultAdditionlTestOutputLabel.PlaceHolderText;
            }
            else
            {
                text = this.Config.Profiles.Current.AdditionalCompletionEntries.CustomerText;
            }

            this.ResultAdditionlTestOutputLabel.Text = text.Replace("\n", " ");
        }

        private void SetResultAdditionalTestLinkLabel()
        {
            this.ResultAdditionTestLinklabel.Text = AdditionalTestEntries.LabelName(this);
        }

        private void SetResultSuggestionLinkLabel()
        {
            this.ResultSuggestionLinklabel.Visible = false;
            if (this.SubstanceManagement.CurrentSubstanceCategory != SubstanceCategory.Classifiaction)
                return;

            if (String.IsNullOrEmpty(this.SubstanceManagement.CurrentSubstance.PdfLinkAdditionalTest))
                return;

            if (Locale.Language != "de")
                return;

            if (this.Config.IdentModuleSettings.Locale != Locale.LocaleType.de_DE)
                return;

            this.ResultSuggestionLinklabel.Visible = true;
            this.ResultSuggestionLinklabel.Sensitive = this.Control.Busy ==     false;
        }

        private void CheckAdditionalTestAsMandatory()
        {
            if (AdditionalTestEntries.IsMandatory(this))
            {
                string text = this.Config.Profiles.Current.AdditionalCompletionEntries.CustomerText;

                if (this.Control.ProtocolState.AdditionalTestRegex.Match(text).Success)
                    this.Control.ProtocolState.SetFlag(ProtocolStates.ResultAdditionalTestCorrect);
                else
                    this.Control.ProtocolState.DeleteFlag(ProtocolStates.ResultAdditionalTestCorrect);
            }
            else
            {
                this.Control.ProtocolState.SetFlag(ProtocolStates.ResultAdditionalTestCorrect);
            }
        }

        private void SetResultImage(bool active)
        {
            this.result_ok_image.Visible = false;
            this.result_fail_image.Visible = false;

            if (active == false || this.Control.CreateNotMeasurableReport == true)
                return;

            if (this.IdentServerClient.IsTruePositive == true)
                this.result_ok_image.Visible = true;
            else
                this.result_fail_image.Visible = true;
        }

        private OutLinedRegexLabel ResultAdditionlTestOutputLabel { get; set; }
        private LinkLabel ResultValuationLinklabel { get; set; }
        private LinkLabel ResultNirLinklabel { get; set; }
        private LinkLabel ResultSuggestionLinklabel { get; set; }
        private LinkLabel ResultAdditionTestLinklabel { get; set; }
        private OutLinedRegexEntry ResultCommentEntry { get; set; }
        private OutLinedRegexLabel ResultClassificationOutputLabel { get; set; }
    }
}
