import bpy
from typing import Set, Dict, List, Optional
from bpy.types import (
    Operator, 
    Panel, 
    Context, 
    UILayout, 
    WindowManager,
    Event
)
from .main_panel import AvatarToolKit_PT_AvatarToolkitPanel, CATEGORY_NAME
from ..core.translations import t, get_languages_list
from ..core.armature_validation import AvatarToolkit_OT_HighlightProblemBones, AvatarToolkit_OT_ClearBoneHighlighting

class AvatarToolkit_OT_TranslationRestartPopup(Operator):
    """Popup dialog shown after language change to inform about restart requirement"""
    bl_idname: str = "avatar_toolkit.translation_restart_popup"
    bl_label: str = t("Language.changed.title")
    
    def execute(self, context: Context) -> Set[str]:
        return {'FINISHED'}
    
    def invoke(self, context: Context, event: Event) -> Set[str]:
        wm: WindowManager = context.window_manager
        return wm.invoke_props_dialog(self)
    
    def draw(self, context: Context) -> None:
        layout: UILayout = self.layout
        layout.label(text=t("Language.changed.success"))
        layout.label(text=t("Language.changed.restart"))

class AvatarToolKit_PT_SettingsPanel(Panel):
    """Settings panel for Avatar Toolkit containing language preferences"""
    bl_label: str = t("Settings.label")
    bl_idname: str = "OBJECT_PT_avatar_toolkit_settings"
    bl_space_type: str = 'VIEW_3D'
    bl_region_type: str = 'UI'
    bl_category: str = CATEGORY_NAME
    bl_parent_id: str = AvatarToolKit_PT_AvatarToolkitPanel.bl_idname
    bl_order: int = 8
    bl_options = {'DEFAULT_CLOSED'}

    def draw(self, context: Context) -> None:
        """Draw the settings panel layout with language selection"""
        layout: UILayout = self.layout
        props = context.scene.avatar_toolkit
        
        # Language Settings
        lang_box: UILayout = layout.box()
        col: UILayout = lang_box.column(align=True)
        row: UILayout = col.row()
        row.scale_y = 1.2
        row.label(text=t("Settings.language"), icon='WORLD')
        col.separator()
        col.prop(props, "language", text="")
        
        # Validation Settings
        val_box: UILayout = layout.box()
        col = val_box.column(align=True)
        row = col.row()
        row.scale_y = 1.2
        row.label(text=t("Settings.validation_mode"), icon='CHECKMARK')
        col.separator()
        col.prop(props, "validation_mode", text="")
        
        # Bone Highlighting Settings
        bone_box: UILayout = layout.box()
        col = bone_box.column(align=True)
        row = col.row()
        row.scale_y = 1.2
        row.label(text=t("Settings.bone_highlighting"), icon='BONE_DATA')
        col.separator()
        col.prop(props, "highlight_problem_bones")
        if props.highlight_problem_bones:
            col.operator(AvatarToolkit_OT_HighlightProblemBones.bl_idname, icon='COLOR')
        else:
            col.operator(AvatarToolkit_OT_ClearBoneHighlighting.bl_idname, icon='X')

        # Debug Settings
        debug_box = layout.box()
        col = debug_box.column()
        row = col.row(align=True)
        row.prop(props, "debug_expand", 
                icon="TRIA_DOWN" if props.debug_expand 
                else "TRIA_RIGHT", 
                icon_only=True, emboss=False)
        row.label(text=t("Settings.debug"), icon='CONSOLE')
        
        if props.debug_expand:
            col = debug_box.column(align=True)
            col.prop(props, "enable_logging")
            
            if props.enable_logging:
                col.prop(props, "log_level")