"""
模拟服务模块 - 用于测试PyInstaller打包
当实际服务模块不可用时，提供基本的模拟功能
"""

import json
import time
from datetime import datetime
from pathlib import Path

class MockTemplateManager:
    """模拟模板管理器"""
    
    @staticmethod
    def get_templates(**kwargs):
        """获取模板列表"""
        return {
            "templates": [
                {
                    "id": "template_001",
                    "name": "商业宣传模板",
                    "description": "适用于商业宣传的视频模板",
                    "duration": 30,
                    "created_at": "2024-01-01T00:00:00Z"
                },
                {
                    "id": "template_002", 
                    "name": "教育培训模板",
                    "description": "适用于教育培训的视频模板",
                    "duration": 60,
                    "created_at": "2024-01-02T00:00:00Z"
                }
            ],
            "total": 2,
            "timestamp": datetime.now().isoformat()
        }
    
    @staticmethod
    def batch_import(**kwargs):
        """批量导入模板"""
        folder_path = kwargs.get('folder_path', '')
        return {
            "imported_count": 5,
            "failed_count": 0,
            "imported_templates": [
                f"template_{i:03d}" for i in range(1, 6)
            ],
            "folder_path": folder_path,
            "timestamp": datetime.now().isoformat()
        }

class MockProjectManager:
    """模拟项目管理器"""
    
    @staticmethod
    def get_projects(**kwargs):
        """获取项目列表"""
        return {
            "projects": [
                {
                    "id": "project_001",
                    "name": "春季促销视频",
                    "status": "active",
                    "created_at": "2024-01-01T00:00:00Z"
                },
                {
                    "id": "project_002",
                    "name": "产品介绍视频", 
                    "status": "completed",
                    "created_at": "2024-01-02T00:00:00Z"
                }
            ],
            "total": 2,
            "timestamp": datetime.now().isoformat()
        }
    
    @staticmethod
    def create_project(**kwargs):
        """创建项目"""
        name = kwargs.get('name', 'New Project')
        return {
            "id": f"project_{int(time.time())}",
            "name": name,
            "status": "active",
            "created_at": datetime.now().isoformat()
        }

class MockMediaManager:
    """模拟媒体管理器"""
    
    @staticmethod
    def get_media_list(**kwargs):
        """获取媒体列表"""
        return {
            "media": [
                {
                    "id": "media_001",
                    "name": "background_music.mp3",
                    "type": "audio",
                    "size": 1024000,
                    "duration": 180
                },
                {
                    "id": "media_002",
                    "name": "intro_video.mp4", 
                    "type": "video",
                    "size": 5120000,
                    "duration": 30
                }
            ],
            "total": 2,
            "timestamp": datetime.now().isoformat()
        }

class MockAudioManager:
    """模拟音频管理器"""
    
    @staticmethod
    def get_audio_list(**kwargs):
        """获取音频列表"""
        return {
            "audio_files": [
                {
                    "id": "audio_001",
                    "name": "background_1.mp3",
                    "duration": 120,
                    "format": "mp3",
                    "sample_rate": 44100
                },
                {
                    "id": "audio_002",
                    "name": "effect_sound.wav",
                    "duration": 5,
                    "format": "wav", 
                    "sample_rate": 48000
                }
            ],
            "total": 2,
            "timestamp": datetime.now().isoformat()
        }

class MockModelManager:
    """模拟模特管理器"""
    
    @staticmethod
    def get_models(**kwargs):
        """获取模特列表"""
        return {
            "models": [
                {
                    "id": "model_001",
                    "name": "商务女性",
                    "category": "business",
                    "age_range": "25-35"
                },
                {
                    "id": "model_002",
                    "name": "时尚男性",
                    "category": "fashion", 
                    "age_range": "20-30"
                }
            ],
            "total": 2,
            "timestamp": datetime.now().isoformat()
        }

class MockResourceCategoryManager:
    """模拟资源分类管理器"""
    
    @staticmethod
    def get_all_categories(**kwargs):
        """获取所有分类"""
        return {
            "categories": [
                {
                    "id": "cat_001",
                    "name": "商业",
                    "description": "商业相关资源",
                    "color": "#FF6B6B"
                },
                {
                    "id": "cat_002", 
                    "name": "教育",
                    "description": "教育相关资源",
                    "color": "#4ECDC4"
                },
                {
                    "id": "cat_003",
                    "name": "娱乐",
                    "description": "娱乐相关资源", 
                    "color": "#45B7D1"
                }
            ],
            "total": 3,
            "timestamp": datetime.now().isoformat()
        }

class MockFileManager:
    """模拟文件管理器"""
    
    @staticmethod
    def list_files(**kwargs):
        """列出文件"""
        path = kwargs.get('path', '.')
        try:
            path_obj = Path(path)
            if path_obj.exists() and path_obj.is_dir():
                files = []
                for item in path_obj.iterdir():
                    files.append({
                        "name": item.name,
                        "type": "directory" if item.is_dir() else "file",
                        "size": item.stat().st_size if item.is_file() else 0,
                        "modified": datetime.fromtimestamp(item.stat().st_mtime).isoformat()
                    })
                return {
                    "path": str(path_obj),
                    "files": files[:10],  # 限制返回数量
                    "total": len(files),
                    "timestamp": datetime.now().isoformat()
                }
            else:
                return {
                    "error": f"Path does not exist or is not a directory: {path}",
                    "timestamp": datetime.now().isoformat()
                }
        except Exception as e:
            return {
                "error": f"Failed to list files: {str(e)}",
                "timestamp": datetime.now().isoformat()
            }

class MockAIVideo:
    """模拟AI视频生成器"""
    
    @staticmethod
    def test_environment(**kwargs):
        """测试环境"""
        return {
            "python_version": "3.11.0",
            "dependencies": {
                "requests": "available",
                "PIL": "available", 
                "json": "available"
            },
            "status": "ready",
            "timestamp": datetime.now().isoformat()
        }
    
    @staticmethod
    def get_status(**kwargs):
        """获取状态"""
        return {
            "service": "AI Video Generator",
            "status": "running",
            "version": "2.0.0",
            "last_check": datetime.now().isoformat()
        }

# 注册模拟服务
MOCK_SERVICES = {
    'services.template_manager': MockTemplateManager,
    'services.project_manager': MockProjectManager,
    'services.media_manager': MockMediaManager,
    'services.audio_manager': MockAudioManager,
    'services.model_manager': MockModelManager,
    'services.resource_category_manager': MockResourceCategoryManager,
    'services.file_manager': MockFileManager,
    'ai_video.video_generator': MockAIVideo,
}

def get_mock_service(module_path):
    """获取模拟服务"""
    return MOCK_SERVICES.get(module_path)
