// 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 System.Collections.Generic;
using System.Threading;
using Gtk;
using Hiperscan.AnalysisModule;
using Hiperscan.ApoIdentCore.Enums;
using Hiperscan.ApoIdentCore.GuiControl;
using Hiperscan.Extensions.ApoIdent.Widgets;
using Hiperscan.GtkExtensions.GuiSync;
using Hiperscan.GtkExtensions.StandardDialogs;
using Hiperscan.Unix;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication : GuiSynchronizerTemplate<SyncControl, UpdateArgs, DialogArgs>
    {
        protected override void InitGuiSyncControl(Gtk.Window main_window, SyncControl sync_control)
        {
            this.MainWindow = main_window;
            this.SyncControl = sync_control;
        }

        protected override void OtherUpdates(UpdateArgs args)
        {
            if (args.SplashScreenState != SplashScreenState.None)
                this.UpdateSplashScreen(args);

            if (args.Menu != UpdateCategory.None)
                this.UpdateMenueSection(args);

            if (args.Pharmacy != UpdateCategory.None)
                this.UpdatePharmacySection(args);

            if (args.Substance != UpdateCategory.None)
                this.UpdateSubstanceSection(args);

            if (args.Specimen != UpdateCategory.None)
                this.UpdateSpecimenSection(args);

            if (args.Measuring != UpdateCategory.None)
                this.UpdateMeasuringSection(args);

            if (args.Result != UpdateCategory.None)
                this.UpdateResultSection(args);

            if (args.Protocol != UpdateCategory.None)
                this.UpdateProtocolSection(args);

            if (args.StateBarValues != null)
                this.UpdateStateSection(args);

            if (args.AnimationType != ProgramState.None)
                this.UpdateAnimationSection(args);

            if (args.DialogArgs.ApoDialogType != ApoDialogType.None)
                this.ShowDialog(args);
        }

        private void UpdateAnimationSection(UpdateArgs arg)
        {
            switch (arg.AnimationType)
            {
                case ProgramState.Working:
                    this.Animation.Update(Widgets.AnimationState.Working);
                    this.Animation.ResetError();
                    break;

                case ProgramState.ServerConnectionFailed:
                    this.Animation.Update(Widgets.AnimationState.Failure);
                    this.Animation.UpdateError(AnimationErrorState.ConnectionServerFailed, arg.DialogArgs.Message);
                    break;

                case ProgramState.DeviceConnectionFailed:
                    this.Animation.Update(Widgets.AnimationState.Failure);
                    this.Animation.UpdateError(AnimationErrorState.ConnectionDeviceFailed, arg.DialogArgs.Message);
                    break;

                case ProgramState.DeviceRegistratopnFailed:
                    this.Animation.Update(Widgets.AnimationState.Failure);
                    this.Animation.UpdateError(AnimationErrorState.DeviceRegistratopnFailed, arg.DialogArgs.Message);
                    break;

                case ProgramState.ConnectionLost:
                    this.Animation.Update(Widgets.AnimationState.Failure);
                    this.Animation.UpdateError(AnimationErrorState.ConnectionLost, arg.DialogArgs.Message);
                    break;

                case ProgramState.BlackFailed:
                    this.Animation.Update(Widgets.AnimationState.Failure);
                    this.Animation.UpdateError(AnimationErrorState.BlackFailed, arg.DialogArgs.Message);
                    break;

                case ProgramState.WhiteFailed:
                    this.Animation.Update(Widgets.AnimationState.Failure);
                    this.Animation.UpdateError(AnimationErrorState.WhiteFailed, arg.DialogArgs.Message);
                    break;

                case ProgramState.TransflectanceFailed:
                    this.Animation.Update(Widgets.AnimationState.Failure);
                    this.Animation.UpdateError(AnimationErrorState.TransflectanceFailed, arg.DialogArgs.Message);
                    break;

                case ProgramState.MeasuringFailed:
                    this.Animation.Update(Widgets.AnimationState.Failure);
                    this.Animation.UpdateError(AnimationErrorState.MeasuringFailed, arg.DialogArgs.Message);
                    break;

                case ProgramState.MeasuringBlackReference:
                    if (arg.IsMeasuring)
                        this.Animation.Update(AnimationState.MeasuringBlackReference);
                    else
                        this.Animation.Update(AnimationState.ReadyForBlackReference);

                    this.Animation.ResetError();
                    break;

                case ProgramState.MeasuringInternalRecalibration:
                    this.Animation.Update(AnimationState.InternalRecalibration);
                    break;

                case ProgramState.MeasuringWhiteReference:
                    if (arg.IsMeasuring)
                        this.Animation.Update(AnimationState.MeasuringWhiteReference);
                    else
                        this.Animation.Update(AnimationState.ReadyForWhiteReference);
                    break;

                case ProgramState.MeasuringWhiteInsertReference:
                    if (arg.IsMeasuring)
                        this.Animation.Update(AnimationState.MeasuringWhiteInsertReference);
                    else
                        this.Animation.Update(AnimationState.ReadyForWhiteInsertReference);
                    break;

                case ProgramState.MeasuringTransflectance:
                    if (arg.IsMeasuring)
                        this.Animation.Update(AnimationState.MeasuringTransflectance);
                    else
                        this.Animation.Update(AnimationState.ReadyForTransflectance);

                    this.Animation.ResetError();
                    break;

                case ProgramState.MeasuringTransflectanceAu:
                    if (arg.IsMeasuring)
                        this.Animation.Update(AnimationState.MeasuringTransflectanceAu);
                    else
                        this.Animation.Update(AnimationState.ReadyForTransflectanceAu);

                    this.Animation.ResetError();
                    break;

                case ProgramState.MeasuringFluid:
                    if (arg.IsMeasuring)
                        this.Animation.Update(AnimationState.MeasuringFluid);
                    else
                        this.Animation.Update(AnimationState.ReadyForMeasurementFluid);

                    this.Animation.ResetError();
                    break;

                case ProgramState.MeasuringSolid:
                    if (arg.IsMeasuring)
                        this.Animation.Update(AnimationState.MeasuringSolid);
                    else
                        this.Animation.Update(AnimationState.ReadyForMeasurementSolid);

                    this.Animation.ResetError();
                    break;

                case ProgramState.MeasuringCannabis:
                    if (arg.IsMeasuring)
                        this.Animation.Update(AnimationState.MeasuringCannabis);
                    else
                        this.Animation.Update(AnimationState.ReadyForMeasuringCannabis);

                    this.Animation.ResetError();
                    break;

                case ProgramState.MeasuringInsert:
                    if (arg.IsMeasuring)
                        this.Animation.Update(AnimationState.MeasuringSolidInsert);
                    else
                        this.Animation.Update(AnimationState.ReadyForMeasurementSolidInsert);

                    this.Animation.ResetError();
                    break;

                case ProgramState.NoSubstances:
                    this.Animation.Update(Widgets.AnimationState.NoSubstances);
                    break;

                case ProgramState.ReadyForSubstanceInput:
                    this.Animation.Update(Widgets.AnimationState.ReadyForSubstanceInput);
                    this.Animation.ResetError();
                    break;

                case ProgramState.ReadyForProtocolInput:
                    this.Animation.Update(Widgets.AnimationState.ReadyForProtocolInput);
                    break;

                case ProgramState.ReadyForProtocolCreation:
                    this.Animation.Update(Widgets.AnimationState.ReadyForProtocolCreation);
                    break;

                case ProgramState.IdentificationFluidFailed:
                    this.Animation.UpdateError(AnimationErrorState.IdentificationFluidFailed, arg.DialogArgs.Message);
                    break;

                case ProgramState.IdentificationSolidFailed:
                    this.Animation.UpdateError(AnimationErrorState.IdentificationSolidFailed, arg.DialogArgs.Message);
                    break;

                case ProgramState.QuantificationFailed:
                    this.Animation.UpdateError(AnimationErrorState.QuantificationFailed, arg.DialogArgs.Message);
                    break;

                case ProgramState.SavingFailed:
                    this.Animation.Update(AnimationState.SavingFailed);
                    this.Animation.UpdateError(AnimationErrorState.SaveFailed, arg.DialogArgs.Message);
                    break;

                case ProgramState.Saved:
                    this.Animation.Update(AnimationState.Saved);
                    this.Animation.ResetError(true);
                    break;

                case ProgramState.NotMeasurable:
                    this.Animation.Update(AnimationState.NotMeasurable);
                    break;

                default:
                    break;
            }
        }

        private void ShowDialog(UpdateArgs arg)
        {
            switch (arg.DialogArgs.ApoDialogType)
            {

                case ApoDialogType.Preferences:
                    Preferences.Start(this.MainWindow, this.Control);
                    break;

                case ApoDialogType.RemindForUpdate:
                    RemindForUpdateDialog.Run(this.MainWindow, this.Control, arg);
                    break;

                case ApoDialogType.DataExchangeImport:
                    arg.DialogArgs.Response = StandardDialog.Convert(DataExchangeImportDialog.Run(this.MainWindow, this.Control));
                    break;

                case ApoDialogType.DataExchangeSelectSubstance:
                    FuzzyNameDialog.Run(this.MainWindow, this.Control);
                    break;

                case ApoDialogType.SelectModelClass:
                    ModelClassDialog.Run(this.MainWindow, this.Control);
                    break;

                case ApoDialogType.SubstanceClassificationInfo:
                    SubstanceClassificationInfoDialog.Run(this.MainWindow, this.Control);
                    break;

                case ApoDialogType.SubstanceExtension:
                    SubstanceExtensionDialog.Run(this.MainWindow, this.Control, this.GuiDesigner);
                    break;

                case ApoDialogType.AboutDialog:
                    ApoIdentAboutDialog.Run(this.MainWindow, this.Control);
                    break;

                case ApoDialogType.AdditionalTest:
                    AdditionalTestDialog.Run(this.MainWindow, this.Control);
                    break;

                case ApoDialogType.ResultPlot:
                    ResultPlotDialog.Run(this.MainWindow, this.Control);
                    break;

                case ApoDialogType.ResultList:
                    ResultListDialog.Run(this.MainWindow, this.Control);
                    break;

                case ApoDialogType.Query:
                    QueryDialog.Run(this.MainWindow, this.Control);
                    break;

                case ApoDialogType.ImportantCustomerAdvice:
                    ImportantCustomerInfoDialog.Run(this.MainWindow, this.Control);
                    break;

                default:
                    break;
            }
        }

        private bool HasVisibleSubstances
        {
            get
            {
                return this.Control.GlobalStates.ConnectionSuccess ||
                    (this.Control.GlobalStates.HasNotMeasurableData && 
                     (this.Control.ProfileManager?.CurrentProfile?.HasVisibleSubstances ?? false));
            }
        }

        private bool HasSubstances
        {
            get
            {
                return this.Control.GlobalStates.ConnectionSuccess ||
                    this.Control.GlobalStates.HasNotMeasurableData;
            }
        }

        private bool IsQuantifierMode
        {
            get
            {
                if (this.Control.SubstanceManager.SubstancesCatalog.AnalysisType == AnalysisType.Quantification)
                    return true;

                return this.Control.GlobalStates.IsOnlyQuantifierModuleAvailable;
            }
        }
    }
}