# -*- coding: utf-8 -*-

from os import pipe
import ramses as ram
import dumaf as maf
import maya.cmds as cmds # pylint: disable=import-error

def setExportMetaData( filePath, pipeType, publishFileInfo ):
    pipeType = pipeType.split('-')[-1]
    ram.RamMetaDataManager.setPipeType( filePath, pipeType )
    ram.RamMetaDataManager.setVersion( filePath, publishFileInfo.version )
    ram.RamMetaDataManager.setState( filePath, publishFileInfo.state )
    ram.RamMetaDataManager.setResource( filePath, publishFileInfo.resource )

def getPublishFilePath( publishFileInfo, extension, pipeName='Published' ):
    sceneInfo = publishFileInfo.copy()
    sceneInfo.version = -1
    sceneInfo.state = ''
    sceneInfo.extension = extension
    if sceneInfo.resource != '': sceneInfo.resource = sceneInfo.resource + '-'
    sceneInfo.resource = sceneInfo.resource + pipeName

    return sceneInfo.filePath()

def publishNodesAsMayaScene( publishFileInfo, nodes, pipeName='Published', extension='mb' ):

    if len(nodes) ==0:
        return ""

    filePath = getPublishFilePath( publishFileInfo, extension, pipeName )

    # Clean scene:
    # Remove empty groups from the scene
    maf.Node.removeEmptyGroups()

    # Select the nodes to publish
    maf.Node.select(nodes)
    # Save file
    selectedNodes = cmds.ls( long=True, selection=True )
    if len(selectedNodes) > 0:
        mayaType = 'mayaBinary'
        if extension == 'ma':
            mayaType = 'mayaAscii'
        print(mayaType)
        cmds.file( rename=filePath )
        cmds.file( exportSelected=True, options="v=1;", typ=mayaType )
        cmds.select(cl=True)
    else:
        ram.log("Some nodes weren't publish: can't select them")
        return ""

    # Set metadata
    setExportMetaData( filePath, pipeName, publishFileInfo )

    return filePath

def publishNodeAsABC( publishFileInfo, node, pipeName, timeRange=(1,1), frameStep=1, filterEuler=False ):

    if maf.Plugin.load("AbcExport"):
        ram.log("I have loaded the Alembic Export plugin, needed for the current task.")

    nodeName = maf.Path.baseName( node )
    nodeName = nodeName[0:nodeName.index('_root_')]
    filePath = getPublishFilePath( publishFileInfo, 'abc', nodeName + '-' + pipeName )

    eulerFilter = ''
    if filterEuler: eulerFilter = '-eulerFilter'

    # Build the ABC command
    abcOptions = ' '.join([
        '-frameRange ' + str(timeRange[0]) + ' ' + str(timeRange[1]),
        eulerFilter,
        '-step ' + str(frameStep),
        '-autoSubd', # crease
        '-uvWrite',
        '-writeUVSets',
        '-worldSpace',
        '-writeVisibility',
        '-dataFormat hdf',
        '-renderableOnly',
        '-root ' + node,
        '-file "' + filePath + '"',
    ])

    ram.log("These are the alembic options:\n" + abcOptions, ram.LogLevel.Debug)

    # Export
    cmds.AbcExport(j=abcOptions)

    # Update Ramses Metadata (version)
    setExportMetaData( filePath, pipeName, publishFileInfo )

    return filePath
