from .extensionbase import ExtensionBase
import bpy

def getDefaultLayerName(layerIndex: int) -> str:
    if layerIndex == 0:
        return "Default"
    if layerIndex == 2:
        return "IgnoreRaycast"

class GameObjectDataPropertyGroup(bpy.types.PropertyGroup):
    # the guid is directly on the object
    # guid: bpy.props.StringProperty(name="guid", default="")
    tag: bpy.props.StringProperty(name="tag", default="")
    layer: bpy.props.IntProperty(name="layer", default=0, min=0, max=31)
    isMainCamera: bpy.props.BoolProperty(name="isMainCamera", default=False)


def register():
    unregister()
    bpy.utils.register_class(GameObjectDataPropertyGroup)
    bpy.types.Object.NEEDLE_gameobject_data = bpy.props.PointerProperty(type=GameObjectDataPropertyGroup)
    pass

def unregister():
    try:
        bpy.utils.unregister_class(GameObjectDataPropertyGroup)
    except:
        pass

def guidKey():
    return "NEEDLE_guid"



class NEEDLE_gameobject_data(ExtensionBase):
    def __init__(self, ext):
        self.ext = ext
        pass

    # EXPORT
    def gather_node_hook(self, gltf2_node, blender_object, node):

        gameObjectData = {}

        # write the guid
        guid = getattr(blender_object, guidKey(), None)
        if guid is None or len(guid) <= 0:
            guid = str(id(blender_object))
        gameObjectData["guid"] = guid
        
        # write visibility if its not true
        isVisible = blender_object.visible_get()
        isVisibleInRender = blender_object.hide_render == False
        if isVisibleInRender == False:
            gameObjectData["activeSelf"] = False

        # write tag if its not empty
        tag = blender_object.NEEDLE_gameobject_data.tag
        if tag is not None and len(tag) > 0:
            gameObjectData["tag"] = tag

        if blender_object.NEEDLE_gameobject_data.isMainCamera:
            gameObjectData["tag"] = "MainCamera"
        
        # write layer if its not 0
        layer = blender_object.NEEDLE_gameobject_data.layer
        if layer != 0:
            gameObjectData["layers"] = layer

        gltf2_node.extensions["NEEDLE_gameobject_data"] = self.ext.Extension(
            name="NEEDLE_gameobject_data",
            extension=gameObjectData,
            required=False
        )


    # IMPORT
    def gather_import_node_after_hook(self, vnode, gltf_node, blender_object, gltf):
        if not gltf_node.extensions:
            return
        if "NEEDLE_gameobject_data" in gltf_node.extensions:
            gameObjectData = gltf_node.extensions["NEEDLE_gameobject_data"]
            if "guid" in gameObjectData:
                blender_object[guidKey()] = gameObjectData["guid"]
            if "activeSelf" in gameObjectData:
                blender_object.hide_set(not gameObjectData["activeSelf"])
            if "tag" in gameObjectData:
                blender_object.NEEDLE_gameobject_data.tag = gameObjectData["tag"]
            if "layer" in gameObjectData:
                blender_object.NEEDLE_gameobject_data.layer = gameObjectData["layer"]
