import bpy
from bpy.props import (EnumProperty, StringProperty, PointerProperty, BoolProperty, 
        CollectionProperty, IntProperty)

import inspect

'''
def get_operator_items(self,context):
    items = []
    from . import func_picker as mod

    for name, func in inspect.getmembers(mod, inspect.isfunction):
            if inspect.getmodule(func) == mod:
                items.append((name,name,""))

    return items
'''

def bones_item(self,context):
    items = []

    if context.scene.rig_picker.rig:
        for bone in context.scene.rig_picker.rig.pose.bones:
            items.append((bone.name,bone.name,''))
    return items


class RP_PG_picker_source(bpy.types.PropertyGroup):
    source : StringProperty(subtype='FILE_PATH')


class RP_PG_armature_ui_settings(bpy.types.PropertyGroup):
    name: StringProperty()
    source: StringProperty(subtype='FILE_PATH')
    sources: CollectionProperty(type=RP_PG_picker_source)


class RP_PG_object_ui_settings(bpy.types.PropertyGroup):
    shape_type: EnumProperty(items=[(i.upper(), i, "") for i in ('Bone', 'Display', 'Operator')])
    #idname: StringProperty()
    #arguments: StringProperty()
    operator: StringProperty()
    shortcut: StringProperty()
    name: StringProperty()


class RP_PG_collection_ui_settings(bpy.types.PropertyGroup):
    enabled : BoolProperty(default=False)
    rig: PointerProperty(type=bpy.types.Object)
    canvas: PointerProperty(type=bpy.types.Object)
    symmetry: PointerProperty(type=bpy.types.Object)
    destination: StringProperty(subtype='FILE_PATH')
    use_pick_bone : BoolProperty(default=False)
    link_shape : BoolProperty(default=False)


class RP_PG_scene_ui_settings(bpy.types.PropertyGroup):
    use_pick_bone : BoolProperty(default=False)


class RP_OT_operator_selector(bpy.types.Operator):
    bl_label = "Select function"
    bl_idname = "rigpicker.operator_selector"
    bl_property = "idname"
    #bl_options = {'REGISTER', 'UNDO'}

    idname: EnumProperty(items=[])

    def execute(self, context):
        ob = context.object
        ob.rig_picker.idname = self.idname
        context.area.tag_redraw()
        return {'FINISHED'}

    def invoke(self, context, event):
        wm = context.window_manager
        wm.invoke_search_popup(self)
        return {'FINISHED'}


classes = (
    RP_PG_picker_source,
    RP_PG_object_ui_settings,
    RP_PG_collection_ui_settings,
    RP_PG_scene_ui_settings,
    RP_PG_armature_ui_settings,
    RP_OT_operator_selector,
)

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

    bpy.types.Armature.rig_picker = bpy.props.PointerProperty(type=RP_PG_armature_ui_settings)
    bpy.types.Object.rig_picker = bpy.props.PointerProperty(type=RP_PG_object_ui_settings)
    bpy.types.Collection.rig_picker = bpy.props.PointerProperty(type=RP_PG_collection_ui_settings)
    bpy.types.Scene.rig_picker = bpy.props.PointerProperty(type=RP_PG_scene_ui_settings)

def unregister():
    del bpy.types.Scene.rig_picker
    del bpy.types.Collection.rig_picker
    del bpy.types.Object.rig_picker
    del bpy.types.Armature.rig_picker

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

