import os

import avalon.maya
import pype.api

from maya import cmds


class ExtractVRayProxy(pype.api.Extractor):
    """Extract the content of the instance to a vrmesh file

    Things to pay attention to:
        - If animation is toggled, are the frames correct
        -
    """

    label = "VRay Proxy (.vrmesh)"
    hosts = ["maya"]
    families = ["vrayproxy"]

    def process(self, instance):

        staging_dir = self.staging_dir(instance)
        file_name = "{}.vrmesh".format(instance.name)
        file_path = os.path.join(staging_dir, file_name)

        anim_on = instance.data["animation"]
        if not anim_on:
            # Remove animation information because it is not required for
            # non-animated subsets
            instance.data.pop("frameStart", None)
            instance.data.pop("frameEnd", None)

            start_frame = 1
            end_frame = 1
        else:
            start_frame = instance.data["frameStart"]
            end_frame = instance.data["frameEnd"]

        vertex_colors = instance.data.get("vertexColors", False)

        # Write out vrmesh file
        self.log.info("Writing: '%s'" % file_path)
        with avalon.maya.maintained_selection():
            cmds.select(instance.data["setMembers"], noExpand=True)
            cmds.vrayCreateProxy(exportType=1,
                                 dir=staging_dir,
                                 fname=file_name,
                                 animOn=anim_on,
                                 animType=3,
                                 startFrame=start_frame,
                                 endFrame=end_frame,
                                 vertexColorsOn=vertex_colors,
                                 ignoreHiddenObjects=True,
                                 createProxyNode=False)

        if "representations" not in instance.data:
            instance.data["representations"] = []

        representation = {
            'name': 'vrmesh',
            'ext': 'vrmesh',
            'files': file_name,
            "stagingDir": staging_dir,
        }
        instance.data["representations"].append(representation)

        self.log.info("Extracted instance '%s' to: %s"
                      % (instance.name, staging_dir))
