import traceback
import bpy
from typing import Set, List, Tuple, ClassVar
from bpy.types import Operator, Context, Object
from ...core.logging_setup import logger
from ...core.translations import t
from ...core.common import (
    get_active_armature,
    get_all_meshes,
    validate_meshes,
    join_mesh_objects,
    ProgressTracker
)
from ...core.armature_validation import validate_armature
import traceback

class AvatarToolkit_OT_JoinAllMeshes(Operator):
    """Operator to join all meshes in the scene"""
    bl_idname: ClassVar[str] = "avatar_toolkit.join_all_meshes"
    bl_label: ClassVar[str] = t("Optimization.join_all_meshes")
    bl_description: ClassVar[str] = t("Optimization.join_all_meshes_desc")
    bl_options: ClassVar[Set[str]] = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context: Context) -> bool:
        armature: Object | None = get_active_armature(context)
        if not armature:
            return False
        valid: bool
        valid, _, _ = validate_armature(armature)
        return valid

    def execute(self, context: Context) -> Set[str]:
        try:
            armature: Object = get_active_armature(context)
            meshes: List[Object] = get_all_meshes(context)
            
            valid: bool
            message: str
            valid, message = validate_meshes(meshes)
            if not valid:
                self.report({'WARNING'}, message)
                return {'CANCELLED'}

            with ProgressTracker(context, 5, "Joining All Meshes") as progress:
                joined_mesh = join_mesh_objects(context, meshes, progress)
                
                if joined_mesh:
                    context.view_layer.objects.active = armature
                    self.report({'INFO'}, t("Optimization.meshes_joined"))
                    return {'FINISHED'}
                else:
                    self.report({'ERROR'}, t("Optimization.error.join_meshes"))
                    return {'CANCELLED'}
                    
        except Exception:
            logger.error(f"Failed to join meshes: {traceback.format_exc()}")
            self.report({'ERROR'}, t("Optimization.error.join_meshes", error=traceback.format_exc()))
            return {'CANCELLED'}

class AvatarToolkit_OT_JoinSelectedMeshes(Operator):
    """Operator to join selected meshes"""
    bl_idname: ClassVar[str] = "avatar_toolkit.join_selected_meshes"
    bl_label: ClassVar[str] = t("Optimization.join_selected_meshes")
    bl_description: ClassVar[str] = t("Optimization.join_selected_meshes_desc")
    bl_options: ClassVar[Set[str]] = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context: Context) -> bool:
        armature: Object | None = get_active_armature(context)
        if not armature:
            return False
        valid: bool
        valid, _, _ = validate_armature(armature)
        return (valid and 
                context.mode == 'OBJECT' and 
                len([obj for obj in context.selected_objects if obj.type == 'MESH']) > 1)

    def execute(self, context: Context) -> Set[str]:
        try:
            selected_meshes: List[Object] = [obj for obj in context.selected_objects if obj.type == 'MESH']
            
            valid: bool
            message: str
            valid, message = validate_meshes(selected_meshes)
            if not valid:
                self.report({'WARNING'}, message)
                return {'CANCELLED'}

            with ProgressTracker(context, 5, "Joining Selected Meshes") as progress:
                joined_mesh = join_mesh_objects(context, selected_meshes, progress)
                
                if joined_mesh:
                    self.report({'INFO'}, t("Optimization.selected_meshes_joined"))
                    return {'FINISHED'}
                else:
                    self.report({'ERROR'}, t("Optimization.error.join_selected"))
                    return {'CANCELLED'}
                
        except Exception:
            logger.error(f"Failed to join selected meshes: {traceback.format_exc()}")
            self.report({'ERROR'}, t("Optimization.error.join_selected", error=traceback.format_exc()))
            return {'CANCELLED'}
