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


namespace Hiperscan.Extensions.ApoIdent
{
    public partial class FinderWindow
    {
        private void UpdateAnimation(FinderUpdateState state, string message)
        {
            switch (state)
            {
                case FinderUpdateState.StartIdentServerAndDevice:
                    this.FinderAnimation.ResetAllErrors();
                    this.FinderAnimation.SetAnimation(AnimationType.Working);
                    break;

                case FinderUpdateState.IdentServerNotInitialized:
                    this.FinderAnimation.SetAnimation(AnimationType.Error);
                    this.FinderAnimation.SetError(AnimationErrorType.IdentServerNotInitialized, message);
                    break;

                case FinderUpdateState.StartDeviceFailed:
                    this.FinderAnimation.SetAnimation(AnimationType.Error);
                    this.FinderAnimation.SetError(AnimationErrorType.DeviceNotInitialized, message);
                    break;

                case FinderUpdateState.LoadingSubstanceClassesFailed:
                    this.FinderAnimation.SetAnimation(AnimationType.Error);
                    this.FinderAnimation.SetError(AnimationErrorType.IdentServerNoModelsAvailable, message);
                    break;

                case FinderUpdateState.DeviceWarmupFailed:
                    this.FinderAnimation.SetAnimation(AnimationType.Error);
                    this.FinderAnimation.SetError(AnimationErrorType.DeviceWarmupFailed, message);
                    break;

                case FinderUpdateState.StartIdentServerAndDeviceSuccess:
                    this.FinderAnimation.SetAnimation(AnimationType.ReadyForInput, true);
                    break;

                case FinderUpdateState.ConnectionLost:
                    this.FinderAnimation.SetAnimation(AnimationType.Error);
                    this.FinderAnimation.SetError(AnimationErrorType.ConnectionLost, message);
                    break;

                case FinderUpdateState.EndConfigProfileChanged:
                    if (this.InitState == ServerDeviceState.Success)
                    {
                        this.FinderAnimation.ResetAllErrors();
                        this.FinderAnimation.SetAnimation(AnimationType.ReadyForInput);
                    }
                    break;

                case FinderUpdateState.ResetAfterSubstanceChanged:
                    this.FinderAnimation.ResetAllErrors();
                    if (this.SubstanceManagement.CurrentClassification.IsNirMeasurable == false)
                    {
                        this.FinderAnimation.SetAnimation(AnimationType.NotMeasurable);
                    }                       
                    else if (this.SubstanceManagement.CurrentModelClass.IsFluid && this.Finder.Current.RecalibrationIsPending)
                    {
                        this.FinderAnimation.SetAnimation(AnimationType.ReadyForEmptyDie);
                    }
                    else
                    {
                        if (this.SubstanceManagement.CurrentModelClass.IsFluid)
                        {
                            if (this.SubstanceManagement.CurrentClassification.ClassificationType == ClassificationType.Multiple)
                                this.FinderAnimation.SetAnimation(AnimationType.ReadyForMeasurementFluidAttention);
                            else
                                this.FinderAnimation.SetAnimation(AnimationType.ReadyForMeasurementFluid);
                        }
                        else
                        {
                            if (this.SubstanceManagement.CurrentClassification.ClassificationType == ClassificationType.Multiple)
                                this.FinderAnimation.SetAnimation(AnimationType.ReadyForMeasurementAttention);
                            else
                                this.FinderAnimation.SetAnimation(AnimationType.ReadyForMeasurement);
                        }
                    }  
                    break;

                case FinderUpdateState.SubstanceNotMeasurableChanged:
                    this.FinderAnimation.SetAnimation(AnimationType.ReadyForProtocolInput);
                    break;



                case FinderUpdateState.AfterEditingUserDefinedSubstances:
                    this.FinderAnimation.ResetAllErrors();
                    this.FinderAnimation.SetAnimation(AnimationType.ReadyForInput);
                    break;

              

               case FinderUpdateState.EmptyDieMeasurementBegin:
                    this.FinderAnimation.ResetAllErrors();
                    this.FinderAnimation.SetAnimation(AnimationType.MeasuringEmptyDie);
                    break;

                case FinderUpdateState.EmptyDieMeasurementEnd:
                    this.FinderAnimation.SetAnimation(AnimationType.ReadyForMeasurementFluid);
                    break;

                case FinderUpdateState.EmptyDieMeasurementFailedEnd:
                    this.FinderAnimation.SetAnimation(AnimationType.MeasuringEmptyDie);
                    break;

                case FinderUpdateState.EmptyDieClassificationMeasurementEnd:
                    this.FinderAnimation.SetAnimation(AnimationType.ReadyForMeasurementFluidAttention);
                    break;

                case FinderUpdateState.MeasurementBegin:
                    this.FinderAnimation.ResetAllErrors();
                    this.FinderAnimation.SetAnimation(AnimationType.Measuring);
                    break;

                case FinderUpdateState.ExternalCalibration:
                    this.FinderAnimation.SetAnimation(AnimationType.ExternalRecalibration);
                    break;

                case FinderUpdateState.InternalCalibration:
                    this.FinderAnimation.SetAnimation(AnimationType.InternalRecalibration);
                    break;

                case FinderUpdateState.MeasurementEnd:
                    this.FinderAnimation.SetAnimation(AnimationType.ReadyForProtocolInput, true);
                    if (this.SubstanceManagement.CurrentClassification.IsNirMeasurable == true)
                    {
                        if (this.IdentServerClient.IsTruePositive == false)
                        {
                            string error = Catalog.GetString("The identification failed. One reason can be a faulty operation.");
                            error += ServiceForRepeatedError;
                            if (this.SubstanceManagement.CurrentModelClass.IsFluid == true)
                            {
                                this.FinderAnimation.IsMisIdentficationFLuid = true;
                                this.FinderAnimation.SetError(AnimationErrorType.MisIdentificationFluid, error);
                            }
                            else
                            {
                                this.FinderAnimation.IsMisIdentficationSolid = true;
                                this.FinderAnimation.SetError(AnimationErrorType.MisIdentificationSolid, error);
                            }
                        }
                    }
                    break;

                case FinderUpdateState.MeasurementFailedEnd:
                    AnimationType type = AnimationType.None;
                    if (this.SubstanceManagement.CurrentModelClass.IsFluid == true)
                    {
                        if (this.SubstanceManagement.CurrentClassification.ClassificationType != ClassificationType.Multiple)
                            type = AnimationType.ReadyForMeasurementFluid;
                        else
                            type = AnimationType.ReadyForMeasurementFluidAttention;
                    }
                    else
                    {
                        if (this.SubstanceManagement.CurrentClassification.ClassificationType != ClassificationType.Multiple)
                            type = AnimationType.ReadyForMeasurement;
                        else
                            type = AnimationType.ReadyForMeasurementAttention;
                    }
                    this.FinderAnimation.SetAnimation(type);
                    break;

                case FinderUpdateState.ReportSaved:
                    this.FinderAnimation.SetAnimation(AnimationType.Saved);
                    break;

                case FinderUpdateState.ReportSavedFailed:
                    this.FinderAnimation.SetAnimation(AnimationType.SavingFailed);
                    this.FinderAnimation.SetError(AnimationErrorType.SavingFailed, message);
                    break;

                case FinderUpdateState.AfterReportSaving:
                    this.FinderAnimation.SetAnimation(AnimationType.ReadyForInput, true);
                    break;

                case FinderUpdateState.DeviceAfterInternalCalibration:
                    this.FinderAnimation.SetAnimation(AnimationType.None);
                    break;

                case FinderUpdateState.ReportChanged:
                case FinderUpdateState.ExpiryDateChanged:
                    if (this.DetailSectionState.IsSet(DetailSectionStates.SaveReport) == true)
                        this.FinderAnimation.SetAnimation(AnimationType.ReadyForProtocolCreation);
                    else
                        this.FinderAnimation?.SetAnimation(AnimationType.ReadyForProtocolInput);
                    break;
            }

            this.design_left_viewport.QueueDraw();
            this.host.ProcessEvents();
        }
    }
}
