// 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.Spectroscopy.IdentModule;


namespace Hiperscan.Extensions.ApoIdent
{
	public class AdditionalTestWindow
	{
		[Widget] Gtk.Window additional_test_window		= null;
		[Widget] Gtk.TextView additional_textview		= null;
		[Widget] Gtk.CheckButton identified_checkbutton	= null;
		[Widget] Gtk.Label final_result_label			= null;
				
		public AdditionalTestWindow (FinderWindow finder_window)
		{	         			         
			this.FinderWindow = finder_window;
			this.Profile = finder_window.Config.Profiles.Current;
            string key = this.FinderWindow.SubstanceManagement.CurrentSubstance.Id;
                             
			this.Profile.AdditionalCompletionEntries.LoadCustomerText(key);

            if (this.FinderWindow.InitState == ServerDeviceState.Success &&
            this.FinderWindow.IdentServerClient.HasValidResult == true &&
            this.FinderWindow.SubstanceManagement.CurrentClassification.IsNirMeasurable == true &&
            this.FinderWindow.IdentServerClient.IsTruePositive == true &&
            this.FinderWindow.SubstanceManagement.CurrentClassification.ClassificationType != ClassificationType.Multiple)
            {
                this.Profile.AdditionalCompletionEntries.SubstanceUniqueIdentifed = this.FinderWindow.IdentServerClient.IsTruePositive;
                this.Profile.AdditionalCompletionEntries.CustomerIdentificationResult = true;
            }	

			Glade.XML gui = new Glade.XML(null, "ApoIdent.Resources.Glade.FinderWindow.glade", "additional_test_window", "Hiperscan");
			gui.Autoconnect(this);

			this.additional_test_window.SetWmclass(Plugin.name.ToLower(), 
			                                       Plugin.name.ToLower());


            
            this.additional_test_window.DefaultWidth = 300;
            this.additional_test_window.DefaultHeight =  600;
            this.additional_test_window.TransientFor = this.FinderWindow.GtkWindow;
            this.additional_test_window.Title = String.Format(Catalog.GetString("{0} (Method and Result)"),
                                                              this.FinderWindow.GetSubstanceTestType(false));

			this.identified_checkbutton.Active = this.Profile.AdditionalCompletionEntries.CustomerIdentificationResult;
            this.identified_checkbutton.Sensitive = this.Profile.AdditionalCompletionEntries.SubstanceUniqueIdentifed == false;
            this.final_result_label.Sensitive = this.Profile.AdditionalCompletionEntries.SubstanceUniqueIdentifed == false;
			this.additional_textview.Buffer.Text = this.Profile.AdditionalCompletionEntries.CustomerText;
			this.OnTextChanged(null, null);

			this.additional_textview.Buffer.Changed += this.OnTextChanged;         
			this.additional_test_window.DeleteEvent += (sender, e) =>
			{
                this.OnAdditionalTestCloseButtonClicked(null, null);
			};
  
			this.additional_test_window.ShowAll();
			this.additional_textview.GrabFocus();
		}

		private void OnTextChanged(object sender, EventArgs e)
		{
			bool has_text = String.IsNullOrEmpty(this.additional_textview.Buffer.Text.Trim()) == false;

			this.identified_checkbutton.Sensitive = has_text && this.Profile.AdditionalCompletionEntries.SubstanceUniqueIdentifed == false;
			if (has_text == false  && this.Profile.AdditionalCompletionEntries.SubstanceUniqueIdentifed == false)
			{
				this.Profile.AdditionalCompletionEntries.CustomerIdentificationResult = false;
				this.identified_checkbutton.Active = false;
			}
		}
		
		private void OnAdditionalTestCloseButtonClicked(object sender, EventArgs e)
		{
			if (this.Profile.AdditionalCompletionEntries.ProtocolText != this.additional_textview.Buffer.Text.Trim() ||
                    this.Profile.AdditionalCompletionEntries.CustomerIdentificationResult != this.identified_checkbutton.Active)
            {
                this.Profile.AdditionalCompletionEntries.CustomerIdentificationResult = this.identified_checkbutton.Active;
                this.Profile.AdditionalCompletionEntries.SetCustomerText(this.additional_textview.Buffer.Text);
                this.Profile.AdditionalCompletionEntries.InsertIntoProtocol = true;
                this.Profile.AdditionalCompletionEntries.Save();

                this.FinderWindow.DetailSectionState.Set(DetailSectionStates.ReportNotSaved, true);
                this.FinderWindow.Update(FinderUpdateState.AdditionalTestChanged);
            }
         
            this.additional_test_window.Destroy();
		}
		
		private void OnAdditionalTestCancelButtonClicked(object sender, EventArgs e)
		{
			if (this.Profile.AdditionalCompletionEntries.InsertIntoProtocol == true)
			{
				if (String.IsNullOrEmpty(this.Profile.AdditionalCompletionEntries.CustomerText) == false ||
				    (this.Profile.AdditionalCompletionEntries.CustomerIdentificationResult == true &&
				     this.Profile.AdditionalCompletionEntries.SubstanceUniqueIdentifed == false))				   
				{					
					this.FinderWindow.DetailSectionState.Set(DetailSectionStates.ReportNotSaved, true);

				}   

				this.Profile.AdditionalCompletionEntries.Reset();
				this.FinderWindow.Update(FinderUpdateState.AdditionalTestChanged);
			}        
            this.additional_test_window.Destroy();
		}
      
		private  AdditionalTestEntries CompletionEntries { get; set; }     
		private Profile Profile { get; set; }      
		private FinderWindow FinderWindow { get; set; }
	}
}

