"""A Blender addon for version 3.3+ designed to enable workflows between Blender and RizomUV.

- Compatibility
    - >= Blender 3.3
    - >= Rizom 2022.1.35
    - Windows
    - MacOS

MacOS and < Rizom 2023 have limited functionality due to lack of RizomUVLink support,
export and import functionality is fully supported. Unsupported features:
    - Script presets
    - Automatic UV mapping
    - Remote RizomUV actions
    - Remote RizomUV setting controls

----------------------------------

- Author: [Matt Ashpole](https://github.com/zmatash)
- Store: (https://blendermarket.com/products/rizomuv-bridge)

"""

from pathlib import Path

import bpy
from bpy.props import PointerProperty
from bpy.types import WindowManager

from rizomuvbridge_ultimate import constants, preferences, properties
from rizomuvbridge_ultimate.functions.utils.addons import disable_old_versions
from rizomuvbridge_ultimate.functions.utils.path_utils import (
    set_rizomlink_availability,
)
from rizomuvbridge_ultimate.operators import (
    edit_uvmaps,
    rizom_action,
    rizom_automatic,
    rizom_export,
    rizom_import,
    rizom_load,
)
from rizomuvbridge_ultimate.preferences import RizomUVBridgePreferences
from rizomuvbridge_ultimate.ui import root_panel
from rizomuvbridge_ultimate.ui.live_tab import rizom_settings_subpanel
from rizomuvbridge_ultimate.ui.operations_tab import (
    export_settings_subpanel,
    import_settings_subpanel,
)

disable_old_versions()

bl_info = {
    "name": "RizomUV Bridge - Ultimate Edition",
    "description": "Enables a seamless workflow between Blender and RizomUV.",
    "author": "Matt Ashpole",
    "version": (1, 1, 2),
    "blender": (3, 3, 0),
    "location": "View3D > Sidebar",
    "doc_url": "https://blendermarket.com/products/rizomuv-bridge/docs",
    "tracker_url": "https://blendermarket.com/products/rizomuv-bridge",
    "category": "Import-Export",
}

PROPERTIES = (
    preferences.RizomUVBridgePreferences,
    properties.RizomUVBridgeGUITabs,
    properties.RizomUVBridgeUVMaps,
)

OPERATORS = (
    rizom_export.ExportToRizom,
    rizom_import.ImportFromRizom,
    rizom_load.LoadInRizom,
    rizom_action.RizomAction,
    rizom_automatic.RizomAutomatic,
    edit_uvmaps.EditUVMaps,
)
LAYOUTS = (
    root_panel.RizomUVBridgePanel,
    export_settings_subpanel.RizomUVExportSettingsPanel,
    import_settings_subpanel.RizomUVImportSettingsPanel,
    rizom_settings_subpanel.RizomUVSettingsPanel,
)


def register():
    """Register operators, menus and properties when addon is enabled."""
    for item in PROPERTIES:
        bpy.utils.register_class(item)

    prefs: RizomUVBridgePreferences = bpy.context.preferences.addons[
        constants.PACKAGE_NAME
    ].preferences

    set_rizomlink_availability(prefs, Path(prefs.rizom_path))

    for item in OPERATORS:
        bpy.utils.register_class(item)

    for item in LAYOUTS:
        item.bl_category = prefs.addon_tab_name
        bpy.utils.register_class(item)

    WindowManager.RizomUVBridgeGUITabs = PointerProperty(type=properties.RizomUVBridgeGUITabs)
    WindowManager.RizomUVBridgeUVMaps = PointerProperty(type=properties.RizomUVBridgeUVMaps)


def unregister():
    """Unregister operators, menus and properties when addon is disabled."""
    for item in PROPERTIES:
        bpy.utils.unregister_class(item)

    for item in OPERATORS:
        bpy.utils.unregister_class(item)

    for item in LAYOUTS:
        bpy.utils.unregister_class(item)

    del WindowManager.RizomUVBridgeGUITabs
    del WindowManager.RizomUVBridgeUVMaps
