import bpy
import bmesh
import os
from ..utils import createTransform
from ..utils import isQuadMesh
from .. import utils
from ..cAPI import SimulationAPI
import numpy as np

class HI_PHY_OT_MeshToHair(bpy.types.Operator):
    bl_idname = "hi_phy.mesh_to_hair"
    bl_label = "Hi Phy Mesh to Hair"
    bl_options = {'REGISTER', 'UNDO_GROUPED'}

    hair_proxy : bpy.props.StringProperty(
        name='Hair Proxy'
    )

    def invoke(self, context, event):
        for obj in bpy.context.selected_objects:
            if self.hair_proxy == "":
                self.hair_proxy = obj.name
        return context.window_manager.invoke_props_dialog(self)

    def draw(self, context):
        layout = self.layout
        layout.prop(self, "hair_proxy")

    def execute(self, context):
        depsgraph = bpy.context.evaluated_depsgraph_get()
        # Guide is evaluated
        hair_proxy_orig = context.scene.objects[self.hair_proxy]
        hair_proxy = hair_proxy_orig.evaluated_get(depsgraph)
        if (hair_proxy.type != 'MESH'):
            self.report({"ERROR"}, "Hair proxy is not a MESH object")
            return {'CANCELLED'}
        if (not utils.isQuadMesh(hair_proxy)):
            self.report({"ERROR"}, "Hair proxy is not a Quad Mesh")
            return {'CANCELLED'}
        hair_proxy_mesh = SimulationAPI.HairProxyMesh()
        return {'FINISHED'}

__CLASSES__ = [
    HI_PHY_OT_MeshToHair
]

def menu_draw(self, context):
    # TODO: only add to the menu if the edited object is a valid simulation object
    layout = self.layout
    layout.operator_context = 'INVOKE_DEFAULT'
    layout.operator("hi_phy.mesh_to_hair")

def register():
    for class_ in __CLASSES__:
        bpy.utils.register_class(class_)


def unregister():
    for class_ in reversed(__CLASSES__):
        bpy.utils.unregister_class(class_)
