#!/usr/bin/env python

"""pylut

Usage:
  pylut [-hf] [--reverse] [--visualize] [--resize=<size>] [--convert=<type>] [--rename=<name>] [--output=<output_folder>] <file>
  pylut --version

Options:
  -h --help                                     show this help message and exit
  --version                                     show version and exit
  --visualize                                   visualize the LUT
  --reverse                                     reverse the LUT
  -f, --force                                   overwrite files if necessary
  --resize <size>                               rescale lut
  --convert <convert>                           convert to a different LUT format (L3DL, N3DL, RCUBE)
  --rename <name>                               name to use for saved LUT
  --output <output_folder>                      set output folder [default: ./]

"""


from docopt import docopt
from pylut import *

import os

def LUTFromFile(lutFilePath):
  lut = None
  filetype = None

  if ".3dl" in os.path.splitext(lutFilePath)[1].lower():
    try:
      lut = LUT.FromLustre3DLFile(lutFilePath)
      return lut, "L3DL"
    except Exception as e:
      pass
    try:
      lut = LUT.FromNuke3DLFile(lutFilePath)
      return lut, "N3DL"
    except Exception as e:
      pass
  elif ".cube" in os.path.splitext(lutFilePath)[1].lower():
    try:
      lut = LUT.FromCubeFile(lutFilePath)
      return lut, "RCUBE"
    except Exception as e:
      pass

  return None, None

def FullFilePath(outputFolder, name, extension):
  return outputFolder + "/" + name + extension

if __name__ == "__main__":
  arguments = docopt(__doc__, version='1.0')

  filePath = os.path.expanduser(arguments["<file>"])
  outputFolder = os.path.abspath(os.path.expanduser(arguments["--output"]))
  name = arguments["--rename"]
  resizeSize = arguments["--resize"]
  convertType = arguments["--convert"]
  overwrite = arguments["--force"]
  visualize = arguments["--visualize"]
  reverse = arguments["--reverse"]

  if not os.path.isfile(filePath):
    raise NameError("Invalid file path.")



  if resizeSize is None and convertType is None and not visualize and not reverse:
    exit()

  if convertType is not None and convertType not in ("L3DL", "N3DL", "RCUBE"):
    raise NameError(str(convertType) + " is not a valid type to convert the LUT to.")
    
  lut, filetype = LUTFromFile(filePath)
  if reverse:
    print "Warning: reverse can take up to 9 minutes"
    lut = lut.Reverse(True)

  if visualize:
    lut.Plot()
    exit()

  if resizeSize is not None:
    lut = lut.Resize(int(resizeSize))

  if name is None:
    name = lut.name
  else:
    name = os.path.splitext(name)[0]

  if convertType is None:
    toType = filetype
  else:
    toType = convertType

  if toType in "L3DL":
    if os.path.isfile(FullFilePath(outputFolder, name, ".3dl")) and not overwrite:
      exit("File already exists!")
    
    lut.ToLustre3DLFile(FullFilePath(outputFolder, name, ".3dl"))
  elif toType in "N3DL":
    if os.path.isfile(FullFilePath(outputFolder, name, ".3dl")) and not overwrite:
      exit("File already exists!")
    
    lut.ToNuke3DLFile(FullFilePath(outputFolder, name, ".3dl"))
  elif toType in "RCUBE":
    if os.path.isfile(FullFilePath(outputFolder, name, ".cube")) and not overwrite:
      exit("File already exists!")
    
    lut.ToCubeFile(FullFilePath(outputFolder, name, ".cube"))







  