using System;

namespace XP.Hardware.Detector.Abstractions
{
    /// <summary>
    /// 探测器操作结果封装 | Detector operation result wrapper
    /// 统一封装成功/失败状态、数据和错误信息
    /// </summary>
    public class DetectorResult
    {
        /// <summary>
        /// 操作是否成功 | Whether the operation succeeded
        /// </summary>
        public bool IsSuccess { get; }

        /// <summary>
        /// 错误消息 | Error message
        /// </summary>
        public string ErrorMessage { get; }

        /// <summary>
        /// 异常对象 | Exception object
        /// </summary>
        public Exception Exception { get; }

        /// <summary>
        /// 错误码 | Error code
        /// </summary>
        public int ErrorCode { get; }

        protected DetectorResult(bool isSuccess, string errorMessage = null, Exception exception = null, int errorCode = 0)
        {
            IsSuccess = isSuccess;
            ErrorMessage = errorMessage;
            Exception = exception;
            ErrorCode = errorCode;
        }

        /// <summary>
        /// 创建成功结果 | Create success result
        /// </summary>
        /// <param name="message">成功消息 | Success message</param>
        /// <returns>成功结果 | Success result</returns>
        public static DetectorResult Success(string message = null)
        {
            return new DetectorResult(true, message);
        }

        /// <summary>
        /// 创建失败结果 | Create failure result
        /// </summary>
        /// <param name="errorMessage">错误消息 | Error message</param>
        /// <param name="exception">异常对象 | Exception object</param>
        /// <param name="errorCode">错误码 | Error code</param>
        /// <returns>失败结果 | Failure result</returns>
        public static DetectorResult Failure(string errorMessage, Exception exception = null, int errorCode = -1)
        {
            return new DetectorResult(false, errorMessage, exception, errorCode);
        }
    }

    /// <summary>
    /// 带数据的探测器操作结果 | Detector operation result with data
    /// </summary>
    /// <typeparam name="T">数据类型 | Data type</typeparam>
    public class DetectorResult<T> : DetectorResult
    {
        /// <summary>
        /// 结果数据 | Result data
        /// </summary>
        public T Data { get; }

        private DetectorResult(bool isSuccess, T data, string errorMessage = null, Exception exception = null, int errorCode = 0)
            : base(isSuccess, errorMessage, exception, errorCode)
        {
            Data = data;
        }

        /// <summary>
        /// 创建成功结果 | Create success result
        /// </summary>
        /// <param name="data">结果数据 | Result data</param>
        /// <param name="message">成功消息 | Success message</param>
        /// <returns>成功结果 | Success result</returns>
        public static DetectorResult<T> Success(T data, string message = null)
        {
            return new DetectorResult<T>(true, data, message);
        }

        /// <summary>
        /// 创建失败结果 | Create failure result
        /// </summary>
        /// <param name="errorMessage">错误消息 | Error message</param>
        /// <param name="exception">异常对象 | Exception object</param>
        /// <param name="errorCode">错误码 | Error code</param>
        /// <returns>失败结果 | Failure result</returns>
        public new static DetectorResult<T> Failure(string errorMessage, Exception exception = null, int errorCode = -1)
        {
            return new DetectorResult<T>(false, default(T), errorMessage, exception, errorCode);
        }
    }
}
