// 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 System.IO;

using Glade;

using Gtk;

using Hiperscan.Common.IO;
using Hiperscan.Unix;
using Hiperscan.GtkExtensions.Widgets;
using Hiperscan.ApoIdentCore;
using Hiperscan.ApoIdentCommon.ApoConfig;

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

        UserConfigComboChanged,
        ConfigSetAdded,
        ConfigSetRemoved,
        ConfigSetRenamed,

        ReportMangementChanged,
        ReportCountryChanged,

        IdentModuleSettingsChanged,
        QuantifierModuleSettingsChanged,
        DeviceSettingsChanged,
        SoftwareSettingsChanged,

        LabelPrinterChanged,
        PageSizeChanged,
        PrinterDialogExited,
        ResetPrinterValues,
    }

    public partial class Preferences
    {
        [Widget] Gtk.Dialog preferences_dialog = null;
        [Widget] Gtk.ScrolledWindow preferences_scrolledwindow = 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] RegexEntry customer_number_regexentry = null;
        [Widget] RegexEntry name_info_regexentry = null;
        [Widget] RegexEntry street_info_regexentry = null;
        [Widget] RegexEntry town_info_regexentry = null;

        [Widget] Gtk.Entry archive_entry = null;

        [Widget] Gtk.ComboBox default_filename_start_combo = null;
        [Widget] Gtk.ComboBox country_combobox = null;
        [Widget] Gtk.LinkButton show_bak_linkbutton = null;
        [Widget] Gtk.Label bak_label = null;
        [Widget] Gtk.CheckButton bak_checkbutton = null;
        [Widget] Gtk.ComboBox report_management_combo = null;
        [Widget] Gtk.Label show_difference_label = null;
        [Widget] Gtk.CheckButton show_difference_checkbutton = null;
        [Widget] Gtk.LinkButton show_difference_linkbutton = null;
        [Widget] Gtk.CheckButton mandatory_checkbutton = null;
        [Widget] Gtk.LinkButton show_deactive_mandatory_linkbutton = null;
        [Widget] Gtk.Label mandatory_label = null;
        [Widget] Gtk.CheckButton print_validation_checkbutton = null;
        [Widget] Gtk.Label print_validation_label = null;
        [Widget] Gtk.LinkButton show_print_validation_linkbutton = null;
        [Widget] Gtk.CheckButton certification_control_checkbutton = null;
        [Widget] Gtk.Label certification_control_label = null;
        [Widget] Gtk.LinkButton show_certification_control_linkbutton = null;
        [Widget] Gtk.Button report_management_button = null;
        [Widget] Gtk.Label report_management_label = 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.Expander device_expander = null;
        [Widget] Gtk.Expander identmodule_expander = null;
        [Widget] Gtk.Expander quantifiermodule_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;

        private void InitializeRegexEntries()
        {
            this.street_info_regexentry = new RegexEntry(Stock.Ok, Stock.DialogError);
            this.street_info_alignment.Child = this.street_info_regexentry;
            this.street_info_regexentry.IgnoreTextValidation = true;

            this.customer_number_regexentry = new RegexEntry(Stock.Ok, Stock.DialogError);
            this.customer_number_alignment.Child = this.customer_number_regexentry;
            this.customer_number_regexentry.IgnoreTextValidation = true;

            this.name_info_regexentry = new RegexEntry(Stock.Ok, Stock.DialogError);
            this.name_info_alignment.Child = this.name_info_regexentry;
            this.name_info_regexentry.IgnoreTextValidation = true;

            this.town_info_regexentry = new RegexEntry(Stock.Ok, Stock.DialogError);
            this.town_info_alignment.Child = this.town_info_regexentry;
            this.town_info_regexentry.IgnoreTextValidation = true;
        }

        private void OnDialogShown(object sender, EventArgs e)
        {
            if (this.Control.ProfileManager.CurrentProfile == null)
            {
                this.Control.ProfileManager.CurrentProfile = new ProfileSettings();
                this.Control.ProfileManager.CurrentProfile.Name = ProfileSettings.DEFAULT_NAME;
                this.Control.ProfileManager.CurrentProfile.CustomerNumber = ProfileSettings.DEFAULT_CUSTOMER_NUMBER;
                this.Control.ProfileManager.CurrentProfile.Company = ProfileSettings.DEFAULT_COMPANY;
                this.Control.ProfileManager.CurrentProfile.Street = ProfileSettings.DEFAULT_STREET;
                this.Control.ProfileManager.CurrentProfile.Town = ProfileSettings.DEFAULT_TOWN;
                this.Control.ProfileManager.CurrentProfile.Archive = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                                                                           ApoIdentInfo.NAME,
                                                                           Catalog.GetString("Archive"),
                                                                    FileIOHelper.ConvertToValidFilename(ProfileSettings.DEFAULT_NAME));

                this.Control.ProfileManager.AllProfiles.Add(this.Control.ProfileManager.CurrentProfile.Name,
                                                            this.Control.ProfileManager.CurrentProfile);

                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.UpdateIdentModuleSection();
                    this.UpdateQuantifierModuleSection();
                    this.UpdateDeviceSection();
                    this.FillPrinterCombo();
                    this.FillPageSizeCombo();
                    this.UpdateLabelPrinterSection();
                    this.UpdateSoftwareSection();
                    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.UpdateProfileSection();
                    this.UpdateReportManagementSettingsButton();
                    break;

                case PreferencesUpdateStatus.ReportCountryChanged:
                    this.UpdateProfileSection();
                    break;

                case PreferencesUpdateStatus.IdentModuleSettingsChanged:
                    this.UpdateIdentModuleSection();
                    break;

                case PreferencesUpdateStatus.QuantifierModuleSettingsChanged:
                    this.UpdateQuantifierModuleSection();
                    break;

                case PreferencesUpdateStatus.DeviceSettingsChanged:
                    this.UpdateDeviceSection();
                    break;

                case PreferencesUpdateStatus.SoftwareSettingsChanged:
                    this.UpdateSoftwareSection();
                    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();
        }

        private void OnExpanderSizeAllocated(object sender, Gtk.SizeAllocatedArgs e)
        {
            Gtk.Requisition desired_size = this.preferences_scrolledwindow.Child.SizeRequest();
            if (desired_size.Height < 100)
                return;

            int height = this.preferences_scrolledwindow.SizeRequest().Height;

            if (this.preferences_scrolledwindow.VscrollbarPolicy == PolicyType.Never)
            {
                this.preferences_dialog.HeightRequest = this.preferences_dialog.Allocation.Height;
                if (desired_size.Height > height)
                {
    
                    this.preferences_scrolledwindow.VscrollbarPolicy = PolicyType.Automatic;
                }
            }
        }

        private void SetInitialScrolledWindow()
        {
            Requisition desired_size = this.preferences_dialog.SizeRequest();

            int height = (int)((this.preferences_dialog.Screen.Height) * 0.9);

            if (this.preferences_scrolledwindow.VscrollbarPolicy == PolicyType.Never)
            {
                if (desired_size.Height > height)
                {
                    this.preferences_scrolledwindow.HeightRequest = (int)(height * 0.8);
                    this.preferences_scrolledwindow.VscrollbarPolicy = PolicyType.Automatic;
                }
            }
        }
    }
}
