from __future__ import print_function

class playInRV(KellerNukePlugin):
    def configurePlugin(self):
        menubar = nuke.menu('Nuke')
        self.m = menubar.addMenu('Workgroup')
        self.m.addCommand('Play in RV', 'playInRV.playInRV()')
        self.m.addCommand('Play in RV (append)', 'playInRV.playInRV(append=true)')

    def unconfigurePlugin(self):
        self.m.removeItem('Play in RV')
        self.m.removeItem('Play in RV (append)')


def playInRV(file, mode):
    """
    loads the file specified in the write node to a new or existing RV session
    rvlink:// -l -play /path/to/my/movie.mov
    attaches a tag to the rv instance which is used to append sources to the session
    TODO: Linux / MacOS Support
    TODO: Better logging
    """
    import os
    import subprocess
    import random
    import nuke

    my_env = os.environ.copy()

    try:
        rvpush = my_env['RV'] + 'rvpush'
        print(('playInRV: Info. Using RV: %s') % rvpush)
    except:
        print('playInRV: Error. No env variable RV found. Exiting.')
        return

    try:
        job = my_env['JOB']
        print(('playInRV: Info. Using JOB: %s') % job)
    except:
        print('playInRV: Warning. No env variable job found.')
        my_env['JOB'] = 'GEN'

    try:
        dev = my_env['DEV']
        print(('playInRV: Info. Using dev: %s') % dev)
    except:
        print('playInRV: Warning. No env variable dev found. Setting pub.')
        my_env['DEV'] = "0"

    # TODO: check 000_dev/000_env vs dev/pub
    # check with default project without dev/pub structure
    if my_env['JOB'] == 'GEN':
        pass
    if my_env['DEV'] == "0":
        my_env['RV_SUPPORT_PATH'] = job.replace('"', '').replace('\\', '/') + '/000_env/rv/'
    else:
        my_env['RV_SUPPORT_PATH'] = job.replace('"', '').replace('\\', '/') + '/000_dev/rv/'
    print(('playInRV: Info. Using RV Workgroup: %s') % my_env['RV_SUPPORT_PATH'])

    if nuke.root()['colorManagement'].value().lower() != 'ocio':
        pass
    else:
        if nuke.root()['OCIO_config'].value() == 'custom':
            if nuke.root()['customOCIOConfigPath'].value() != "":
                my_env['OCIO'] = nuke.root()['customOCIOConfigPath'].value()
        else:
            my_env['OCIO'] = 'c:/Program Files/Nuke' + nuke.NUKE_VERSION_STRING + '/plugins/OCIOConfigs/configs/' + \
                             nuke.root()['OCIO_config'].value() + '/config.ocio'

        print(('playInRV: Info. Using OCIO config %s') % my_env['OCIO'])

    # check modes
    if mode == 0:
        # new RV
        # get a random string for a random rv id
        rnd = random.choice('abcdefghijklmnopqrstuvwxyz')
        subprocess.call([rvpush, '-tag', rnd, 'set', file], env=my_env)
        pass
    elif mode == 1:
        # sets / replaces the source of the connected rv
        subprocess.call([rvpush, '-tag', 'nukeKeller', 'set', file], env=my_env)
        pass
    elif mode == 2:
        # appends to the connected rv
        subprocess.call([rvpush, '-tag', 'nukeKeller', 'merge', file], env=my_env)
        pass
    return


# test in nuke
#file = nuke.toNode('Write1').knob('file').getValue()
#playInRV(file, 1)