"""论文格式定义."""

from dataclasses import dataclass
from typing import Dict


@dataclass
class FontSettings:
    """字体设置."""

    name: str = "宋体"
    english_name: str = "Times New Roman"
    size: float = 12  # 磅
    bold: bool = False
    italic: bool = False


@dataclass
class ParagraphSettings:
    """段落设置."""

    line_spacing: float = 1.5  # 行距
    first_line_indent: float = 0.0  # 首行缩进（字符数）
    space_before: float = 0.0  # 段前间距（磅）
    space_after: float = 0.0  # 段后间距（磅）
    alignment: str = "left"  # left, center, right, justify


@dataclass
class PageSettings:
    """页面设置."""

    top_margin: float = 2.54  # 厘米
    bottom_margin: float = 2.54
    left_margin: float = 3.17
    right_margin: float = 3.17
    page_width: float = 21.0  # A4 宽度
    page_height: float = 29.7  # A4 高度


@dataclass
class ThesisFormat:
    """论文格式规范."""

    name: str
    description: str

    # 页面设置
    page: PageSettings

    # 字体设置
    title_font: FontSettings  # 论文标题
    heading1_font: FontSettings  # 一级标题
    heading2_font: FontSettings  # 二级标题
    heading3_font: FontSettings  # 三级标题
    body_font: FontSettings  # 正文
    abstract_title_font: FontSettings  # 摘要标题
    abstract_content_font: FontSettings  # 摘要内容
    reference_font: FontSettings  # 参考文献

    # 段落设置
    title_paragraph: ParagraphSettings
    heading1_paragraph: ParagraphSettings
    heading2_paragraph: ParagraphSettings
    heading3_paragraph: ParagraphSettings
    body_paragraph: ParagraphSettings
    abstract_paragraph: ParagraphSettings
    reference_paragraph: ParagraphSettings

    # 特殊要求
    has_cover_page: bool = True
    has_abstract_separate_page: bool = True
    has_toc: bool = True
    toc_levels: int = 3


# 中山大学格式
SYSU_FORMAT = ThesisFormat(
    name="sysu",
    description="中山大学本科生毕业论文格式",
    page=PageSettings(
        top_margin=2.54,
        bottom_margin=2.54,
        left_margin=3.17,
        right_margin=3.17,
    ),
    title_font=FontSettings(name="黑体", english_name="Times New Roman", size=22, bold=True),
    heading1_font=FontSettings(name="黑体", english_name="Times New Roman", size=16, bold=True),
    heading2_font=FontSettings(name="黑体", english_name="Times New Roman", size=14, bold=True),
    heading3_font=FontSettings(name="黑体", english_name="Times New Roman", size=12, bold=True),
    body_font=FontSettings(name="宋体", english_name="Times New Roman", size=12),
    abstract_title_font=FontSettings(
        name="黑体", english_name="Times New Roman", size=16, bold=True
    ),
    abstract_content_font=FontSettings(name="宋体", english_name="Times New Roman", size=12),
    reference_font=FontSettings(name="宋体", english_name="Times New Roman", size=10.5),
    title_paragraph=ParagraphSettings(
        line_spacing=1.5,
        alignment="center",
        space_before=24,
        space_after=24,
    ),
    heading1_paragraph=ParagraphSettings(
        line_spacing=1.5,
        alignment="left",
        space_before=18,
        space_after=12,
    ),
    heading2_paragraph=ParagraphSettings(
        line_spacing=1.5,
        alignment="left",
        space_before=12,
        space_after=6,
    ),
    heading3_paragraph=ParagraphSettings(
        line_spacing=1.5,
        alignment="left",
        space_before=6,
        space_after=6,
    ),
    body_paragraph=ParagraphSettings(
        line_spacing=1.5,
        first_line_indent=2.0,
        alignment="justify",
    ),
    abstract_paragraph=ParagraphSettings(
        line_spacing=1.5,
        first_line_indent=2.0,
        alignment="justify",
    ),
    reference_paragraph=ParagraphSettings(
        line_spacing=1.0,
        alignment="left",
    ),
    has_cover_page=True,
    has_abstract_separate_page=True,
    has_toc=True,
    toc_levels=3,
)

# 华南师范大学格式
SCNU_FORMAT = ThesisFormat(
    name="scnu",
    description="华南师范大学本科生毕业论文格式",
    page=PageSettings(
        top_margin=2.54,
        bottom_margin=2.54,
        left_margin=3.0,
        right_margin=3.0,
    ),
    title_font=FontSettings(name="黑体", english_name="Times New Roman", size=18, bold=True),
    heading1_font=FontSettings(name="黑体", english_name="Times New Roman", size=16, bold=True),
    heading2_font=FontSettings(name="黑体", english_name="Times New Roman", size=14, bold=True),
    heading3_font=FontSettings(name="黑体", english_name="Times New Roman", size=12, bold=True),
    body_font=FontSettings(name="宋体", english_name="Times New Roman", size=12),
    abstract_title_font=FontSettings(
        name="黑体", english_name="Times New Roman", size=14, bold=True
    ),
    abstract_content_font=FontSettings(name="宋体", english_name="Times New Roman", size=12),
    reference_font=FontSettings(name="宋体", english_name="Times New Roman", size=10.5),
    title_paragraph=ParagraphSettings(
        line_spacing=1.5,
        alignment="center",
        space_before=18,
        space_after=18,
    ),
    heading1_paragraph=ParagraphSettings(
        line_spacing=1.5,
        alignment="left",
        space_before=18,
        space_after=12,
    ),
    heading2_paragraph=ParagraphSettings(
        line_spacing=1.5,
        alignment="left",
        space_before=12,
        space_after=6,
    ),
    heading3_paragraph=ParagraphSettings(
        line_spacing=1.5,
        alignment="left",
        space_before=6,
        space_after=6,
    ),
    body_paragraph=ParagraphSettings(
        line_spacing=1.5,
        first_line_indent=2.0,
        alignment="justify",
    ),
    abstract_paragraph=ParagraphSettings(
        line_spacing=1.5,
        first_line_indent=2.0,
        alignment="justify",
    ),
    reference_paragraph=ParagraphSettings(
        line_spacing=1.0,
        alignment="left",
    ),
    has_cover_page=True,
    has_abstract_separate_page=False,  # 中英文摘要连续排版
    has_toc=True,
    toc_levels=3,
)

# 通用格式
GENERIC_FORMAT = ThesisFormat(
    name="generic",
    description="通用论文格式（GB/T 7713.1-2006）",
    page=PageSettings(
        top_margin=2.54,
        bottom_margin=2.54,
        left_margin=3.17,
        right_margin=3.17,
    ),
    title_font=FontSettings(name="黑体", english_name="Times New Roman", size=16, bold=True),
    heading1_font=FontSettings(name="黑体", english_name="Times New Roman", size=14, bold=True),
    heading2_font=FontSettings(name="黑体", english_name="Times New Roman", size=12, bold=True),
    heading3_font=FontSettings(name="黑体", english_name="Times New Roman", size=12, bold=True),
    body_font=FontSettings(name="宋体", english_name="Times New Roman", size=12),
    abstract_title_font=FontSettings(
        name="黑体", english_name="Times New Roman", size=14, bold=True
    ),
    abstract_content_font=FontSettings(name="宋体", english_name="Times New Roman", size=12),
    reference_font=FontSettings(name="宋体", english_name="Times New Roman", size=10.5),
    title_paragraph=ParagraphSettings(
        line_spacing=1.5,
        alignment="center",
        space_before=18,
        space_after=18,
    ),
    heading1_paragraph=ParagraphSettings(
        line_spacing=1.5,
        alignment="left",
        space_before=18,
        space_after=12,
    ),
    heading2_paragraph=ParagraphSettings(
        line_spacing=1.5,
        alignment="left",
        space_before=12,
        space_after=6,
    ),
    heading3_paragraph=ParagraphSettings(
        line_spacing=1.5,
        alignment="left",
        space_before=6,
        space_after=6,
    ),
    body_paragraph=ParagraphSettings(
        line_spacing=1.5,
        first_line_indent=2.0,
        alignment="justify",
    ),
    abstract_paragraph=ParagraphSettings(
        line_spacing=1.5,
        first_line_indent=2.0,
        alignment="justify",
    ),
    reference_paragraph=ParagraphSettings(
        line_spacing=1.0,
        alignment="left",
    ),
    has_cover_page=True,
    has_abstract_separate_page=True,
    has_toc=True,
    toc_levels=3,
)

# 格式注册表
FORMATS: Dict[str, ThesisFormat] = {
    "sysu": SYSU_FORMAT,
    "scnu": SCNU_FORMAT,
    "generic": GENERIC_FORMAT,
}


def get_format(university: str) -> ThesisFormat:
    """获取指定大学的格式规范.

    Args:
        university: 大学代码

    Returns:
        ThesisFormat: 格式规范

    Raises:
        ValueError: 如果大学代码不存在
    """
    if university not in FORMATS:
        available = ", ".join(FORMATS.keys())
        raise ValueError(f"不支持的大学代码: {university}. 可用选项: {available}")
    return FORMATS[university]


def list_formats() -> Dict[str, str]:
    """列出所有支持的格式.

    Returns:
        Dict[str, str]: 大学代码和描述的字典
    """
    return {code: fmt.description for code, fmt in FORMATS.items()}
