#!/usr/bin/env python3
"""
Result Saver
结果保存工具
"""

import json
import csv
from pathlib import Path
from dataclasses import asdict

from python_core.utils.logger import logger
from ..types import DetectionResult, OutputFormat


class ResultSaver:
    """结果保存器"""
    
    def save_results(self, result: DetectionResult, output_path: Path, 
                    output_format: OutputFormat) -> None:
        """保存检测结果"""
        try:
            if output_format == OutputFormat.JSON:
                self._save_json(result, output_path)
            elif output_format == OutputFormat.CSV:
                self._save_csv(result, output_path)
            elif output_format == OutputFormat.TXT:
                self._save_txt(result, output_path)
            else:
                raise ValueError(f"不支持的输出格式: {output_format}")
            
            logger.info(f"💾 结果已保存到: {output_path}")
            
        except Exception as e:
            logger.error(f"保存结果失败: {e}")
            raise
    
    def _save_json(self, result: DetectionResult, output_path: Path) -> None:
        """保存为JSON格式"""
        # 确保输出目录存在
        output_path.parent.mkdir(parents=True, exist_ok=True)
        
        # 转换为字典
        result_dict = asdict(result)
        
        # 保存JSON文件
        with open(output_path, 'w', encoding='utf-8') as f:
            json.dump(result_dict, f, indent=2, ensure_ascii=False)
    
    def _save_csv(self, result: DetectionResult, output_path: Path) -> None:
        """保存为CSV格式"""
        # 确保输出目录存在
        output_path.parent.mkdir(parents=True, exist_ok=True)
        
        with open(output_path, 'w', newline='', encoding='utf-8') as f:
            writer = csv.writer(f)
            
            # 写入头部信息
            writer.writerow(['# 场景检测结果'])
            writer.writerow(['# 文件名', result.filename])
            writer.writerow(['# 检测器', result.detector_type])
            writer.writerow(['# 阈值', result.threshold])
            writer.writerow(['# 总场景数', result.total_scenes])
            writer.writerow(['# 总时长', f"{result.total_duration:.2f}秒"])
            writer.writerow(['# 检测时间', f"{result.detection_time:.2f}秒"])
            writer.writerow([])
            
            # 写入场景数据
            writer.writerow(['场景索引', '开始时间(秒)', '结束时间(秒)', '时长(秒)', '开始帧', '结束帧'])
            
            for scene in result.scenes:
                writer.writerow([
                    scene.index,
                    f"{scene.start_time:.2f}",
                    f"{scene.end_time:.2f}",
                    f"{scene.duration:.2f}",
                    scene.start_frame,
                    scene.end_frame
                ])
    
    def _save_txt(self, result: DetectionResult, output_path: Path) -> None:
        """保存为TXT格式"""
        # 确保输出目录存在
        output_path.parent.mkdir(parents=True, exist_ok=True)
        
        with open(output_path, 'w', encoding='utf-8') as f:
            f.write("场景检测结果\n")
            f.write("=" * 50 + "\n\n")
            
            # 基本信息
            f.write(f"文件名: {result.filename}\n")
            f.write(f"检测器: {result.detector_type}\n")
            f.write(f"阈值: {result.threshold}\n")
            f.write(f"总场景数: {result.total_scenes}\n")
            f.write(f"总时长: {result.total_duration:.2f}秒\n")
            f.write(f"检测时间: {result.detection_time:.2f}秒\n")
            f.write(f"检测状态: {'成功' if result.success else '失败'}\n")
            
            if result.error:
                f.write(f"错误信息: {result.error}\n")
            
            f.write("\n场景详情:\n")
            f.write("-" * 50 + "\n")
            
            # 场景列表
            for scene in result.scenes:
                f.write(f"场景 {scene.index + 1:2d}: ")
                f.write(f"{scene.start_time:7.2f}s - {scene.end_time:7.2f}s ")
                f.write(f"(时长: {scene.duration:6.2f}s, ")
                f.write(f"帧: {scene.start_frame:5d} - {scene.end_frame:5d})\n")
