"""Panel class for the UI."""

import bpy
from bpy.types import Context, UILayout

from rizomuvbridge_ultimate import constants
from rizomuvbridge_ultimate.functions.bpy_funcs.get_prefs import get_addon_prefs, get_uvmaps_prop
from rizomuvbridge_ultimate.properties import RizomUVBridgeGUITabs


class RizomUVBridgePanel(bpy.types.Panel):
    """The root UI panel for the addon."""

    bl_idname = "PANEL_PT_RizomUVBridge"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "RizomUV"
    bl_context = "objectmode"
    bl_label = "RizomUV Bridge - Ultimate Edition"

    def __init__(self):
        """Access to the addon preferences."""
        self.prefs = get_addon_prefs()
        self.uvmaps_prop = get_uvmaps_prop()
        self.tabs_prop: RizomUVBridgeGUITabs = bpy.context.window_manager.RizomUVBridgeGUITabs

    def draw(self, context: Context):
        """Panel draw method (Required)."""
        layout = self.layout

        box = layout.box()
        row = box.row()
        row.scale_y = 1.3

        row.prop(self.tabs_prop, "names", icon_only=True, expand=True)

        if self.tabs_prop.names == "OPERATIONS":
            row.label(text="UV Transfer")
            self.draw_primary_ops_ui(box)
            self.draw_uv_maps_ui(box)
            self.draw_file_manager_ui(box)

        elif self.tabs_prop.names == "RIZOM_LIVE":
            row.label(text="Rizom Live Access")
            if self.prefs.attempt_livelink:
                self.draw_live_panel(box)
            else:
                box = layout.box()
                row = box.row()
                row.scale_y = 1.3
                row.label(text="RizomUVLink is disabled", icon="INFO")
        elif self.tabs_prop.names == "ADDON_SETTINGS":
            row.label(text="Addon Settings")
            self.draw_addon_configuration_ui(box)

    def draw_addon_configuration_ui(self, container: UILayout):
        """Draw the addon configuration UI inside the given container.

        Args:
        ----
            container (UILayout): Container to draw the UI inside.

        """
        box = container.box()
        row = box.row()
        row.scale_y = 1.3
        row.prop(self.prefs, "rizom_path", text="Rizom Path")

        box = container.box()
        row = box.row()
        row.scale_y = 1.3
        row.prop(self.prefs, "addon_tab_name", text="Addon Tab")

        box = container.box()

        if not self.prefs.livelink_allowed:
            row = box.row()
            row.scale_y = 1.3
            row.label(text="RizomUVLink is unavailable, check docs for compatibility", icon="INFO")

        link_row = box.row()
        link_row.scale_y = 1.3
        link_row.prop(self.prefs, "attempt_livelink", text="Use RizomUVLink")
        link_row.enabled = self.prefs.livelink_allowed

    def draw_primary_ops_ui(self, container: UILayout):
        """Draw the primary operations UI inside the given container.

        Includes the Export and Import buttons.
        Includes the Automatic and Close Rizom buttons if RizomUVLink is enabled.

        Args:
        ----
            container (UILayout): Container to draw the UI inside.

        """
        row = container.row(align=True)
        row.label(text="Primary Operations:", icon="UV_DATA")

        row = container.row()
        row.scale_y = 1.3

        row.operator("ruv.rizom_export", text="Export", icon="EXPORT")
        row.operator("ruv.rizom_import", text="Import", icon="IMPORT")

        if self.prefs.attempt_livelink:
            row = container.row()
            row.scale_y = 1.3

            row.operator("ruv.rizom_automatic", text="Automatic", icon="AUTO")
            row.operator("ruv.rizom_action", text="Close Rizom", icon="QUIT").action = "CLOSE"

    def draw_uv_maps_ui(self, container: UILayout):
        """Draw the UV maps UI inside the given container.

        Includes the UV map selector and the Validate UVSets button.

        Args:
        ----
            container (UILayout): Container to draw the UI inside.

        """
        row = container.row(align=True)
        row.scale_y = 1.3
        split = row.split(factor=0.65, align=True)
        split.prop(self.uvmaps_prop, "uv_maps")
        split.operator("ruv.edit_uvmaps", text="Edit Names", icon="UV_DATA")

    def draw_file_manager_ui(self, container: UILayout):
        """Draw the file manager UI inside the given container.

        Includes the Load button and the File Type selector.

        Args:
        ----
            container (UILayout): Container to draw the UI inside.

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

        split = row.split(factor=0.65, align=True)
        split.prop(self.prefs, "export_format")
        row = split.row(align=True)
        row.operator("ruv.rizom_load", text="Load", icon="FILE_CACHE")

    def draw_live_panel(self, container: UILayout):
        """Draw the Rizom Live panel.

        Args:
        ----
            container (UILayout): Container to draw the UI inside.

        """
        row = container.row(align=True)
        row.scale_y = 1.3
        row.prop(self.prefs, "remote_actions", text="Actions")
        op = row.operator("ruv.rizom_action", text="Execute")
        op.action = self.prefs.remote_actions
        op.apply_to_all = self.prefs.apply_to_all_uvmaps_action

        if self.prefs.remote_actions in constants.AUTOSEAMS_ENUMS.keys():
            self.common_autoseams_settings(container)
        elif self.prefs.remote_actions not in ["OFFSET_STACKS", "CLOSE"]:
            row = container.row(align=True)
            row.scale_y = 1.3
            row.prop(
                self.prefs, "apply_to_all_uvmaps_action", text="Apply To All UV Maps", toggle=True
            )

    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.remote_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")
