"""
模式设置（新建/编辑）
"""
import streamlit as st
from PIL import Image
from typing import Dict, Tuple
import sys
from pathlib import Path

# 添加父目录到路径
sys.path.insert(0, str(Path(__file__).parent.parent))

try:
    from ..table_line_generator import TableLineGenerator
except ImportError:
    from table_line_generator import TableLineGenerator

from .display_controls import create_display_settings_section, create_undo_redo_section
from .analysis_controls import create_analysis_section


def setup_new_annotation_mode(
    ocr_data: list,
    image: Image.Image,
    display_config: Dict
) -> Tuple:
    """
    设置新建标注模式
    
    Returns:
        (generator, structure, undo_stack, line_width, display_mode, zoom_level, show_line_numbers)
    """
    # 🔑 获取当前工具类型
    tool = st.session_state.get('current_tool', 'ppstructv3')
    
    # 初始化生成器
    if 'generator' not in st.session_state:
        st.session_state.generator = TableLineGenerator(image, ocr_data)
    
    # 分析控件
    structure = create_analysis_section(
        st.session_state.generator,
        tool=tool  # 🔑 传入工具类型
    )
    
    # 显示控件
    line_width, display_mode, zoom_level, show_line_numbers = create_display_settings_section(
        display_config
    )
    
    # 撤销/重做
    undo_stack = []
    
    return (
        st.session_state.generator,
        structure,
        undo_stack,
        line_width,
        display_mode,
        zoom_level,
        show_line_numbers
    )


def setup_edit_annotation_mode(
    structure: Dict,
    image: Image.Image,
    display_config: Dict
) -> Tuple:
    """
    设置编辑标注模式
    
    Returns:
        (image, line_width, display_mode, zoom_level, show_line_numbers)
    """
    # 显示控件
    line_width, display_mode, zoom_level, show_line_numbers = create_display_settings_section(
        display_config
    )
    
    # 撤销/重做控件
    create_undo_redo_section()
    
    return (
        image,
        line_width,
        display_mode,
        zoom_level,
        show_line_numbers
    )