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

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

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

        [Widget] Gtk.Label result_name_content_label = null;
        [Widget] Gtk.Label result_classification_output_label = null;

        [Obsolete("Do not use this field")]
        [Widget] Gtk.Label result_nir_label = null;
        [Widget] Gtk.Label result_nir_output_label = null;
        [Widget] Gtk.Alignment result_nir_alignment = null;
        [Widget] Gtk.Alignment result_nir_output_alignment = null;

        [Obsolete("Do not use this field")]
        [Widget] Gtk.Label result_rating_label = null;
        [Widget] Gtk.Label result_rating_output_label = null;
        [Widget] Gtk.Alignment result_rating_alignment = null;
        [Widget] Gtk.Alignment result_rating_output_alignment = null;


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

        [Obsolete("Do not use this field")]
        [Widget] Gtk.Label result_additional_test_label = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.Label result_additional_test_output_label = null;
        [Obsolete("Do not use this field")]
        [Widget] Gtk.Label result_suggestion_label = null;

        [Widget] Gtk.Label result_thc_label = null;
        [Widget] Gtk.Label result_thc_output_label = null;
        [Widget] Gtk.Alignment result_thc_alignment = null;
        [Widget] Gtk.Alignment result_thc_output_alignment = null;

        [Widget] Gtk.Label result_cbd_label = null;
        [Widget] Gtk.Label result_cbd_output_label = null;
        [Widget] Gtk.Alignment result_cbd_alignment = null;
        [Widget] Gtk.Alignment result_cbd_output_alignment = null;




        private void UpdateResultSection(UpdateArgs update_arg)
        {
            if (update_arg.Result.HasFlag(UpdateCategory.Sensitives))
                this.SetResultSensitives();

            if (update_arg.Result.HasFlag(UpdateCategory.ValidStates))
                this.SetResultValids();

            if (update_arg.Result.HasFlag(UpdateCategory.SetValues))
            {
                this.SetResultClassificationOutputLabel();
                this.SetResultNirOutputLabel();
                this.SetResultRatingOutputLabel();
                this.SetResultCommentEntry();
                this.SetResultAdditionalTestLinkLabel();
                this.SetResultAdditionalTestOutputLabel();
                this.SetResultSuggestionLinkLabel();
                this.SetResultImage();
                this.SetResultTHCLabel();
                this.SetResultCBDLabel();
                this.SetResultVisibilities();
            }
        }

        private void SetResultSensitives()
        {
            bool sensitive = this.Control.GlobalStates.QuitRequested == false &&
                                 this.HasVisibleSubstances &&
                              this.Control.GlobalStates.Busy == false &&
                              this.Control.ResultManager.AnalysisResultExists;

            this.result_section_label.Sensitive = sensitive;
            this.result_name_content_label.Sensitive = sensitive;
            this.result_comment_label.Sensitive = sensitive;
            this.result_rating_output_label.Sensitive = sensitive;
            this.ResultClassificationOutputLabel.Sensitive = sensitive;
            this.ResultNirLinklabel.Sensitive = sensitive;
            this.result_nir_output_label.Sensitive = sensitive;
            this.ResultRatingLinklabel.Sensitive = sensitive;
            this.ResultCommentEntry.Sensitive = sensitive;
            this.ResultAdditionTestLinklabel.Sensitive = sensitive;
            this.ResultAdditionlTestOutputLabel.Sensitive = sensitive;
            this.result_ok_image.Sensitive = sensitive;
            this.result_fail_image.Sensitive = sensitive;

            this.result_cbd_label.Sensitive = sensitive;
            this.result_cbd_output_label.Sensitive = sensitive;
            this.result_thc_label.Sensitive = sensitive;
            this.result_thc_output_label.Sensitive = sensitive;

            this.ResultSuggestionLinklabel.Sensitive = this.Control.GlobalStates.Busy == false &&
                this.Control.MeasuringManager.IsReferencing == false;
        }

        private void SetResultValids()
        {
            this.ResultAdditionlTestOutputLabel.IsValid =  this.Control.ResultManager.AdditopalTestField.IsEntryValid;
        }

        private void SetResultClassificationOutputLabel()
        {
            this.ResultClassificationOutputLabel.Text = this.Control.ResultManager.GetClassificationName(true);

            if (this.Control.ResultManager.IsYellowSubstance())
                this.GuiDesigner.SetResultClassificationOutputColor(this.ResultClassificationOutputLabel, true);
            else
                this.GuiDesigner.SetResultClassificationOutputColor(this.ResultClassificationOutputLabel, false);
        }

        private void SetResultNirOutputLabel()
        {
            this.result_nir_output_label.Markup = this.Control.ResultManager.GetNirResultText(true);
        }

        private void SetResultRatingOutputLabel()
        {
            this.result_rating_output_label.Markup = this.Control.ResultManager.GetNirRatingText(true);
        }

        private void SetResultCommentEntry()
        {
            this.ResultCommentEntry.GtkEntry.Text = this.Control.ResultManager.GetCommentText();
        }

        private void SetResultAdditionalTestLinkLabel()
        {
            this.ResultAdditionTestLinklabel.Text = this.Control.ResultManager.GetAdditionalTestLabelName();
            this.ResultAdditionTestLinklabel.Tooltip = this.Control.ResultManager.GetAdditionalTestLabelTooltip();
        }

        private void SetResultAdditionalTestOutputLabel()
        {
            this.ResultAdditionlTestOutputLabel.Text = this.Control.ResultManager.GetAdditionalTestText();
        }

        private void SetResultSuggestionLinkLabel()
        {
            this.ResultSuggestionLinklabel.Visible = this.Control.ResultManager.HasSuggestion();
        }

        private void SetResultTHCLabel()
        {
            this.result_thc_output_label.Markup = this.Control.ResultManager.GetTHCValue(true);
        }

        private void SetResultCBDLabel()
        {
            this.result_cbd_output_label.Markup = this.Control.ResultManager.GetCBDValue(true);
        }

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

            if (this.IsQuantifierMode)
                return;

            bool? visibility = this.Control.ResultManager.GetNirEvaluation();

            this.result_ok_image.Visible = visibility == true;   
            this.result_fail_image.Visible = visibility == false;
        }

        private void SetResultVisibilities()
        {
            if (this.IsQuantifierMode == true)
            {
                this.result_thc_alignment.Visible = true;
                this.result_thc_output_alignment.Visible = true;
                this.result_cbd_alignment.Visible = true;
                this.result_cbd_output_alignment.Visible = true;

                this.result_nir_alignment.Visible = false;
                this.result_nir_output_alignment.Visible = false;
                this.result_rating_alignment.Visible = false;
                this.result_rating_output_alignment.Visible = false;
            }
            else
            {
                this.result_thc_alignment.Visible = false;
                this.result_thc_output_alignment.Visible = false;
                this.result_cbd_alignment.Visible = false;
                this.result_cbd_output_alignment.Visible = false;

                this.result_nir_alignment.Visible = true;
                this.result_nir_output_alignment.Visible = true;
                this.result_rating_alignment.Visible = true;
                this.result_rating_output_alignment.Visible = true;
            }
        }

        private OutLinedLabel ResultAdditionlTestOutputLabel { get; set; }
        private LinkLabel ResultRatingLinklabel { get; set; }
        private LinkLabel ResultNirLinklabel { get; set; }
        private LinkLabel ResultSuggestionLinklabel { get; set; }
        private LinkLabel ResultAdditionTestLinklabel { get; set; }
        private OutLinedEntry ResultCommentEntry { get; set; }
        private OutLinedLabel ResultClassificationOutputLabel { get; set; }
    }
}
