using MainShell.Process;
using System;

namespace MainShell.ProcessResult
{
    public class AutoProductionResumeValidator
    {
        public bool IsStateConsistentForResume(
            string resumeStepId,
            PreparationAreaProcessState preparationState,
            CurrentChipProcessState currentChipState)
        {
            if (string.IsNullOrWhiteSpace(resumeStepId))
            {
                return true;
            }

            if (preparationState != null && preparationState.Status == ChipPreparationStatus.Faulted)
            {
                return false;
            }

            CurrentChipLifecycleState chipState = currentChipState == null
                ? CurrentChipLifecycleState.Empty
                : currentChipState.State;

            if (string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.SubstrateLoadEntry, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.PreparationSignalEntry, StringComparison.OrdinalIgnoreCase))
            {
                return preparationState == null || preparationState.Status != ChipPreparationStatus.Faulted;
            }

            if (string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.ChipPreparationSyncEntry, StringComparison.OrdinalIgnoreCase))
            {
                return chipState == CurrentChipLifecycleState.Loading ||
                       chipState == CurrentChipLifecycleState.LoadedPendingTransfer ||
                       chipState == CurrentChipLifecycleState.InUse ||
                       (preparationState != null && preparationState.Status == ChipPreparationStatus.Loaded);
            }

            if (string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.PreTransferValidationEntry, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.ChipStraighteningEntry, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.DiePositionEntry, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.DieTransferEntry, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.DieRecheckEntry, StringComparison.OrdinalIgnoreCase))
            {
                return chipState == CurrentChipLifecycleState.LoadedPendingTransfer ||
                       chipState == CurrentChipLifecycleState.InUse ||
                       (preparationState != null && preparationState.Status == ChipPreparationStatus.Loaded);
            }

            if (string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.ChipUnloadEntry, StringComparison.OrdinalIgnoreCase))
            {
                return chipState == CurrentChipLifecycleState.PendingUnload ||
                       chipState == CurrentChipLifecycleState.Unloading ||
                       chipState == CurrentChipLifecycleState.InUse;
            }

            if (string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.SubstrateUnloadEntry, StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }

            return true;
        }

        public string BuildResumeValidationError(
            string resumeStepId,
            PreparationAreaProcessState preparationState,
            CurrentChipProcessState currentChipState)
        {
            if (IsStateConsistentForResume(resumeStepId, preparationState, currentChipState))
            {
                return string.Empty;
            }

            if (preparationState != null && preparationState.Status == ChipPreparationStatus.Faulted)
            {
                return "自动流程恢复失败：准备区当前处于故障状态。";
            }

            if (string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.DieTransferEntry, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.DieRecheckEntry, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.DiePositionEntry, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.ChipStraighteningEntry, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.PreTransferValidationEntry, StringComparison.OrdinalIgnoreCase))
            {
                return "自动流程恢复失败：当前芯片状态与贴装前工艺状态不一致。";
            }

            if (string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.ChipPreparationSyncEntry, StringComparison.OrdinalIgnoreCase))
            {
                return "自动流程恢复失败：当前芯片上料状态与同步点状态不一致。";
            }

            if (string.Equals(resumeStepId, WorkflowStepIds.AutoProduction.ChipUnloadEntry, StringComparison.OrdinalIgnoreCase))
            {
                return "自动流程恢复失败：当前芯片状态不满足芯片下料恢复条件。";
            }

            return "自动流程恢复失败：当前资源状态与恢复入口不一致。";
        }
    }
}