// Copyright © Selmo Technology GmbH. All Rights Reserved

using Novus.Connectivity.Common;
using SelmoStudio.Hmi.Common.SelmoProject.ProcessValue;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Shared.Model
{
    public static class StatusHelper
    {
        public static OperationMode GetOperationMode(ProcessValuesSequenceStatus infos,
            Dictionary<ProcessValueId, IProcessValue> sequenceStatusValues)
        {
            if (!sequenceStatusValues.ContainsKey(infos.AutomaticReleased.Id) ||
                !sequenceStatusValues.ContainsKey(infos.AutomaticModeSelected.Id) ||
                !sequenceStatusValues.ContainsKey(infos.InterlockFault.Id) ||
                !sequenceStatusValues.ContainsKey(infos.NoCmzFault.Id) ||
                !sequenceStatusValues.ContainsKey(infos.ManualReleased.Id))
                return OperationMode.Error;

            try
            {
                bool automaticRelease = (bool)sequenceStatusValues[infos.AutomaticReleased.Id].Value;
                bool automaticModeSelected = (bool)sequenceStatusValues[infos.AutomaticModeSelected.Id].Value;
                bool interlockFault = (bool)sequenceStatusValues[infos.InterlockFault.Id].Value;
                bool noCmzFault = (bool)sequenceStatusValues[infos.NoCmzFault.Id].Value;
                bool manualReleased = (bool)sequenceStatusValues[infos.ManualReleased.Id].Value;

                if (interlockFault || !noCmzFault)
                    return OperationMode.Error;

                if (automaticRelease)
                    return OperationMode.Automation;

                if (automaticModeSelected)
                    return OperationMode.AutomationSelected;

                if (manualReleased)
                    return OperationMode.Manual;

                return OperationMode.Error;
            }
            catch (Exception)
            {
                return OperationMode.Error;
            }
        }

        public static OperationMode GetOperationMode(ProcessValuesHardwareZone infos,
            Dictionary<ProcessValueId, IProcessValue> statusValues)
        {
            if (!statusValues.ContainsKey(infos.AllSequencesAutomaticRelease.Id) ||
                !statusValues.ContainsKey(infos.AnySequencesReady.Id) ||
                !statusValues.ContainsKey(infos.AnySeqInterlockFault.Id) ||
                !statusValues.ContainsKey(infos.AnySeqCmzFault.Id) ||
                !statusValues.ContainsKey(infos.ManualMode.Id))
                return OperationMode.Error;

            try
            {
                bool automaticRelease = (bool)statusValues[infos.AllSequencesAutomaticRelease.Id].Value;
                bool automaticModeSelected = (bool)statusValues[infos.AnySequencesReady.Id].Value;
                bool interlockFault = (bool)statusValues[infos.AnySeqInterlockFault.Id].Value;
                bool seqCmzFault = (bool)statusValues[infos.AnySeqCmzFault.Id].Value;
                bool manualReleased = (bool)statusValues[infos.ManualMode.Id].Value;

                if (interlockFault || seqCmzFault)
                    return OperationMode.Error;

                if (automaticRelease)
                    return OperationMode.Automation;

                if (automaticModeSelected)
                    return OperationMode.AutomationSelected;

                if (manualReleased)
                    return OperationMode.Manual;

                return OperationMode.Error;
            }
            catch (Exception)
            {
                return OperationMode.Error;
            }
        }
    }
}