using System;
using NPP.SmartSchedue.Api.Contracts.Core.Enums;

namespace NPP.SmartSchedue.Api.Contracts.Core.Extensions;

/// <summary>
/// 不可用原因类型扩展方法
/// 提供原因类型的各种辅助功能，如显示名称、颜色样式、符号等
/// </summary>
public static class UnavailabilityReasonTypeExtensions
{
    /// <summary>
    /// 将int值转换为UnavailabilityReasonType枚举
    /// </summary>
    /// <param name="reasonType">原因类型的int值</param>
    /// <returns>对应的枚举值，如果无法转换则返回Other</returns>
    public static UnavailabilityReasonType ToEnum(this int reasonType)
    {
        return Enum.IsDefined(typeof(UnavailabilityReasonType), reasonType) 
            ? (UnavailabilityReasonType)reasonType 
            : UnavailabilityReasonType.Other;
    }
    
    /// <summary>
    /// 获取原因类型所属的分组
    /// </summary>
    /// <param name="reasonType">原因类型</param>
    /// <returns>所属分组</returns>
    public static UnavailabilityCategory GetCategory(this UnavailabilityReasonType reasonType)
    {
        return reasonType switch
        {
            UnavailabilityReasonType.PersonalPreference => UnavailabilityCategory.Personal,
            UnavailabilityReasonType.Family => UnavailabilityCategory.Personal,
            UnavailabilityReasonType.Other => UnavailabilityCategory.Personal,
            
            UnavailabilityReasonType.Training => UnavailabilityCategory.WorkTask,
            UnavailabilityReasonType.Meeting => UnavailabilityCategory.WorkTask,
            UnavailabilityReasonType.Equipment => UnavailabilityCategory.WorkTask,
            UnavailabilityReasonType.Certification => UnavailabilityCategory.WorkTask,
            
            UnavailabilityReasonType.TemporaryLeave => UnavailabilityCategory.Leave,
            UnavailabilityReasonType.PlannedLeave => UnavailabilityCategory.Leave,
            UnavailabilityReasonType.Medical => UnavailabilityCategory.Leave,
            
            UnavailabilityReasonType.Rotation => UnavailabilityCategory.SystemArranged,
            
            _ => UnavailabilityCategory.Personal
        };
    }
    
    /// <summary>
    /// 获取原因类型的显示名称（中文）
    /// </summary>
    /// <param name="reasonType">原因类型</param>
    /// <returns>中文显示名称</returns>
    public static string GetDisplayName(this UnavailabilityReasonType reasonType)
    {
        return reasonType switch
        {
            UnavailabilityReasonType.PersonalPreference => "个人意愿",
            UnavailabilityReasonType.Training => "培训任务",
            UnavailabilityReasonType.Meeting => "会议任务", 
            UnavailabilityReasonType.Equipment => "设备维护",
            UnavailabilityReasonType.TemporaryLeave => "临时请假",
            UnavailabilityReasonType.PlannedLeave => "计划请假",
            UnavailabilityReasonType.Medical => "医疗原因",
            UnavailabilityReasonType.Family => "家庭事务",
            UnavailabilityReasonType.Rotation => "轮岗安排",
            UnavailabilityReasonType.Certification => "技能认证",
            UnavailabilityReasonType.Other => "其他原因",
            _ => "未知"
        };
    }
    
    /// <summary>
    /// 获取原因类型的描述信息
    /// </summary>
    /// <param name="reasonType">原因类型</param>
    /// <returns>详细描述</returns>
    public static string GetDescription(this UnavailabilityReasonType reasonType)
    {
        return reasonType switch
        {
            UnavailabilityReasonType.PersonalPreference => "员工个人偏好不希望工作该班次",
            UnavailabilityReasonType.Training => "参加培训、学习、考试等",
            UnavailabilityReasonType.Meeting => "参加会议、汇报等",
            UnavailabilityReasonType.Equipment => "参与设备检修、维护工作",
            UnavailabilityReasonType.TemporaryLeave => "临时事假、病假等",
            UnavailabilityReasonType.PlannedLeave => "年假、调休等计划性假期",
            UnavailabilityReasonType.Medical => "体检、就医、康复等",
            UnavailabilityReasonType.Family => "家庭重要事务处理",
            UnavailabilityReasonType.Rotation => "临时调配到其他岗位",
            UnavailabilityReasonType.Certification => "参加技能考核、认证等",
            UnavailabilityReasonType.Other => "其他特殊情况",
            _ => ""
        };
    }
    
    /// <summary>
    /// 获取原因类型在网格视图中的显示符号
    /// </summary>
    /// <param name="reasonType">原因类型</param>
    /// <returns>显示符号</returns>
    public static string GetGridSymbol(this UnavailabilityReasonType reasonType)
    {
        return reasonType switch
        {
            UnavailabilityReasonType.PersonalPreference => "P",
            UnavailabilityReasonType.Training => "T",
            UnavailabilityReasonType.Meeting => "M",
            UnavailabilityReasonType.Equipment => "E",
            UnavailabilityReasonType.TemporaryLeave => "TL",
            UnavailabilityReasonType.PlannedLeave => "PL",
            UnavailabilityReasonType.Medical => "MD",
            UnavailabilityReasonType.Family => "F",
            UnavailabilityReasonType.Rotation => "R",
            UnavailabilityReasonType.Certification => "C",
            UnavailabilityReasonType.Other => "O",
            _ => "?"
        };
    }
    
    /// <summary>
    /// 获取原因类型的CSS样式类名
    /// </summary>
    /// <param name="reasonType">原因类型</param>
    /// <returns>CSS类名</returns>
    public static string GetColorClass(this UnavailabilityReasonType reasonType)
    {
        return reasonType.GetCategory() switch
        {
            UnavailabilityCategory.Personal => "reason-personal",      // 灰色
            UnavailabilityCategory.WorkTask => "reason-work",          // 橙色
            UnavailabilityCategory.Leave => "reason-leave",            // 红色
            UnavailabilityCategory.SystemArranged => "reason-system",  // 蓝色
            _ => "reason-default"
        };
    }
}

/// <summary>
/// int类型的原因类型扩展方法
/// 为直接使用int值提供便捷的扩展方法
/// </summary>
public static class IntReasonTypeExtensions
{
    /// <summary>
    /// 获取int类型原因的显示名称
    /// </summary>
    /// <param name="reasonType">原因类型int值</param>
    /// <returns>显示名称</returns>
    public static string GetDisplayName(this int reasonType)
    {
        return reasonType.ToEnum().GetDisplayName();
    }
    
    /// <summary>
    /// 获取int类型原因的网格符号
    /// </summary>
    /// <param name="reasonType">原因类型int值</param>
    /// <returns>网格符号</returns>
    public static string GetGridSymbol(this int reasonType)
    {
        return reasonType.ToEnum().GetGridSymbol();
    }
    
    /// <summary>
    /// 获取int类型原因的CSS样式类
    /// </summary>
    /// <param name="reasonType">原因类型int值</param>
    /// <returns>CSS类名</returns>
    public static string GetColorClass(this int reasonType)
    {
        return reasonType.ToEnum().GetColorClass();
    }
    
    /// <summary>
    /// 获取int类型原因的分组
    /// </summary>
    /// <param name="reasonType">原因类型int值</param>
    /// <returns>所属分组</returns>
    public static UnavailabilityCategory GetCategory(this int reasonType)
    {
        return reasonType.ToEnum().GetCategory();
    }
}