# Path: analyzer/utils.py
# def env(name, default=None):
# def window(seq, n=2):
# def block_shaped(image, n_rows, n_cols):
# def flatten(l):
# def camel_to_underscore(name):
# def underscore_to_camel(name):
# def change_dict_naming_convention(d, convert_function):
# 	def as_dict(self, camel=True):
# 	def from_dict(cls, data, from_camel=True):
# 	def from_dicts(cls, data, from_camel=True):
# 	def to_mongo_dict(self):
# def to_mongo_dict(obj):
# def to_dict(obj):
# def props(obj):
# def image_filename(index, file_type="jpg"):
# def extract_filename(path):
# def extract_name(file):
# def basepath(path):
# def extract_index(path):
# def objects_as_dict(objects):
# def slice_paths(paths, limit):
# def derivative(distances):
# def crop_image(image, cx=20, cy=20):
# DEBUG = bool(int(getenv('DEBUG', False)))
# 	T = type(objects[0])
# class Model(object):
# 
# Path: analyzer/project.py
# class Project(Model):
# 	keyframe_montage_size = None
# 	keyframe_size = None
# 
# 	def __init__(self, title):
# 		self.name = title
# 
# 		identifier = re.sub('[^a-zA-Z0-9-_*.]', ' ', title.lower())
# 		self.identifier = identifier.replace(" ", "_")
# 
# 		local_base_path = StoragePath.local.base_path(self.identifier)
# 		create_directory(local_base_path)
# 
# 	class Folder(Enum):
# 		frames = 1
# 		keyframes = 2
# 		keyframe_thumbnails = 3
# 		spatio = 4
# 		plots = 5
# 
# 		def __str__(self):
# 			return {
# 				Project.Folder.frames: "frames",
# 				Project.Folder.keyframes: "keyframes",
# 				Project.Folder.keyframe_thumbnails: "keyframe_thumbnails",
# 				Project.Folder.spatio: "spatio_temporal_slices",
# 				Project.Folder.plots: "plots",
# 			}[self]
# 
# 	class File(Enum):
# 		shots = 1
# 		chapters = 2
# 		subtitles = 3
# 		merged_subtitles = 4
# 		original_subtitles = 5
# 		script = 6
# 		keyframe_montage = 7
# 		shot_change_ratio = 8
# 
# 		def __str__(self):
# 			return {
# 				Project.File.shots: "shots.json",
# 				Project.File.chapters: "chapters.json",
# 				Project.File.subtitles: "subtitles.json",
# 				Project.File.original_subtitles: "subtitles.srt",
# 				Project.File.merged_subtitles: "merged_subtitles.json",
# 				Project.File.script: "script.json",
# 				Project.File.keyframe_montage: "keyframe_montage.jpg",
# 				Project.File.shot_change_ratio: "shot_change_ratio.json",
# 			}[self]
# 
# 	def setup(self):
# 		for folder_type in Project.Folder:
# 			self.folder_path(folder_type)
# 
# 	@staticmethod
# 	def file_exists(path):
# 		return os.path.exists(path)
# 
# 	def folder_path(self, folder_type, destination=None, storage_env=StoragePath.local):
# 		assert folder_type is not None
# 
# 		folder = str(folder_type) if type(folder_type) == Project.Folder else folder_type
# 
# 		if destination:
# 			return self.__folder_path(destination, storage_env)
# 		else:
# 			base_path = storage_env.base_path(self.identifier)
# 			default_path = os.path.join(base_path, folder)
# 			return self.__folder_path(default_path, storage_env)
# 
# 	def file_path(self, file_type, destination=None, storage_env=StoragePath.local):
# 		return self.__file_path(str(file_type), destination, storage_env)
# 
# 	def write(self, data, file_type, destination=None):
# 		destination = self.__file_path(str(file_type), destination)
# 		write_json(destination, data)
# 
# 	def read(self, file_type):
# 		path = self.__file_path(str(file_type))
# 		with open(path) as data:
# 			return json.load(data)
# 
# 	def __folder_path(self, folder, storage_env=StoragePath.local):
# 		if storage_env == StoragePath.local:
# 			create_directory(folder)
# 
# 		return folder
# 
# 	def __file_path(self, filename, full_path=None, storage_env=StoragePath.local):
# 		if full_path is None:
# 			base_path = storage_env.base_path(self.identifier)
# 			return os.path.join(base_path, filename)
# 
# 		if os.path.isdir(full_path):
# 			full_path = os.path.join(full_path, filename)
# 
# 		dir = os.path.dirname(full_path)
# 		create_directory(dir)
# 		return full_path
# 
# Path: analyzer/shot_detection.py
# class Label(Model):
# 	def __init__(self, description, score):
# 		self.description = description
# 		self.score = score
# 
# 	@classmethod
# 	def from_dict(cls, data, from_camel=True):
# 		d = Model.from_dict(data, from_camel)
# 		return cls(d.get("description"), d.get("score"))
# 
# 	def __str__(self):
# 		return "{:<12} => {}".format(self.description, self.score)
# 
# 	def __repr__(self):
# 		return self.__str__()


import base64
from os import path
from tqdm import tqdm
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from analyzer import utils
from analyzer.project import Project
from analyzer.shot_detection import Label



DISCOVERY_URL = 'https://{api}.googleapis.com/$discovery/rest?version={apiVersion}'


def run(project, shots):
	def shot_to_keyframe_path(shot):
		index = shot.keyframe.index
		filename = utils.image_filename(index)

# Path: analyzer/utils.py
# def env(name, default=None):
# def window(seq, n=2):
# def block_shaped(image, n_rows, n_cols):
# def flatten(l):
# def camel_to_underscore(name):
# def underscore_to_camel(name):
# def change_dict_naming_convention(d, convert_function):
# 	def as_dict(self, camel=True):
# 	def from_dict(cls, data, from_camel=True):
# 	def from_dicts(cls, data, from_camel=True):
# 	def to_mongo_dict(self):
# def to_mongo_dict(obj):
# def to_dict(obj):
# def props(obj):
# def image_filename(index, file_type="jpg"):
# def extract_filename(path):
# def extract_name(file):
# def basepath(path):
# def extract_index(path):
# def objects_as_dict(objects):
# def slice_paths(paths, limit):
# def derivative(distances):
# def crop_image(image, cx=20, cy=20):
# DEBUG = bool(int(getenv('DEBUG', False)))
# 	T = type(objects[0])
# class Model(object):
# 
# Path: analyzer/project.py
# class Project(Model):
# 	keyframe_montage_size = None
# 	keyframe_size = None
# 
# 	def __init__(self, title):
# 		self.name = title
# 
# 		identifier = re.sub('[^a-zA-Z0-9-_*.]', ' ', title.lower())
# 		self.identifier = identifier.replace(" ", "_")
# 
# 		local_base_path = StoragePath.local.base_path(self.identifier)
# 		create_directory(local_base_path)
# 
# 	class Folder(Enum):
# 		frames = 1
# 		keyframes = 2
# 		keyframe_thumbnails = 3
# 		spatio = 4
# 		plots = 5
# 
# 		def __str__(self):
# 			return {
# 				Project.Folder.frames: "frames",
# 				Project.Folder.keyframes: "keyframes",
# 				Project.Folder.keyframe_thumbnails: "keyframe_thumbnails",
# 				Project.Folder.spatio: "spatio_temporal_slices",
# 				Project.Folder.plots: "plots",
# 			}[self]
# 
# 	class File(Enum):
# 		shots = 1
# 		chapters = 2
# 		subtitles = 3
# 		merged_subtitles = 4
# 		original_subtitles = 5
# 		script = 6
# 		keyframe_montage = 7
# 		shot_change_ratio = 8
# 
# 		def __str__(self):
# 			return {
# 				Project.File.shots: "shots.json",
# 				Project.File.chapters: "chapters.json",
# 				Project.File.subtitles: "subtitles.json",
# 				Project.File.original_subtitles: "subtitles.srt",
# 				Project.File.merged_subtitles: "merged_subtitles.json",
# 				Project.File.script: "script.json",
# 				Project.File.keyframe_montage: "keyframe_montage.jpg",
# 				Project.File.shot_change_ratio: "shot_change_ratio.json",
# 			}[self]
# 
# 	def setup(self):
# 		for folder_type in Project.Folder:
# 			self.folder_path(folder_type)
# 
# 	@staticmethod
# 	def file_exists(path):
# 		return os.path.exists(path)
# 
# 	def folder_path(self, folder_type, destination=None, storage_env=StoragePath.local):
# 		assert folder_type is not None
# 
# 		folder = str(folder_type) if type(folder_type) == Project.Folder else folder_type
# 
# 		if destination:
# 			return self.__folder_path(destination, storage_env)
# 		else:
# 			base_path = storage_env.base_path(self.identifier)
# 			default_path = os.path.join(base_path, folder)
# 			return self.__folder_path(default_path, storage_env)
# 
# 	def file_path(self, file_type, destination=None, storage_env=StoragePath.local):
# 		return self.__file_path(str(file_type), destination, storage_env)
# 
# 	def write(self, data, file_type, destination=None):
# 		destination = self.__file_path(str(file_type), destination)
# 		write_json(destination, data)
# 
# 	def read(self, file_type):
# 		path = self.__file_path(str(file_type))
# 		with open(path) as data:
# 			return json.load(data)
# 
# 	def __folder_path(self, folder, storage_env=StoragePath.local):
# 		if storage_env == StoragePath.local:
# 			create_directory(folder)
# 
# 		return folder
# 
# 	def __file_path(self, filename, full_path=None, storage_env=StoragePath.local):
# 		if full_path is None:
# 			base_path = storage_env.base_path(self.identifier)
# 			return os.path.join(base_path, filename)
# 
# 		if os.path.isdir(full_path):
# 			full_path = os.path.join(full_path, filename)
# 
# 		dir = os.path.dirname(full_path)
# 		create_directory(dir)
# 		return full_path
# 
# Path: analyzer/shot_detection.py
# class Label(Model):
# 	def __init__(self, description, score):
# 		self.description = description
# 		self.score = score
# 
# 	@classmethod
# 	def from_dict(cls, data, from_camel=True):
# 		d = Model.from_dict(data, from_camel)
# 		return cls(d.get("description"), d.get("score"))
# 
# 	def __str__(self):
# 		return "{:<12} => {}".format(self.description, self.score)
# 
# 	def __repr__(self):
# 		return self.__str__()


# Path: analyzer/label_detection.py
import base64
from os import path
from tqdm import tqdm
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from analyzer import utils
from analyzer.project import Project
from analyzer.shot_detection import Label




DISCOVERY_URL = 'https://{api}.googleapis.com/$discovery/rest?version={apiVersion}'


def run(project, shots):
	def shot_to_keyframe_path(shot):
		index = shot.keyframe.index
		filename = utils.image_filename(index)

		return path.join(project.folder_path(Project.Folder.keyframes), filename)