import bpy
from . import utils
from ..cAPI import SimulationAPI
from .. import utils as ut

class HI_PHY_PT_CurvesMotionMapper(bpy.types.Panel):
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "physics"
    bl_label = "Hi Phy: Curves Motion Mapper"

    @classmethod
    def poll(cls, context):
        hi_phy = context.object.hi_phy
        return hi_phy.is_active and hi_phy.object_type == 'CURVES_MOTION_MAPPER'

    def draw(self, context):
        obj = context.object
        hi_phy = obj.hi_phy
        lay = self.layout
        utils.draw_prop(lay, hi_phy, 'guide_shape', 'Guide Shape', expand=False, use_column=True)
        utils.draw_prop(lay, hi_phy, 'driven_shape', 'Driven Shape', expand=False, use_column=True)
        utils.draw_prop(lay, hi_phy, 'motion_mapper_use_id', 'Use ID', expand=False, use_column=True)
        if (hi_phy.motion_mapper_use_id):
            utils.draw_prop(lay, hi_phy, 'motion_mapper_id_attribute', 'ID Attribute', expand=False, use_column=True)
        utils.draw_prop(lay, hi_phy, 'motion_mapper_use_frame', 'Use Frame', expand=False, use_column=True)
        if (hi_phy.motion_mapper_use_frame):
            utils.draw_prop(lay, hi_phy, 'motion_mapper_frame_mode', 'Frame Mode', expand=False, use_column=True)
            if (hi_phy.motion_mapper_frame_mode == 'FRAME_NORMAL'):
                utils.draw_prop(lay, hi_phy, 'motion_mapper_frame_attribute', 'Frame Normal Attribute', expand=False, use_column=True)
            elif (hi_phy.motion_mapper_frame_mode == 'BASE_NORMAL_TWIST'):
                utils.draw_prop(lay, hi_phy, 'motion_mapper_base_normal_attribute', 'Base Normal', expand=False, use_column=True)
                utils.draw_prop(lay, hi_phy, 'motion_mapper_twist_attribute', 'Twist', expand=False, use_column=True)

        utils.draw_prop(lay, hi_phy, 'motion_mapper_binding_radius', 'Binding Radius', expand=False, use_column=True)
        utils.draw_prop(lay, hi_phy, 'motion_mapper_number_of_guides', 'Number of Guides', expand=False, use_column=True)
        utils.draw_prop(lay, hi_phy, 'motion_mapper_live_update', 'Live Update', expand=False, use_column=True)
        utils.draw_prop(lay, hi_phy, 'motion_mapper_dynamic_parting', 'Dynamic Parting', expand=False, use_column=True)
        if (hi_phy.motion_mapper_dynamic_parting):
            utils.draw_prop(lay, hi_phy, 'motion_mapper_dynamic_parting_power', 'Dynamic Parting Power', expand=False, use_column=True)

        row = lay.row(align=True)

        if (obj.name in ut.curves_motion_mappers and ut.curves_motion_mappers[obj.name].IsBond()):
            row.label(text="The motion mapper has been bond.")

        row = lay.row(align=True)
        operator = row.operator('hi_phy.motion_mapper_curves_binding', text='Binding')
        operator.motion_mapper = obj.name
        if (hi_phy.guide_shape):
            operator.guide = hi_phy.guide_shape.name
        if (hi_phy.driven_shape):
            operator.driven = hi_phy.driven_shape.name
        operator.use_id = hi_phy.motion_mapper_use_id
        operator.use_frame = hi_phy.motion_mapper_use_frame
        operator.id_attribute = hi_phy.motion_mapper_id_attribute
        operator.frame_attribute = hi_phy.motion_mapper_frame_attribute
        operator.base_normal_attribute = hi_phy.motion_mapper_base_normal_attribute
        operator.twist_attribute = hi_phy.motion_mapper_twist_attribute
        operator.number_of_guides = hi_phy.motion_mapper_number_of_guides
        operator.binding_radius = hi_phy.motion_mapper_binding_radius
        if (hi_phy.motion_mapper_frame_mode == 'FRAME_NORMAL'):
            operator.use_base_normal_twist = False
        elif (hi_phy.motion_mapper_frame_mode == 'BASE_NORMAL_TWIST'):
            operator.use_base_normal_twist = True
