"""
表格结构渲染视图
"""
import streamlit as st
from typing import Dict

from .drawing import get_cached_table_lines_image
from .viewer import show_image_with_scroll
from .adjustments import create_adjustment_section


def render_table_structure_view(
    structure: Dict, 
    image, 
    line_width: int, 
    display_mode: str, 
    zoom_level: float, 
    show_line_numbers: bool, 
    viewport_width: int, 
    viewport_height: int
):
    """
    渲染表格结构视图（统一三种模式的显示逻辑）
    
    Args:
        structure: 表格结构
        image: 图片对象
        line_width: 线条宽度
        display_mode: 显示模式
        zoom_level: 缩放级别
        show_line_numbers: 是否显示线条编号
        viewport_width: 视口宽度
        viewport_height: 视口高度
    """
    # 绘制表格线
    img_with_lines = get_cached_table_lines_image(
        image, structure, line_width=line_width, show_numbers=show_line_numbers
    )
    
    # 根据显示模式显示图片
    if display_mode == "对比显示":
        col1, col2 = st.columns(2)
        with col1:
            show_image_with_scroll(
                image, "原图", 
                viewport_width, viewport_height, zoom_level
            )
        with col2:
            show_image_with_scroll(
                img_with_lines, "表格线", 
                viewport_width, viewport_height, zoom_level
            )
    
    elif display_mode == "仅显示划线图":
        show_image_with_scroll(
            img_with_lines, 
            f"表格线图 (缩放: {zoom_level:.0%})", 
            viewport_width, 
            viewport_height, 
            zoom_level
        )
    
    else:  # 仅显示原图
        show_image_with_scroll(
            image, 
            f"原图 (缩放: {zoom_level:.0%})", 
            viewport_width, 
            viewport_height, 
            zoom_level
        )
    
    # 手动调整区域
    create_adjustment_section(structure)
    
    # 显示详细信息
    with st.expander("📊 表格结构详情"):
        st.json({
            "行数": len(structure['rows']),
            "列数": len(structure['columns']),
            "横线数": len(structure.get('horizontal_lines', [])),
            "竖线数": len(structure.get('vertical_lines', [])),
            "横线坐标": structure.get('horizontal_lines', []),
            "竖线坐标": structure.get('vertical_lines', []),
            "标准行高": structure.get('row_height'),
            "列宽度": structure.get('col_widths'),
            "修改的横线": list(structure.get('modified_h_lines', set())),
            "修改的竖线": list(structure.get('modified_v_lines', set()))
        })