"""RizomUV settings panel."""

import bpy

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


class RizomUVSettingsPanel(bpy.types.Panel):
    """Import settings panel."""

    bl_idname = "PANEL_PT_RizomUVBridgeRizomSettings"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "RizomUV"
    bl_context = "objectmode"
    bl_label = "RizomUV 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 and livelink is enabled.

        Specifically not using get_addon_prefs as this is called a lot so I am avoiding the
        overhead of the get_prefs function.

        """
        return (
            context.window_manager.RizomUVBridgeGUITabs.names == "RIZOM_LIVE"
            and context.preferences.addons[constants.PACKAGE_NAME].preferences.attempt_livelink
            is True
        )

    def draw(self, context):
        """Draw method (Required)."""
        layout = self.layout
        box = layout.box()
        box.label(text="Unwrap Settings:")
        row = box.row()
        row.scale_y = 1.3
        row.prop(self.prefs, "prevent_flips", toggle=True)
        row.prop(self.prefs, "prevent_overlaps", toggle=True)

        box = layout.box()
        box.label(text="Packing Settings:")
        row = box.row(align=True)
        row.scale_y = 1.3
        row.prop(self.prefs, "accuracy")
        row.prop(self.prefs, "iterations")

        row = box.row(align=True)
        row.scale_y = 1.3
        row.prop(self.prefs, "orientation", text="")
        row.prop(self.prefs, "step_angle")

        box = layout.box()
        box.label(text="Layout Settings:")
        row = box.row(align=True)
        row.scale_y = 1.3

        row.prop(self.prefs, "margin")
        row.prop(self.prefs, "padding")

        row = box.row(align=True)
        row.scale_y = 1.3

        row.prop(self.prefs, "map_res")

        row = box.row(align=True)
        row.scale_y = 1.3

        row.prop(self.prefs, "td_target")
        row.prop(self.prefs, "td_unit", text="")
