// 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.Unix;
using Hiperscan.ApoIdentCore;
using Hiperscan.ApoIdentCommon.ApoConfig.Cache;
using Hiperscan.AnalysisModule;
using Hiperscan.GtkExtensions.Widgets;

namespace Hiperscan.Extensions.ApoIdent
{
    public class AdditionalTestDialog
    {
        [Widget] Gtk.Dialog additional_test_dialog        = null;
        [Widget] Gtk.TextView additional_textview        = null;
        [Widget] Gtk.CheckButton customer_evaluation_checkbutton    = null;
        [Widget] Gtk.Label final_result_label            = null;
        [Widget] Gtk.Button ok_button = null;
        [Widget] Gtk.Button cancel_button = null;

        [Widget] Gtk.HBox warning_hbox = null;
        [Widget] Gtk.Label warning_label = null;
        [Widget] Gtk.LinkButton suggestion_linkbutton = null;


        public static AdditionalTestDialog Run(Gtk.Window parent_window, AppControl control)
        {
            return new AdditionalTestDialog(parent_window, control);
        }

        private AdditionalTestDialog(Gtk.Window parent_window, AppControl control)
        {
            this.DocumentManager = control.DocumentManager;
            this.AdditionalTest = control.ResultManager.CurrentAdditionalTest;
            this.CustomerText = this.AdditionalTest.CurrentValue;
            this.CustomerEvaluation = this.AdditionalTest.CustomerEvalution;
            if (this.AdditionalTest.IsGreenSubstance && this.AdditionalTest.TruePositive)
                this.CustomerEvaluation = true;

            Glade.XML gui = new Glade.XML(null, "Hiperscan.Extensions.ApoIdent.Dialogs.AdditionalTestDialog.additional_test_dialog.glade", "additional_test_dialog", "Hiperscan");
            gui.Autoconnect(this);

            WidgetColor.SetFontColor(this.warning_label, GuiDesigner.ApoIdentColors.HIPERSCAN_RED);
            this.suggestion_linkbutton.Visible = control.ResultManager.HasSuggestion();
            this.suggestion_linkbutton.Clicked += this.OnResultSuggestionButtonClicked;

            if (this.AdditionalTest.AnalysisType == AnalysisType.Quantification)
            {
                this.final_result_label.Text = Catalog.GetString("Documentation:");
                this.customer_evaluation_checkbutton.Label = Catalog.GetString("Completed");
            }
            else
            {
                this.final_result_label.Text = Catalog.GetString("Final result:");
                this.customer_evaluation_checkbutton.Label = Catalog.GetString("Identified");
            }

            ApoIdent.ApoIdentApplication.SetWmcClassName(this.additional_test_dialog);

            this.additional_test_dialog.WidthRequest = 800;
            this.additional_test_dialog.HeightRequest =  600;
            this.additional_test_dialog.TransientFor = parent_window;

            this.additional_test_dialog.Title = String.Format(Catalog.GetString("{0} (methods and results)"),
                                                              control.ResultManager.GetTypeOfAdditionalTest());

            this.additional_test_dialog.Destroyed += this.OnAdditionalTestCancelButtonClicked;

            this.additional_textview.Buffer.Text = this.CustomerText;
            this.Update();

            this.additional_textview.Buffer.Changed += this.OnTextChanged;
            this.warning_hbox.Visible = this.AdditionalTest.AnalysisType == AnalysisType.Quantification;

            GuiDesigner.SetSchema(this.additional_test_dialog);

            this.additional_test_dialog.ShowAll();
            this.additional_textview.GrabFocus();
            this.additional_test_dialog.Run();

            if (additional_test_dialog != null)
                this.additional_test_dialog.Destroy();

            this.SetProtocolState();
        }

        private void OnResultSuggestionButtonClicked(object sender, EventArgs e)
        {
            this.DocumentManager.ShowSuggestionPdf();
        }

        private void OnTextChanged(object sender, EventArgs e)
        {
            if (this.IsUpdating)
                return;

            this.IsUpdating = true;
            this.CustomerText = this.additional_textview.Buffer.Text.Trim();
            this.CustomerEvaluation &= this.HasText;
            this.Update();
            this.IsUpdating = false;
        }

        private void OnCustomerEvaluationToggled(object sender, EventArgs e)
        {
            if (this.IsUpdating)
                return;

            this.IsUpdating = true;
            this.CustomerEvaluation = this.customer_evaluation_checkbutton.Active;
            this.Update();
            this.IsUpdating = false;
        }

        private void OnAdditionalTestCloseButtonClicked(object sender, EventArgs e)
        {
            this.Confirmed = true;
            this.additional_test_dialog.Destroy();
            this.additional_test_dialog = null;
        }
        
        private void OnAdditionalTestCancelButtonClicked(object sender, EventArgs e)
        {  
            this.additional_test_dialog.Destroy();
            this.additional_test_dialog = null;
        }

     
        private void Update()
        {
            this.UpdateTextView();
            this.UpdateResultCheckButton();
            this.UpdateOkButton();
            this.UpdateCancelButton();
        }

        private void UpdateTextView()
        {
            this.additional_textview.Sensitive = this.AdditionalTest.QuantificationConfirmed == false;
        }

        private void UpdateResultCheckButton()
        {
            if (this.AdditionalTest.AnalysisType == AnalysisType.Quantification)
            {
                this.customer_evaluation_checkbutton.Sensitive = this.HasText && this.AdditionalTest.QuantificationConfirmed == false;
                this.final_result_label.Sensitive = this.HasText && this.AdditionalTest.QuantificationConfirmed == false;

                if (this.HasText == false)
                    this.customer_evaluation_checkbutton.Active = false;
            }
            else
            {
                this.customer_evaluation_checkbutton.Active = this.CustomerEvaluation;
                if (this.AdditionalTest.IsGreenSubstance && this.AdditionalTest.TruePositive)
                {
                    this.customer_evaluation_checkbutton.Sensitive = false;
                    this.final_result_label.Sensitive = false;
                }
                else
                {
                    this.customer_evaluation_checkbutton.Sensitive = this.HasText;
                    this.final_result_label.Sensitive = this.HasText;
                }
            }
        }

        private void UpdateOkButton()
        {
            if (this.AdditionalTest.AnalysisType == AnalysisType.Quantification)
            {
                if (this.AdditionalTest.AnalysisType == AnalysisType.Quantification)
                    this.ok_button.Sensitive = this.HasText && this.CustomerEvaluation;
                else
                    this.ok_button.Sensitive = this.HasText;
            }
            else
            {
                this.ok_button.Sensitive = this.HasText;
            }
        }

        private void UpdateCancelButton()
        {
            if (this.AdditionalTest.AnalysisType == AnalysisType.Quantification)
            {
                this.cancel_button.Sensitive = this.AdditionalTest.QuantificationConfirmed == false;
            }
        }



        private void SetProtocolState()
        {

            this.AdditionalTest.CurrentValue = this.CustomerText;
            this.AdditionalTest.CustomerEvalution = this.CustomerEvaluation;

            if (this.AdditionalTest.QuantificationConfirmed == false)
            {
                this.AdditionalTest.QuantificationConfirmed = this.Confirmed &&
                    this.AdditionalTest.AnalysisType == AnalysisType.Quantification &&
                    this.AdditionalTest.CustomerEvalution == true;
            }

            if (this.AdditionalTest.AnalysisType == AnalysisType.Quantification)
            {
                this.AdditionalTest.InsertIntoProtocol = this.AdditionalTest.QuantificationConfirmed;
            }
            else
            {
                this.AdditionalTest.InsertIntoProtocol = this.Confirmed;
            }
        }

        private bool HasText
        {
            get
            {
                return String.IsNullOrEmpty(this.CustomerText) == false;
            }
        }

        private AdditionalTest AdditionalTest { get; set; }

        private string CustomerText { get; set; } = String.Empty;
        private bool CustomerEvaluation { get; set; } = false;
        private bool Confirmed { get; set; } = false;
        private bool IsUpdating { get; set; } = false;

        private DocumentManager DocumentManager { get; set; }
    }
}

