

import bpy
from bpy.types import AddonPreferences
from bpy.props import (CollectionProperty, StringProperty)

from . properties import AssetLibrary
from . core.bl_utils import get_addon_prefs
from . core.asset_library_utils import update_library_path


class AssetLibraryPrefs(AddonPreferences):
    bl_idname = __package__

    libraries : CollectionProperty(type=AssetLibrary)
    bundle_directory : StringProperty(
        name="Path",
        subtype='DIR_PATH',
        default='',
        update=update_library_path
    )

    def draw(self, context):
        prefs = get_addon_prefs()

        layout = self.layout
        col = layout.column(align=False)

        col.prop(self, "bundle_directory", text='Bundle Directory')
        col.separator()

        row = col.row()
        row.label(text='Libraries:')
        #row.alignment = 'RIGHT'
        row.separator_spacer()
        row.operator("assetlibrary.reload_addon", icon='FILE_REFRESH', text='')
        row.operator("assetlibrary.add_library", icon="ADD", text='', emboss=False)

        for lib in self.libraries:# list(self.env_libraries) + list(self.user_libraries):   
            lib.draw(col)




classes = (
    AssetLibraryPrefs,
)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)


def unregister():
    for cls in reversed(classes):
        bpy.utils.unregister_class(cls)
