using MainShell.Common;
using MainShell.Models;
using System;
using AppCameraType = MainShell.Common.CameraType;

namespace MainShell.Vision
{
    /// <summary>
    /// ????????????
    /// </summary>
    public enum ImageCaptureStatus
    {
        Success = 0,
        Cancelled = 1,
        CameraNotFound = 2,
        CameraNotOpen = 3,
        CameraNotGrabbing = 4,
        Timeout = 5,
        NoFrame = 6,
        SoftTriggerFailed = 7,
        DriverError = 8,
    }

    /// <summary>
    /// ??????????
    /// </summary>
    public class ImageCaptureResult
    {
        public ImageCaptureResult()
        {
            Status = ImageCaptureStatus.DriverError;
            ErrorCode = VisionErrorCode.DriverError;
            FailureCategory = VisionFailureCategory.Driver;
            Message = string.Empty;
            UserMessageKey = MessageKey.None;
        }

        public ImageCaptureStatus Status { get; set; }

        public VisionErrorCode ErrorCode { get; set; }

        public VisionFailureCategory FailureCategory { get; set; }

        public int? AlarmId { get; set; }

        public MessageKey UserMessageKey { get; set; }

        public AppCameraType CameraSource { get; set; }

        public CameraCaptureMode CaptureMode { get; set; }

        public string Message { get; set; }

        public Exception Exception { get; set; }

        public MxImage Image { get; set; }

        public bool Succeeded
        {
            get
            {
                return Status == ImageCaptureStatus.Success;
            }
        }

        public bool IsCancelled
        {
            get
            {
                return Status == ImageCaptureStatus.Cancelled;
            }
        }

        public static ImageCaptureResult Success(AppCameraType source, CameraCaptureMode captureMode, MxImage image)
        {
            return new ImageCaptureResult
            {
                Status = ImageCaptureStatus.Success,
                ErrorCode = VisionErrorCode.None,
                FailureCategory = VisionFailureCategory.None,
                AlarmId = null,
                UserMessageKey = MessageKey.None,
                CameraSource = source,
                CaptureMode = captureMode,
                Image = image,
                Message = string.Format(
                    "ImageCaptureService: capture succeeded from '{0}' with mode '{1}'.",
                    source,
                    captureMode),
            };
        }

        public static ImageCaptureResult Failure(
            ImageCaptureStatus status,
            VisionErrorCode errorCode,
            VisionFailureCategory failureCategory,
            int? alarmId,
            MessageKey userMessageKey,
            AppCameraType source,
            CameraCaptureMode captureMode,
            string message,
            Exception exception = null)
        {
            return new ImageCaptureResult
            {
                Status = status,
                ErrorCode = errorCode,
                FailureCategory = failureCategory,
                AlarmId = alarmId,
                UserMessageKey = userMessageKey,
                CameraSource = source,
                CaptureMode = captureMode,
                Message = message,
                Exception = exception,
                Image = null,
            };
        }
    }
}
