from unreal import EditorLevelLibrary as ell
from pype.hosts.unreal.plugin import Creator
from avalon.unreal import (
    instantiate,
)


class CreateLayout(Creator):
    """Layout output for character rigs"""

    name = "layoutMain"
    label = "Layout"
    family = "layout"
    icon = "cubes"

    root = "/Game"
    suffix = "_INS"

    def __init__(self, *args, **kwargs):
        super(CreateLayout, self).__init__(*args, **kwargs)

    def process(self):
        data = self.data

        name = data["subset"]

        selection = []
        # if (self.options or {}).get("useSelection"):
        #     sel_objects = unreal.EditorUtilityLibrary.get_selected_assets()
        #     selection = [a.get_path_name() for a in sel_objects]

        data["level"] = ell.get_editor_world().get_path_name()

        data["members"] = []

        if (self.options or {}).get("useSelection"):
            # Set as members the selected actors
            for actor in ell.get_selected_level_actors():
                data["members"].append("{}.{}".format(
                    actor.get_outer().get_name(), actor.get_name()))

        instantiate(self.root, name, data, selection, self.suffix)
