// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2021 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.GtkExtensions.Widgets;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        [Widget] Gtk.Label big_test_label = null;
        [Widget] Gtk.Label test_user_label = null;
        [Widget] Gtk.Entry test_user_entry = null;
        [Widget] Gtk.ComboBox test_profile_combobox = null;

        private void UpdateTestSection(bool update_profile_data)
        {
            bool active = this.Control.QuitRequested == false &&
                              this.Control.MeasuringProgressState.IsProcessing == false &&
                              this.Control.Busy == false;


            this.big_test_label.Sensitive = active;
            this.test_user_label.Sensitive = active;
            this.test_profile_combobox.Sensitive = this.Config.Profiles.AllProfiles.Count > 1 && active;
            this.SetTestUserEntry(active);
            this.SetTestProfileCombo(update_profile_data);
        }

        private void SetTestProfileCombo(bool update_profile_data)
        {
            if (update_profile_data == false)
                return;

            Gtk.ListStore store = (Gtk.ListStore)this.test_profile_combobox.Model;
            store.Clear();

            if (this.Config.Profiles.AllProfiles.Count > 0)
            {
                foreach (Configurations.ProfilePreferences item in this.Config.Profiles.AllProfiles.Values)
                    this.test_profile_combobox.AppendText(item.Name);

                this.test_profile_combobox.Active = this.Config.Profiles.CurrentIndex(this.Config.Profiles.Current);
            }

            this.test_profile_combobox.Sensitive = this.Config.Profiles.AllProfiles.Count > 1;
        }

        private void SetTestUserEntry(bool active)
        {
            switch (this.SubstanceManagement.ProtocolCompletion)
            {
                case ProtocolAutoCompletionType.ThirdParty:
                    this.TestUserEntry.Sensitive = false;
                    string key = this.Config.Profiles.Current.ExchangeData.CurrentDataSetKey;
                    this.TestUserEntry.GtkEntry.Text = this.Config.Profiles.Current.ExchangeData[key].User;
                    break;

                default:
                    this.TestUserEntry.Sensitive = active;
                    break;
            }
        }

        private OutLinedRegexEntry TestUserEntry { get; set; }
    }
}
