import bpy

from ..properties.models import Outline


class ConfigBlendScene(bpy.types.Operator):
    """
    This operator init a news blend file, or update it.

    Prepare a set of collection (from the models in `properties/models/Outline`.

    Hierarchy design give that:
    - Placeholder
      | ${StaticMesh}
      | Socket (Optional)
    - Icon
    - Game Object
    """
    bl_idname = 'graou.build_scene'
    bl_label = 'Config or update the outline'

    def __init__(self):
        self._outline = Outline()

        # Main property
        self._settings = bpy.context.scene.graou_props
        self._socket = self._settings.socket_collection

    def execute(self, context):
        for key, value in self._outline.collections.items():
            collection = bpy.data.collections.get(key)

            # Make the collection if this item not exist
            if collection is None:
                collection = bpy.data.collections.new(value.name)
                context.scene.collection.children.link(collection)

            # Check if the color are correctly setup, if not update-it
            if collection.color_tag is not value.color:
                collection.color_tag = value.color

            # Check child collection color
            if collection.children:
                for child in collection.children:
                    if isinstance(child, bpy.types.Collection):
                        child.color_tag = value.color

        # Set or update socket collection
        if self._socket:
            self.set_socket_collection()
        else:
            self.del_socket_collection()

        return {'FINISHED'}

    def set_socket_collection(self):
        """Make or update the socket collection"""
        col_socket = bpy.data.collections.get(self._outline.socket.name)
        col_placeholder = self._outline.get_placeholder_collection
        col_game_object = self._outline.get_game_object_collection

        if col_socket is None:
            col_socket = bpy.data.collections.new(self._outline.socket.name)

        # Attach to Placeholder and Game Icon if they collection exist
        if col_placeholder.children.get(self._outline.socket.name) is None:
            col_placeholder.children.link(col_socket)

        if col_game_object.children.get(self._outline.socket.name) is None:
            col_game_object.children.link(col_socket)

        # Set the COLOR Tag
        col_socket.color_tag = self._outline.socket.color

    def del_socket_collection(self):
        """Remove the socket collection, however, if the collection doesn't exist, terminate the function"""
        socket = bpy.data.collections.get(self._outline.socket.name)
        if socket is not None:
            bpy.data.collections.remove(socket)


class SetCollectionSocket(bpy.types.Operator):
    """Configure a/the collection with socket param"""
    bl_idname = 'graou.collection.socket_setup'
    bl_label = 'Set param to be socket functional'

    def __init__(self):
        self.socket = bpy.data.collections.get('Socket')

    def execute(self, context):
        # TODO
        return {'FINISHED'}
