"""InterestPrint 库
作者:袁窦涵
邮箱:w111251@outlook.com
各个函数用法如下:
ColorfulPrint(*text, color='white', bold=False, end='\n', sep=' '): 带颜色打印
MeowPrint(*text, meow_count=1, end='\n', sep=' ', front=True, back=True): 喵喵喵打印"""

import sys
import ctypes
from typing import Any
def ColorfulPrint(*text:Any, color:str='white', bold:bool=False, end:str='\n', sep:str=' ')->None:
    """
    带颜色打印
    :param text: 要打印的内容（可变参数）
    :param color: 字体颜色,可选:black/red/green/yellow/blue/purple/cyan/white
    :param bold: 是否加粗,默认False
    :param end: 结尾字符，默认换行
    :param sep: 多个参数的分隔符，默认空格
    """
    colors = {
        'black': 30, 'red': 31, 'green': 32, 'yellow': 33,
        'blue': 34, 'purple': 35, 'cyan': 36, 'white': 37
    }
    if color not in colors:
        raise ValueError(f"颜色必须是以下之一：{list(colors.keys())}")
    if sys.platform == 'win32':
        try:
            kernel32 = ctypes.windll.kernel32
            kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
        except:
            print(*text, end=end, sep=sep)
            return
    style = 1 if bold else 0
    color_code = colors[color]
    ansi_prefix = f'\033[{style};{color_code}m'
    ansi_suffix = '\033[0m'
    print(f"{ansi_prefix}{sep.join(map(str, text))}{ansi_suffix}", end=end)

def MeowPrint(*text:Any, meow_count:int=1, end:str='\n', sep:str=' ', front:bool=True, back:bool=True)->None:
    """
    喵喵喵打印
    :param text: 要打印的内容（可变参数）
    :param meow_count: 猫咪表情数量,默认1
    :param end: 结尾字符,默认换行
    :param sep: 多个参数的分隔符,默认空格
    :param front: 是否在前面打印表情,默认True
    :param back: 是否打印后面打印表情,默认True
    """
    front_meow = '🐱' * meow_count if front else ''
    back_meow = '🐱' * meow_count if back else ''
    text_str = sep.join(map(str, text)) if text else ''
    print(f"{front_meow}{text_str}{back_meow}", end=end, sep=sep)

def BgColorfulPrint(*text: Any, bg_color: str = 'black',end: str = '\n', sep: str = ' ',bold=False) -> None:
    """
    带字体色+背景色的花式打印（默认白色字体）
    :param text: 要打印的内容（可变参数）
    :param bg_color: 背景颜色,可选:black/red/green/yellow/blue/purple/cyan/white
    :param bold: 是否加粗,默认False
    :param end: 结尾字符，默认换行
    :param sep: 多个参数的分隔符，默认空格
    """
    text_colors = {
        'black': 30, 'red': 31, 'green': 32, 'yellow': 33,
        'blue': 34, 'purple': 35, 'cyan': 36, 'white': 37
    }
    bg_colors = {
        'black': 40, 'red': 41, 'green': 42, 'yellow': 43,
        'blue': 44, 'purple': 45, 'cyan': 46, 'white': 47
    }
    if bg_color not in bg_colors:
        raise ValueError(f"背景颜色必须是以下之一：{list(bg_colors.keys())}")
    if sys.platform == 'win32':
        try:
            kernel32 = ctypes.windll.kernel32
            kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
        except:
            print(*text, end=end, sep=sep)
            return
    style = 1 if bold else 0
    text_code = text_colors['white']
    bg_code = bg_colors[bg_color]
    ansi_prefix = f'\033[{style};{text_code};{bg_code}m'
    ansi_suffix = '\033[0m'
    text_str = sep.join(map(str, text))
    print(f"{ansi_prefix}{text_str}{ansi_suffix}", end=end)

def FgAndBgColorfulPrint(*text: Any, fg_color: str = 'white', bg_color: str = 'black',
                        end: str = '\n', sep: str = ' ') -> None:
    """
    同时设置前景色（字体色）和背景色的花式打印
    :param text: 要打印的内容（可变参数，支持多个值传入）
    :param fg_color: 前景色（字体色），可选:black/red/green/yellow/blue/purple/cyan/white
    :param bg_color: 背景色，可选:black/red/green/yellow/blue/purple/cyan/white
    :param end: 结尾字符，默认换行符（\n），对齐Python内置print参数
    :param sep: 多个text参数之间的分隔符，默认空格，对齐Python内置print参数
    """
    fg_color_map = {
        'black': 30, 'red': 31, 'green': 32, 'yellow': 33,
        'blue': 34, 'purple': 35, 'cyan': 36, 'white': 37
    }
    bg_color_map = {
        'black': 40, 'red': 41, 'green': 42, 'yellow': 43,
        'blue': 44, 'purple': 45, 'cyan': 46, 'white': 47
    }
    if fg_color not in fg_color_map:
        raise ValueError(f"前景色必须是以下有效值之一：{list(fg_color_map.keys())}")
    if bg_color not in bg_color_map:
        raise ValueError(f"背景色必须是以下有效值之一：{list(bg_color_map.keys())}")
    if sys.platform == 'win32':
        try:
            kernel32 = ctypes.windll.kernel32
            kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
        except Exception:
            print(*text, end=end, sep=sep)
            return
    fg_code = fg_color_map[fg_color]
    bg_code = bg_color_map[bg_color]
    ansi_prefix = f'\033[{fg_code};{bg_code}m'
    ansi_suffix = '\033[0m'
    text_str = sep.join(map(str, text))
    print(f"{ansi_prefix}{text_str}{ansi_suffix}", end=end)

FgColorfulPrint = ColorfulPrint
__all__ = ['ColorfulPrint', 'MeowPrint', 'BgColorfulPrint', 'FgAndBgColorfulPrint', 'FgColorfulPrint']

# ============测试用例============
if __name__ == '__main__':
    ColorfulPrint("这是红色加粗", color='red', bold=True)
    ColorfulPrint("这是绿色常规", color='green')
    ColorfulPrint("多参数", "测试", color='blue', sep='|')
    ColorfulPrint("黄色结尾无换行", color='yellow', end='')
    print(" → 看，没换行～")
    MeowPrint("喵喵喵", meow_count=2, front=True, back=False)
    MeowPrint("喵喵喵", meow_count=3, front=False, back=True)
    MeowPrint("喵喵喵","wfe", meow_count=4, end="mmmmm\n",sep=" ")
    BgColorfulPrint("背景色测试", bg_color='red')
    FgAndBgColorfulPrint("前景色+背景色测试", fg_color='red', bg_color='green')
    FgColorfulPrint("别名这一块")