// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2018 Thomas Klose & 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.Spectroscopy.IdentModule;
using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{
    public partial class FinderWindow
    {
        [Widget] Gtk.Alignment result_align1 = null;
        [Widget] Gtk.Alignment result_align2 = null;

        [Widget] Gtk.Label name_label = null;
        [Widget] Gtk.Label latin_label = null;
        [Widget] Gtk.Label result_label = null;
        [Widget] Gtk.Label significance1_label = null;
        [Widget] Gtk.Label significance2_label = null;

        [Widget] Gtk.Button result_substance_category_button = null;
        [Widget] Gtk.Viewport result_viewport                = null;

        [Widget] Gtk.Button name_label_button               = null;
        [Widget] Gtk.LinkButton nir_result_linkbutton       = null;
        [Widget] Gtk.LinkButton rating_linkbutton           = null;
        [Widget] Gtk.LinkButton validation_linkbutton       = null;
        [Widget] Gtk.LinkButton detection_rate_linkbutton   = null;



        private void UpdateResultSection()
        {
            bool active = (this.InitState == ServerDeviceState.StartIdentModuleSuccess ||
                            this.InitState == ServerDeviceState.Success) &&
                            this.SubstanceManagement.CurrentSubstance != null &&
                            (this.IdentServerClient.HasValidResult || this.CreateNotMeasurableReport == true);
            
            this.result_align1.Sensitive = active;
            this.result_align2.Sensitive = active;

            this.SetButtonStyle(WindowSections.ResultSection, active);

            this.UpdateResultName(active);
            this.UpdateNirResult(active);
            this.UpdateEvaluation(active);
            this.UpdateSignificanceLevel(active);
            this.ResetLinkButons();
        }

        private void UpdateResultName(bool active)
        {
            string begin = "<span size=\"xx-large\" weight=\"bold\">";
            string end = "</span>";
            this.name_label.Markup = begin + " " + end;

            this.result_substance_category_button.Visible = false;

            if (active == false)
                return;

            if (this.SubstanceManagement.CurrentClassification.ClassificationType == ClassificationType.Multiple)
            {
                begin += $"<span bgcolor=\"{FinderWindow.GdkColorToPango(this.ClassificationColor)}\">";
                end   += "</span>";
                this.result_substance_category_button.Visible = true;
                this.name_label.Markup = begin + this.SubstanceManagement.CurrentClassification.ClassificationName + end;
            }
            else
            {
                this.name_label.Markup = begin + this.SubstanceManagement.CurrentSubstance.SelectedDefaultName + end;
            }
        }

        private void UpdateNirResult(bool active)
        {
            string begin = "<span size=\"xx-large\" weight=\"bold\">";
            string end = "</span>";
            this.latin_label.Markup = begin + " " + end;

            if (active == false)
                return;

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

            if (this.IdentServerClient.IsTruePositive == true)
            {
                string text = Catalog.GetString("Comply");
                if (this.SubstanceManagement.CurrentClassification.ClassificationType == ClassificationType.Multiple)
                    text += " " + Catalog.GetString("(Additional Test)");

                this.latin_label.Markup = begin + text + end;
            }
            else
                this.latin_label.Markup = begin + Catalog.GetString("Does not comply") + end;
        }

        private void UpdateEvaluation(bool active)
        {
            string begin = "<span size=\"xx-large\" weight=\"bold\">";
            string end = "</span>";
            this.result_label.Markup = begin + " " + end;

            if (active == false)
                return;

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

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

            if (this.IdentServerClient.IsTruePositive == false)
            {
                if (rating == (this.IdentServerClient.Result.MinRating * 100).ToString("f1"))
                    rating = (this.IdentServerClient.Result.MinRating * 100 - 0.1).ToString("f1");
                if (this.no_misidentification_yet == true)
                {
                    this.ShowHintMessage();
                    this.no_misidentification_yet = false;
                }
            }

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

        private void UpdateSignificanceLevel(bool active)
        {
            this.significance1_label.Text = String.Empty;
            this.significance2_label.Text = String.Empty;
            this.detection_rate_linkbutton.Label = String.Empty;
            this.detection_rate_linkbutton.Visible = false;

            if (active == false)
                return;

            if (this.CreateNotMeasurableReport == true)
            {
                this.significance1_label.Text = Catalog.GetString("Not available");
                return;
            }

            if (this.SubstanceManagement.CurrentModelClass.ValidationDocument == false)
            {
                this.significance1_label.Text = Catalog.GetString("Not available");
                return;
            }


            if (this.SubstanceManagement.CurrentClassification.ClassificationType == ClassificationType.NonMultiple || ApoIdentEnvs.DebugOn)
            {
                if (String.IsNullOrEmpty(this.IdentServerClient.Result.SignificanceLevel) == true)
                {
                    this.significance1_label.Text = Catalog.GetString("Available");
                }
                else
                {
                    string[] values = this.IdentServerClient.Result.SignificanceLevel.Split(new char[] { ' ' });
                    this.significance1_label.Text = values[0];
                    this.significance2_label.Text = "(" + values[1] + Catalog.GetString(" statistical forecast)");
                    if (this.IdentServerClient.IsTruePositive == true)
                        this.detection_rate_linkbutton.Label = Catalog.GetString("specificity");
                    else
                        this.detection_rate_linkbutton.Label = Catalog.GetString("detection rate");
                    this.detection_rate_linkbutton.Visible = true;
                }
            }
            else
            {
                this.significance1_label.Text = Catalog.GetString("Available");
            }
        }
    }
}
