import bpy
from . import utils
from ..cAPI import SimulationAPI

class HI_PHY_PT_Constraint(bpy.types.Panel):
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "physics"
    bl_label = "Hi Phy: Constraint"

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

    def draw(self, context):
        obj = context.object
        hi_phy = obj.hi_phy
        lay = self.layout

        if (hi_phy.constraint_type == SimulationAPI.ConstraintType.BindingConstraint):
            utils.draw_prop(lay, hi_phy, 'constraint_live_update', 'Live Update', expand=False, use_column=True)
        if (hi_phy.constraint_type != SimulationAPI.ConstraintType.BindingConstraint and
            hi_phy.constraint_type != SimulationAPI.ConstraintType.RootConstraint and
            hi_phy.constraint_type != SimulationAPI.ConstraintType.GrabConstraint):
            utils.draw_prop(lay, hi_phy, 'constraintIsSoft', 'Is Soft', expand=False, use_column=True)

        utils.draw_prop(lay, hi_phy, 'constraintEnabled', 'Is Enabled', expand=False, use_column=True)
        # Root constraint doesn't have any parameter besides enabled
        if (hi_phy.constraint_type != SimulationAPI.ConstraintType.RootConstraint and hi_phy.constraint_type != SimulationAPI.ConstraintType.GrabConstraint):
            # they are zero rest length constraints, so no compression stiffness
            if (hi_phy.constraint_type == SimulationAPI.ConstraintType.MeshConstraint or hi_phy.constraint_type == SimulationAPI.ConstraintType.PointConstraint):
                utils.draw_dynamic_props(lay, hi_phy, SimulationAPI.Property.Tag.Constraint, expand=False, use_column=True, ignore_list=["constraintIsSoft", "constraintEnabled", "constraintCompressionStiffness", "constraintDisableCollision"])
            else:
                utils.draw_dynamic_props(lay, hi_phy, SimulationAPI.Property.Tag.Constraint, expand=False, use_column=True, ignore_list=["constraintIsSoft", "constraintEnabled", "constraintDisableCollision"])
        if (hi_phy.constraint_type == SimulationAPI.ConstraintType.BindingConstraint):
            utils.draw_prop(lay, hi_phy, 'constraintDisableCollision', 'Disable Collision Between Constrained Points', expand=False, use_column=True)
            utils.draw_prop(lay, hi_phy, 'constraint_object0', 'Constraint Object 1', expand=False, use_column=True)
            utils.draw_prop(lay, hi_phy, 'constraint_object1', 'Constraint Object 2', expand=False, use_column=True)
        elif (hi_phy.constraint_type == SimulationAPI.ConstraintType.MeshConstraint or hi_phy.constraint_type == SimulationAPI.ConstraintType.RootConstraint):
            utils.draw_prop(lay, hi_phy, 'constraint_object0', 'Constraint Object', expand=False, use_column=True)
            utils.draw_prop(lay, hi_phy, 'constraint_object1', 'Constraint Mesh', expand=False, use_column=True)
        elif (hi_phy.constraint_type == SimulationAPI.ConstraintType.GrabConstraint):
            utils.draw_prop(lay, hi_phy, 'constraintStretchStiffness', 'Stiffness', expand=False, use_column=True)
            utils.draw_prop(lay, hi_phy, 'constraintDamping', 'Damping', expand=False, use_column=True)
            utils.draw_prop(lay, hi_phy, 'grab_constraint_inner_radius', 'Inner Radius', expand=False, use_column=True)
            utils.draw_prop(lay, hi_phy, 'grab_constraint_outer_radius', 'Outer Radius', expand=False, use_column=True)
            utils.draw_prop(lay, hi_phy, 'constraintMaxStiffnessDistance', 'Max Stiffness Distance', expand=False, use_column=True)
            utils.draw_prop(lay, hi_phy, 'grab_constraint_apply_to_all', 'Grab All', expand=False, use_column=True)
            if (not hi_phy.grab_constraint_apply_to_all):
                row = lay.row(align=True)
                row.label(text="Applies to:")
                row = lay.row(align=True)
                row.template_list(
                    "HI_PHY_UL_ObjectListWithWeight",
                    "",
                    hi_phy, "grab_constraint_object_list",  # data, property
                    hi_phy, "grab_constraint_object_index",
                )
                col = row.column(align=True)
                op = col.operator("hi_phy.add_item_to_list", icon="ADD", text="")
                op.list_prop = "grab_constraint_object_list"
                op.index_prop = "grab_constraint_object_index"
                op = col.operator("hi_phy.remove_item_from_list", icon="REMOVE", text="")
                op.list_prop = "grab_constraint_object_list"
                op.index_prop = "grab_constraint_object_index"
            split = lay.split(factor=0.5, align=True)
            split.operator('hi_phy.grab_constraint_grab', text='Grab').grab_constraint = obj.name
            split.operator('hi_phy.grab_constraint_release', text='Release').grab_constraint = obj.name
        else:
            utils.draw_prop(lay, hi_phy, 'constraint_object0', 'Constraint Object', expand=False, use_column=True)
