import bpy
from bpy.types import UIList
#import collections
#import inspect
from .core.addon_utils import get_operator_from_id, get_picker_collection
from .core.bl_utils import get_mat

import re


# class RP_UL_picker_source(UIList):
#     def draw_item(self, context, layout, data, item, _icon, _active_data, _active_propname, _index):
#         ob = context.object
#         row = layout.row(align=True)
#         is_packed = ('picker' in ob.data.rig_picker.keys())
#         sub_row = row.row(align=True)
#         sub_row.prop(item, 'source', text='')
#         sub_row.enabled = not is_packed
#         #row.operator('rigpicker.pack_picker', icon='PACKAGE' if is_packed else 'UGLYPACKAGE', text='')


class RP_PT_picker_maker_panel(bpy.types.Panel):
    bl_label = 'Rig Picker'
    bl_category = 'Rigging'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'

    @classmethod
    def poll(cls, context):
        return context.object

    def draw_header_preset(self, context):
        self.layout.operator('rigpicker.add_picker_collection', text="", icon='ADD', emboss=False)

    def draw(self, context):
        ob = context.object
        scn = context.scene
        collection = get_picker_collection(ob)

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

        if collection:
            col.prop_search(collection.rig_picker, 'rig', scn, 'objects', text='Rig ')
            col.prop_search(collection.rig_picker, 'canvas', scn, 'objects', text='Canvas ')
            col.prop_search(collection.rig_picker, 'symmetry', scn, 'objects', text='Symmetry ')

        if ob.type == 'ARMATURE':
            box = col.box()
            box.enabled = not ob.data.library
            col = box.column(align=False)

            row = col.row(align=True)
            row.separator(factor=0.5)
            row.label(text="Sources")

            is_packed = ('pickers' in ob.data.rig_picker.keys())
            row.operator('rigpicker.pack_picker', icon='PACKAGE' if is_packed else 'UGLYPACKAGE', text='', emboss=False)
            row.operator("rigpicker.add_picker_source", icon ='ADD', text="", emboss=False)

            for i, item in enumerate(ob.data.rig_picker.sources):
                row = col.row(align=True)
                row.enabled = not is_packed
                row.prop(item, 'source', text='')
                row.operator("rigpicker.remove_picker_source", icon ='PANEL_CLOSE', text="", emboss=False).index=i

        if collection:
            #layout.separator()
            layout.prop(collection.rig_picker, 'destination', text='Filepath')
            layout.operator('rigpicker.save_picker', icon='PASTEDOWN', text='Save Picker')


class RP_PT_shape(bpy.types.Panel):
    bl_label = 'Shape'
    bl_category = 'Rigging'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_parent_id = "RP_PT_picker_maker_panel"

    def draw(self, context):
        ob = context.object
        scn = context.scene
        collection = get_picker_collection(ob)

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

        if collection:
            #if context.collection and context.collection.rig_picker.enabled:
            col = layout.column(align=True)
            row = col.row(align=True)
            row.operator('rigpicker.create_shape', icon='MESH_DATA', text='Create Shape')
            row.operator('rigpicker.mirror_shape', icon='MOD_MIRROR', text='Mirror Shape')
            col.prop(scn.rig_picker, 'use_pick_bone', icon='EYEDROPPER', text='Auto Bone Assign')
            col.operator('rigpicker.name_from_bone', icon='SORTALPHA' , text='Name From Bones')
        else:
            col = layout.column(align=True)
            row = col.row(align=True)
            row.operator('rigpicker.create_shape', icon='MESH_DATA', text='Create Shape')

        if ob.type != 'ARMATURE':
            box = layout.box()
            col = box.column(align=False)

            material_row = col.row(align=True)
            material_row.operator('rigpicker.remove_mat', icon='REMOVE', text='')
            material_row.operator('rigpicker.add_mat', icon='ADD', text='')
            mat = False
            if ob.type in ('MESH', 'CURVE', 'FONT') and ob.data.materials:
                mat = get_mat(ob)
                if mat and mat.node_tree:
                    emission_nodes = [n for n in mat.node_tree.nodes if n.type =='EMISSION']
                    if emission_nodes:
                        material_row.prop(emission_nodes[0].inputs[0], 'default_value', text='')
                        mat = True
            if not mat:
                material_row.label(text='No Material')
            material_row.operator('rigpicker.eyedropper_mat', icon='EYEDROPPER', text='')

            shape_type_row = col.row(align=True)
            shape_type_row.prop(ob.rig_picker,'shape_type',expand = True)
            shape_type_row.operator('rigpicker.select_shape_type', text='', icon='RESTRICT_SELECT_OFF')


            if ob.rig_picker.shape_type == 'OPERATOR':
                
                op_row = col.row(align=True)
                op_row.prop(ob.rig_picker, 'operator', text='')
                op_row.operator('rigpicker.operator_selector', text='', icon='COLLAPSEMENU')

                if ob.rig_picker.operator:
                    col.prop(ob.rig_picker, 'name', text='Tooltip')
                    col.prop(ob.rig_picker,'shortcut', text='Shortcut')

                else:
                    col.prop(ob.rig_picker, 'name', text='Tooltip')

            elif ob.rig_picker.shape_type == 'BONE':
                if collection and collection.rig_picker.rig:
                    col.prop_search(ob.rig_picker, 'name', collection.rig_picker.rig.pose, 'bones', text='Bone')

classes = (
    #RP_UL_picker_source,
    RP_PT_picker_maker_panel,
    RP_PT_shape
)

register, unregister = bpy.utils.register_classes_factory(classes)