// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2017 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 Gtk;
using Glade;

using HollyLibrary;
using Hiperscan.Unix;
using System.IO;
using Hiperscan.FileIO;

namespace Hiperscan.Extensions.ApoIdent
{
	public enum PreferencesUpdateStatus
	{
		InitWindow,

		UserConfigComboChanged,
		ConfigSetAdded,
		ConfigSetRemoved,
		ConfigSetRenamed,

		ReportMangementChanged,

		IdentficationModeChanged,

		LabelPrinterChanged,
		PageSizeChanged,
        PrinterDialogExited,
		ResetPrinterValues,
	}
	
	public partial class Preferences
	{
		#region widget-fields
		
		[Widget] Gtk.Dialog preferences_dialog 							= null;	

		[Widget] Gtk.ComboBox config_combo                 				= null;
		[Widget] Gtk.Button remove_config_button          				= null;
		[Widget] Gtk.Button add_config_button			  				= null;
		[Widget] Gtk.Button rename_config_button			  			= null;

		[Widget] Gtk.Table profile_table								= null;
		[Widget] Gtk.Alignment customer_number_alignment			    = null;
		[Widget] Gtk.Alignment name_info_alignment 						= null;
		[Widget] Gtk.Alignment street_info_alignment					= null;
		[Widget] Gtk.Alignment town_info_alignment 						= null;
		[Widget] HRegExEntry customer_number_hregexentry				= null;
		[Widget] HRegExEntry name_info_hregexentry 						= null;
		[Widget] HRegExEntry street_info_hregexentry					= null;
		[Widget] HRegExEntry town_info_hregexentry 						= null;

		[Widget] Gtk.Entry archive_entry                                = null;	

		[Widget] Gtk.ComboBox default_filename_start_combo 				= null;
		[Widget] Gtk.ComboBox country_combobox							= null;
		[Widget] Gtk.ComboBox report_management_combo					= null;
		[Widget] Gtk.CheckButton show_difference_checkbutton			= null;
		[Widget] Gtk.Button report_management_button					= null;
		[Widget] Gtk.Label report_management_label						= null;

		[Widget] Gtk.Entry server_entry 								= null;
		[Widget] Gtk.Entry port_entry 									= null;
		[Widget] Gtk.ComboBox identification_combo         				= null;
		[Widget] Gtk.Label server_label 								= null;
		[Widget] Gtk.Label port_label 									= null;
		[Widget] Gtk.Table acquisition_table							= null;

		[Widget] Gtk.ComboBox label_printer_combo          				= null;
		[Widget] Gtk.ComboBox page_size_combo 							= null;
		[Widget] Gtk.ComboBox label_orientation_combo      				= null;
		[Widget] Gtk.Button label_printer_settings_button 				= null;
		[Widget] Gtk.Button label_printer_test_button     				= null;

		[Widget] Gtk.Expander label_printer_expander 					= null;
		[Widget] Gtk.SpinButton label_width_spin        				= null;
		[Widget] Gtk.SpinButton label_height_spin       				= null;
		[Widget] Gtk.SpinButton label_x_offset_spin     				= null;
		[Widget] Gtk.SpinButton label_y_offset_spin     				= null;
		[Widget] Gtk.SpinButton label_scale_factor_spin 				= null;
		[Widget] Gtk.Label page_size_label								= null;
		[Widget] Gtk.Label orientation_label							= null;

        [Widget] Gtk.Button close_button = null;

		#endregion


		#region methods

		private void InitializeHollyWidgets()
		{
			this.street_info_hregexentry = new HRegExEntry();
			this.street_info_alignment.Child = this.street_info_hregexentry;
			this.street_info_hregexentry.RegularExpression = @"\w{1}";
			this.street_info_hregexentry.OkMessage = string.Empty;
			this.street_info_hregexentry.ErrorMessage = Catalog.GetString("This input field is mandatory.");
			this.street_info_hregexentry.StockImageOK = ApoIdentStock.TRANSPARENCY;
			this.street_info_hregexentry.ShowOKTooltip = false;
			
			this.customer_number_hregexentry = new HRegExEntry();
			this.customer_number_alignment.Child = this.customer_number_hregexentry;
			this.customer_number_hregexentry.RegularExpression = @"^\d{5,6}$";
			this.customer_number_hregexentry.OkMessage = string.Empty;
			this.customer_number_hregexentry.ErrorMessage = Catalog.GetString("This input field is mandatory.");
			this.customer_number_hregexentry.StockImageOK = ApoIdentStock.TRANSPARENCY;
			this.customer_number_hregexentry.ShowOKTooltip = false;

			this.name_info_hregexentry = new HRegExEntry();
			this.name_info_alignment.Child = this.name_info_hregexentry;
			this.name_info_hregexentry.RegularExpression = @"\w{1}";
			this.name_info_hregexentry.OkMessage = string.Empty;
			this.name_info_hregexentry.ErrorMessage = Catalog.GetString("This input field is mandatory.");
			this.name_info_hregexentry.StockImageOK = ApoIdentStock.TRANSPARENCY;
			this.name_info_hregexentry.ShowOKTooltip = false;
			
			this.town_info_hregexentry = new HRegExEntry();
			this.town_info_alignment.Child = this.town_info_hregexentry;
			this.town_info_hregexentry.RegularExpression = @"\w{1}";
			this.town_info_hregexentry.OkMessage = string.Empty;
			this.town_info_hregexentry.ErrorMessage = Catalog.GetString("This input field is mandatory.");
			this.town_info_hregexentry.StockImageOK = ApoIdentStock.TRANSPARENCY;
			this.town_info_hregexentry.ShowOKTooltip = false;
        }

		private void OnDialogShown(object sender, EventArgs e)
		{
			if (this.Config.Profiles.Current == null)
			{
				this.Config.Profiles.Current = new Profile();
				this.Config.Profiles.Current.Name           = Profile.DEFAULT_NAME;
				this.Config.Profiles.Current.CustomerNumber = Profile.DEFAULT_CUSTOMER_NUMBER;
				this.Config.Profiles.Current.Company        = Profile.DEFAULT_COMPANY;
				this.Config.Profiles.Current.Street         = Profile.DEFAULT_STREET;
				this.Config.Profiles.Current.Town           = Profile.DEFAULT_TOWN;
				this.Config.Profiles.Current.Archive        = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
																	Plugin.name,
				                                                           Catalog.GetString("Archive"),
																	FileIOHelper.ConvertToValidFilename(Profile.DEFAULT_NAME));
				
				this.Config.Profiles.Add(this.Config.Profiles.Current.Name,
				                         this.Config.Profiles.Current);

				this.Update(PreferencesUpdateStatus.ConfigSetAdded);
			}
		}

		private void Update(PreferencesUpdateStatus status)
		{
			this.RemoveChangedEvents();
			switch(status)
			{
	
			case PreferencesUpdateStatus.InitWindow:
				this.FillConfigCombo();
				this.UpdateConfigComboSection();
				this.UpdateProfileSection();
				this.UpdateReportManagementSettingsButton();
				this.UpdateAcquisitionSection();
				this.FillPrinterCombo();
				this.FillPageSizeCombo();
				this.UpdateLabelPrinterSection();
				break;
				
			case PreferencesUpdateStatus.UserConfigComboChanged:
				this.UpdateReportManagementSettingsButton();
				this.UpdateProfileSection();
				this.UpdateConfigComboSection();
				break;
			
			case PreferencesUpdateStatus.ConfigSetAdded:
			case PreferencesUpdateStatus.ConfigSetRemoved:
			case PreferencesUpdateStatus.ConfigSetRenamed:
				this.FillConfigCombo();
				this.UpdateConfigComboSection();
				this.UpdateProfileSection();
				this.UpdateReportManagementSettingsButton();
				break;

			case PreferencesUpdateStatus.ReportMangementChanged:
				this.UpdateReportManagementSettingsButton();
				break;

			case PreferencesUpdateStatus.IdentficationModeChanged:
				this.UpdateAcquisitionSection();
				break;

			case PreferencesUpdateStatus.LabelPrinterChanged:
				this.FillPageSizeCombo();
				this.UpdateLabelPrinterSection();
				break;

			case PreferencesUpdateStatus.PageSizeChanged:			
			case PreferencesUpdateStatus.ResetPrinterValues:
				this.UpdateLabelPrinterSection();
				break;

                case PreferencesUpdateStatus.PrinterDialogExited:
                    this.UpdateLabelPrinterSection();
				break;

			default:
				break;
			}

			this.AddChangeEvents();
		}
	
		#endregion
	}
}
