using System.ComponentModel;
using Hotline.Share.Enums.CallCenter;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using SqlSugar;
using XF.Domain.Entities;
using XF.Domain.Repository;

namespace Hotline.Users;

/// <summary>
/// 工作记录(用户绑定分机记录)
/// </summary>
[Description("工作记录")]
public class Work : CreationModificationEntity
{
    /// <summary>
    /// 绑定分机时间
    /// </summary>
    public DateTime StartTime { get; init; }

    /// <summary>
    /// 解绑分机时间
    /// </summary>
    public DateTime? EndTime { get; private set; }

    public string UserId { get; init; }

    public string TelId { get; init; }

    /// <summary>
    /// 分机号（冗余）
    /// </summary>
    public string TelNo { get; init; }

    /// <summary>
    /// 姓名（冗余）
    /// </summary>
    public string UserName { get; init; }

    /// <summary>
    /// 工作时长（单位：秒）
    /// </summary>
    [SugarColumn(ColumnDescription = "工作时长（单位：秒）")]
    public double? WorkingDuration { get; private set; }

    public string? TelPwd { get; set; }

    public string? Description { get; set; }

    public string? QueueId { get; set; }

    public string? StaffNo { get; set; }

    public ETelModel? TelModel { get; set; }

    ///// <summary>
    ///// SignalR.ConnectionId
    ///// </summary>
    //[SugarColumn(IsNullable = true)]
    //public string? SignalRId { get; set; }

    public Work()
    {

    }

    public Work(string userId, string name, string telId, string telNo, string? telPwd, string? description, string? queueId, string? staffNo, ETelModel? telModel)
    {
        StartTime = DateTime.Now;
        UserId = userId;
        UserName = name;
        TelId = telId;
        TelNo = telNo;
        TelPwd = telPwd;
        Description = description;
        QueueId = queueId;
        StaffNo = staffNo;
        TelModel = telModel;
    }

    public Work(string userId, string name, string telId, string telNo)
    {
        StartTime = DateTime.Now;
        UserId = userId;
        UserName = name;
        TelId = telId;
        TelNo = telNo;
    }

    /// <summary>
    /// 下班
    /// </summary>
    public void OffDuty()
    {
        EndTime = DateTime.Now;
        WorkingDuration = (EndTime.Value - StartTime).TotalSeconds;
    }

    public string GetKey(KeyMode keyMode) => keyMode switch
    {
        KeyMode.UserId => $"{GetKeyPrefix(KeyMode.UserId)}{UserId}",
        KeyMode.TelNo => $"{GetKeyPrefix(KeyMode.TelNo)}{TelNo}",
        _ => throw new ArgumentOutOfRangeException(nameof(keyMode), keyMode, null)
    };

    /// <summary>
    /// userId模式key=userId，telNo模式key=telNo
    /// </summary>
    /// <param name="keyMode"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentOutOfRangeException"></exception>
    public static string GetKey(KeyMode keyMode, string? key) =>
    keyMode switch
    {
        KeyMode.UserId => $"{GetKeyPrefix(KeyMode.UserId)}{key}",
        KeyMode.TelNo => $"{GetKeyPrefix(KeyMode.TelNo)}{key}",
        _ => throw new ArgumentOutOfRangeException(nameof(keyMode), keyMode, null)
    };

    public static string GetKeyPrefix(KeyMode keyMode) =>
    keyMode switch
    {
        KeyMode.UserId => $"WorkUserId-",
        KeyMode.TelNo => $"WorkTelNo-",
        _ => throw new ArgumentOutOfRangeException(nameof(keyMode), keyMode, null)
    };
}

/// <summary>
/// key类型，依据UserId或TelNo
/// </summary>
public enum KeyMode
{
    UserId = 0,
    TelNo = 1,
}