using System.Runtime.InteropServices;
using System.Text;

namespace WmsMobileServe.Utils;

/// <summary>
/// 输出信息
/// </summary>
public class ConsoleLog
{

    private static readonly object _locker = new();

    /// <summary>
    /// 输出提示信息
    /// </summary>
    /// <param name="messages"></param>
    public static void Info(params string[] messages) => Info(true, messages);

    /// <summary>
    /// 输出提示信息 
    /// </summary>
    /// <param name="isShow"></param>
    /// <param name="messages"></param>
    public static void Info(bool isShow, params string[] messages)
    {
        if (!isShow) return;
        if (messages.Length == 0) return;
        DateTime now = DateTime.Now;
        StringBuilder stringBuilder = new();
        stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
        foreach (string message in messages)
        {
            stringBuilder.AppendLine($" -- {message}");
        }
        lock (_locker)
        {
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.Write(stringBuilder.ToString());
        }
    }


    /// <summary>
    /// 输出错误信息
    /// </summary>
    /// <param name="messages"></param>
    public static void Error(params string[] messages) => Error(true, messages);

    /// <summary>
    /// 输出错误信息
    /// </summary>
    /// <param name="isShow"></param>
    /// <param name="messages"></param>
    public static void Error(bool isShow, params string[] messages)
    {
        if (!isShow) return;
        if (messages.Length == 0) return;
        DateTime now = DateTime.Now;
        StringBuilder stringBuilder = new();
        stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
        foreach (string message in messages)
        {
            stringBuilder.AppendLine($" -- {message}");
        }
        lock (_locker)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(stringBuilder.ToString());
        }
    }


    /// <summary>
    /// 输出异常信息
    /// </summary>
    /// <param name="messages"></param>
    public static void Exception(params string[] messages) => Exception(true, messages);

    /// <summary>
    /// 输出异常信息
    /// </summary>
    /// <param name="isShow"></param>
    /// <param name="messages"></param>
    public static void Exception(bool isShow, params string[] messages)
    {
        if (!isShow) return;
        if (messages.Length == 0) return;
        DateTime now = DateTime.Now;
        StringBuilder stringBuilder = new();
        stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
        foreach (string message in messages)
        {
            stringBuilder.AppendLine($" -- {message}");
        }
        lock (_locker)
        {
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Write(stringBuilder.ToString());
        }
    }


    /// <summary>
    /// 输出成功信息
    /// </summary>
    /// <param name="messages"></param>
    public static void Success(params string[] messages) => Success(true, messages);

    /// <summary>
    /// 输出成功信息
    /// </summary>
    /// <param name="isShow"></param>
    /// <param name="messages"></param>
    public static void Success(bool isShow, params string[] messages)
    {
        if (!isShow) return;
        if (messages.Length == 0) return;
        DateTime now = DateTime.Now;
        StringBuilder stringBuilder = new();
        stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
        foreach (string message in messages)
        {
            stringBuilder.AppendLine($" -- {message}");
        }
        lock (_locker)
        {
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.Write(stringBuilder.ToString());
        }
    }


    /// <summary>
    /// 输出警告信息
    /// </summary>
    /// <param name="messages"></param>
    public static void Warning(params string[] messages) => Warning(true, messages);

    /// <summary>
    /// 输出警告信息
    /// </summary>
    /// <param name="isShow"></param>
    /// <param name="messages"></param>
    public static void Warning(bool isShow, params string[] messages)
    {
        if (!isShow) return;
        if (messages.Length == 0) return;
        DateTime now = DateTime.Now;
        StringBuilder stringBuilder = new();
        stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
        foreach (string message in messages)
        {
            stringBuilder.AppendLine($" -- {message}");
        }
        lock (_locker)
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write(stringBuilder.ToString());
        }
    }


    /// <summary>
    /// 输出蓝色提示信息
    /// </summary>
    /// <param name="messages"></param>
    public static void Tip(params string[] messages) => Tip(true, messages);

    /// <summary>
    /// 输出蓝色提示信息
    /// </summary>
    /// <param name="isShow"></param>
    /// <param name="messages"></param>
    public static void Tip(bool isShow, params string[] messages)
    {
        if (!isShow) return;
        if (messages.Length == 0) return;
        DateTime now = DateTime.Now;
        StringBuilder stringBuilder = new();
        stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
        foreach (string message in messages)
        {
            stringBuilder.AppendLine($" -- {message}");
        }
        lock (_locker)
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write(stringBuilder.ToString());
        }
    }

    /// <summary>
    /// Tcp 通讯专用
    /// </summary>
    /// <param name="messages"></param>
    public static void Tcp(params string[] messages) => Tcp(true, messages);

    /// <summary>
    /// Tcp 通讯专用
    /// </summary>
    /// <param name="isShow"></param>
    /// <param name="messages"></param>
    public static void Tcp(bool isShow, params string[] messages)
    {
        if (!isShow) return;
        if (messages.Length == 0) return;
        DateTime now = DateTime.Now;
        StringBuilder stringBuilder = new();
        stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
        foreach (string message in messages)
        {
            stringBuilder.AppendLine($" -- {message}");
        }
        lock (_locker)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(stringBuilder.ToString());
        }
    }


    const int STD_INPUT_HANDLE = -10;
    const uint ENABLE_QUICK_EDIT_MODE = 0x0040;
    [DllImport("kernel32.dll", SetLastError = true)]
    internal static extern nint GetStdHandle(int hConsoleHandle);
    [DllImport("kernel32.dll", SetLastError = true)]
    internal static extern bool GetConsoleMode(nint hConsoleHandle, out uint mode);
    [DllImport("kernel32.dll", SetLastError = true)]
    internal static extern bool SetConsoleMode(nint hConsoleHandle, uint mode);

    /// <summary>
    /// 禁用快速编辑 ---- 此项会屏蔽控制台输入
    /// </summary>
    public static void DisbleQuickEditMode()
    {
        nint hStdin = GetStdHandle(STD_INPUT_HANDLE);
        GetConsoleMode(hStdin, out uint mode);
        mode &= ~ENABLE_QUICK_EDIT_MODE;
        SetConsoleMode(hStdin, mode);
    }


}