"""Collapsible panel for export settings."""

import bpy
from bpy.types import UILayout

from rizomuvbridge_ultimate import RizomUVBridgePreferences, constants
from rizomuvbridge_ultimate.functions.bpy_funcs.get_prefs import get_addon_prefs

actions_dic = {
    "RESET_UVS": "Reset / Weld UVs",
    "SHARP_EDGES": "Autoseams - Sharp Edges",
    "MOSAIC": "Autoseams - Mosaic",
    "PELT": "Autoseams - Pelt",
    "BOX": "Autoseams - Box",
}


def draw_dynamic_action_settings(prefs: RizomUVBridgePreferences, container: UILayout, key: str):
    """Draw different settings based on the selected action.

    Args:
    ----
        prefs (RizomUVBridgePreferences): Addon Preferences.
        container (UILayout): Container to render the settings in.
        key (str): Action dictionary key for the chosen action.

    """

    def no_settings_ui() -> None:
        row = container.row()
        row.scale_y = 1.3

        row.label(text="No Settings Available")

    def reset_uvs_ui() -> None:
        row = container.row(align=True)
        row.scale_y = 1.3

        row.prop(
            prefs,
            "apply_to_all_uvmaps_action",
            text="Apply To All UV Maps",
            toggle=True,
        )

    def sharp_edges_ui() -> None:
        row = container.row()
        row.scale_y = 1.3

        row.prop(prefs, "sharp_angle")

    def mosaic_ui() -> None:
        row = container.row()
        row.scale_y = 1.3

        row.prop(prefs, "mosaic_force")

    def pelt_ui() -> None:
        row = container.row()
        row.scale_y = 1.3

        row.prop(prefs, "pelt_trunk", toggle=True)
        row.prop(prefs, "pelt_branch", toggle=True)
        row.prop(prefs, "pelt_leaf", toggle=True)

    actions_dict = {
        "RESET_UVS": reset_uvs_ui,
        "SHARP_EDGES": sharp_edges_ui,
        "MOSAIC": mosaic_ui,
        "PELT": pelt_ui,
    }

    if key in actions_dict:
        return actions_dict[key]()

    return no_settings_ui()


class RizomUVExportSettingsPanel(bpy.types.Panel):
    """Export settings panel."""

    bl_idname = "PANEL_PT_RizomUVBridgeExportSettings"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "RizomUV"
    bl_context = "objectmode"
    bl_label = "Export Settings"
    bl_parent_id = "PANEL_PT_RizomUVBridge"

    def __init__(self):
        """Access to the addon preferences."""
        self.prefs = get_addon_prefs()

    @classmethod
    def poll(cls, context):
        """Only show the panel if the user is on the operations tab."""
        return context.window_manager.RizomUVBridgeGUITabs.names == "OPERATIONS"

    def draw(self, context):
        """Draw method (Required)."""
        layout = self.layout
        box = layout.box()
        self.draw_export_options(box)
        if self.prefs.export_actions != "EXPORT" and self.prefs.attempt_livelink:
            box = layout.box()
            self.common_autoseams_settings(box)
            if self.prefs.export_actions in constants.AUTOSEAMS_ENUMS.keys():
                box = layout.box()
                row = box.row(align=True)
                row.scale_y = 1.3
                row.label(text=f"{actions_dic[self.prefs.export_actions]} Settings:")
                draw_dynamic_action_settings(self.prefs, box, self.prefs.export_actions)

    def draw_export_options(self, container: UILayout):
        """Draw the settings related to file export.

        Args:
        ----
            container (UILayout): Container element to render the settings in.

        """
        row = container.row(align=True)
        row.scale_y = 1.3
        row.prop(self.prefs, "exclude_clones")
        row.prop(self.prefs, "confirm_export")

        row = container.row(align=True)
        row.scale_y = 1.3
        row.prop(self.prefs, "triangulate", text="Triangulate Meshes")

        split = row.split()
        split.prop(self.prefs, "fix_names", text="Fix Names")
        if self.prefs.export_format not in ("OBJ", "USD"):
            split.enabled = False

        row = container.row(align=True)
        row.scale_y = 1.3
        row.prop(self.prefs, "apply_modifiers", text="Apply Modifiers")

        if self.prefs.attempt_livelink:
            row.prop(self.prefs, "apply_rizom_settings", text="Apply Rizom Settings")
        else:
            row.prop(self.prefs, "use_seams", text="Use Seams")

        if self.prefs.attempt_livelink:
            box = container.box()
            row = box.row(align=True)
            row.scale_y = 1.3
            row.prop(self.prefs, "texture_path", text="Texture")

            row = box.row(align=True)
            row.scale_y = 1.5
            row.prop(self.prefs, "export_actions", text="Actions")

    def common_autoseams_settings(self, container: UILayout):
        """Draw the shared settings for all autoseams actions.

        Args:
        ----
            container (UILayout): Container element to render the settings in.

        """
        row = container.row(align=True)
        row.scale_y = 1.3

        split1 = row.split()
        split1.prop(
            self.prefs,
            "apply_to_all_uvmaps_action",
            text="Apply To All UV Maps",
            toggle=True,
        )
        if self.prefs.export_format not in ("FBX", "USD"):
            split1.enabled = False

        split2 = row.split()
        if self.prefs.export_actions in constants.AUTOSEAMS_ENUMS.keys():
            split2.prop(
                self.prefs,
                "weld_before_autoseams",
                text="Weld Before Autoseams",
                toggle=True,
            )
            row = container.row(align=True)
            row.scale_y = 1.3
            row.prop(self.prefs, "cut_handles", toggle=True)
            row.prop(self.prefs, "link_gaps", toggle=True)
            row.prop(self.prefs, "open_cylinders", toggle=True)

            row = container.row(align=True)
            row.scale_y = 1.3
            row.prop(self.prefs, "stretch_quality")
