import bpy
from ...utils_debug import debug_log, debug_warn
from .animator_conditions import Condition

class AnimatorTransition(bpy.types.PropertyGroup):

    #############################
    # Serialization

    def serialize(self, serializer, animatorStateNode):
        dest = animatorStateNode.getDestinationStateIndex(self.transitionId)
        if dest is None:
            return None
        obj = {
            "isExit" : False, # not needed until we have layer support
            "hasExitTime" : self.hasExitTime,
            "exitTime" : self.exitTime,
            "duration" : self.transitionDuration,
            "offset" : self.transitionOffset,
            "destinationState" : dest,
            "conditions" : self.serializeConditions(serializer)
        }
        return obj

    def serializeConditions(self, serializer):
        arr = []
        for condition in self.conditions:
            res = condition.serialize(serializer)
            if res:
                arr.append(res)
            else:
                debug_warn("Failed to serialize condition", self.name, "ENUM: " + condition.parameterEnum)
        return arr


    ############################

    def nameChanged(self, context):
        activeNode = context.active_node
        if activeNode and self:
            activeNode.onTransitionNameChanged(self.transitionId, self.name)

    transitionId: bpy.props.StringProperty()
    name: bpy.props.StringProperty(name="Name", default="Transition", update=nameChanged)
    hasExitTime: bpy.props.BoolProperty(name="Has Exit Time", default=True)
    exitTime: bpy.props.FloatProperty(name="Exit Time", default=1, min=0, max=1, description="Exit Time")
    transitionDuration: bpy.props.FloatProperty(name="Transition Duration", default=0, min=0, max=1, description="Transition Duration")
    transitionOffset: bpy.props.FloatProperty(name="Transition Offset", default=0, min=0, max=1, description="Transition Offset")

    conditions: bpy.props.CollectionProperty(type=Condition)
    conditions_index: bpy.props.IntProperty(default=-1)
    
    def draw(self, context, layout):
        row = layout.row()
        row.prop(self, "name")
        row.operator("needle_animator.remove_transition", text="", icon='X').transitionId = self.transitionId
        layout.prop(self, "hasExitTime")
        if self.hasExitTime:
            layout.prop(self, "exitTime")
        layout.prop(self, "transitionDuration")
        layout.prop(self, "transitionOffset")

        # row = layout.row()
        # row.enabled = False
        # row.label(text=self.transitionId)

        conditionsRow = layout.row()
        conditionsRow.label(text="Conditions")
        add_condition_op = conditionsRow.operator("needle_animator.add_condition", text="Add Condition", icon='ADD')
        add_condition_op.transitionId = self.transitionId
        if len(self.conditions) > 0:
            layout.template_list("NEEDLE_UL_AnimatorParametersList", "", self, "conditions", self, "conditions_index", rows=1)

    def getCondition(self, id):
        for condition in self.conditions:
            if condition.conditionId == id:
                return condition
        return None

    def removeCondition(self, id):
        for i in range(len(self.conditions)):
            condition = self.conditions[i]
            if condition.conditionId == id:
                self.conditions.remove(i)
                return

class NEEDLE_UL_AnimatorTransitionList(bpy.types.UIList):
    def draw_item(self, context, layout, data, item, icon, active_data, active_propname):
        layout = layout.box()
        item.draw(context, layout)
            


def register():
    bpy.utils.register_class(AnimatorTransition)
    bpy.utils.register_class(NEEDLE_UL_AnimatorTransitionList)

def unregister():
    bpy.utils.unregister_class(AnimatorTransition)
    bpy.utils.unregister_class(NEEDLE_UL_AnimatorTransitionList)
