# Path: analyzer/shot_detection.py
# class Shot(Model):
# 	def __init__(self, start_index=None, end_index=None, id=None, relative_diff=None):
# 		if end_index:
# 			self.keyframe = Keyframe(start_index)
# 			self.end_index = end_index
# 			self.length = end_index - start_index
# 			self.duration = int(self.length / 25 * 1000)
# 		else:
# 			self.keyframe = None
# 			self.end_index = None
# 			self.duration = None
# 
# 		self.start_index = start_index
# 		self.id = id
# 		self.start_diff = relative_diff
# 
# 	def as_dict(self, camel=True):
# 		d = Model.as_dict(self)
# 		if self.keyframe.labels:
# 			d["keyframe"]["labels"] = [label.as_dict() for label in self.keyframe.labels]
# 
# 		return d
# 
# 	def to_mongo_dict(self):
# 		d = self.as_dict()
# 		return utils.to_mongo_dict(d)
# 
# 	@classmethod
# 	def from_dict(cls, data, from_camel=True):
# 		d = Model.from_dict(data, from_camel)
# 		shot = Shot(d.get("start_index"), d.get("end_index"), d.get("id"), d.get("relative_diff"))
# 
# 		if "labels" in d["keyframe"] and d["keyframe"]["labels"] is not None:
# 			shot.keyframe.labels = [Label.from_dict(l) for l in (d["keyframe"]["labels"])]
# 
# 		if "colors" in d["keyframe"] and d["keyframe"]["colors"] is not None:
# 			shot.keyframe.colors = [l for l in (d["keyframe"]["colors"])]
# 
# 		return shot
# 
# 	def __str__(self):
# 		return "id: {}, start_index: {} length: {}, end_index: {}" \
# 			.format(self.id, self.start_index, self.length, self.end_index)
# 
# 	def __repr__(self):
# 		return self.__str__()
# 
# Path: analyzer/utils.py
# def env(name, default=None):
# 	return getenv(name, default)


from pymongo import MongoClient
from analyzer.shot_detection import Shot
from analyzer.utils import env
		self.db_name = DB_NAME

		self.t_key = "titleId"

		self.client = None
		self.context = self.connect(self.host, self.user, self.passwd, self.db_name)

	def connect(self, host, user, passwd, db_name):
		uri = "mongodb://{}:{}@{}/{}?authMechanism=SCRAM-SHA-1".format(user, passwd, host, db_name)
		self.client = MongoClient(uri)
		db = self.client[db_name]

		self.context = db
		return db

	def find_shots(self, title):
		title_object = self.context.titles.find_one({"name": title})

		return list(self.context.shots.find({"titleId": title_object["_id"]}))

	def find_title(self, project):
		return self.context.titles.find_one({"identifier": project.identifier})

	def remove_shots(self, title):
		self.context.shots.delete_many({"titleId": title["_id"]})

	def cinemetrics(self, movie_id):
		movie = self.context.cinemetrics.find_one({"id": movie_id})

		def cinemetric_to_shot(metrics, index):

# Path: analyzer/shot_detection.py
# class Shot(Model):
# 	def __init__(self, start_index=None, end_index=None, id=None, relative_diff=None):
# 		if end_index:
# 			self.keyframe = Keyframe(start_index)
# 			self.end_index = end_index
# 			self.length = end_index - start_index
# 			self.duration = int(self.length / 25 * 1000)
# 		else:
# 			self.keyframe = None
# 			self.end_index = None
# 			self.duration = None
# 
# 		self.start_index = start_index
# 		self.id = id
# 		self.start_diff = relative_diff
# 
# 	def as_dict(self, camel=True):
# 		d = Model.as_dict(self)
# 		if self.keyframe.labels:
# 			d["keyframe"]["labels"] = [label.as_dict() for label in self.keyframe.labels]
# 
# 		return d
# 
# 	def to_mongo_dict(self):
# 		d = self.as_dict()
# 		return utils.to_mongo_dict(d)
# 
# 	@classmethod
# 	def from_dict(cls, data, from_camel=True):
# 		d = Model.from_dict(data, from_camel)
# 		shot = Shot(d.get("start_index"), d.get("end_index"), d.get("id"), d.get("relative_diff"))
# 
# 		if "labels" in d["keyframe"] and d["keyframe"]["labels"] is not None:
# 			shot.keyframe.labels = [Label.from_dict(l) for l in (d["keyframe"]["labels"])]
# 
# 		if "colors" in d["keyframe"] and d["keyframe"]["colors"] is not None:
# 			shot.keyframe.colors = [l for l in (d["keyframe"]["colors"])]
# 
# 		return shot
# 
# 	def __str__(self):
# 		return "id: {}, start_index: {} length: {}, end_index: {}" \
# 			.format(self.id, self.start_index, self.length, self.end_index)
# 
# 	def __repr__(self):
# 		return self.__str__()
# 
# Path: analyzer/utils.py
# def env(name, default=None):
# 	return getenv(name, default)


# Path: analyzer/database.py
from pymongo import MongoClient
from analyzer.shot_detection import Shot
from analyzer.utils import env

		self.db_name = DB_NAME

		self.t_key = "titleId"

		self.client = None
		self.context = self.connect(self.host, self.user, self.passwd, self.db_name)

	def connect(self, host, user, passwd, db_name):
		uri = "mongodb://{}:{}@{}/{}?authMechanism=SCRAM-SHA-1".format(user, passwd, host, db_name)
		self.client = MongoClient(uri)
		db = self.client[db_name]

		self.context = db
		return db

	def find_shots(self, title):
		title_object = self.context.titles.find_one({"name": title})

		return list(self.context.shots.find({"titleId": title_object["_id"]}))

	def find_title(self, project):
		return self.context.titles.find_one({"identifier": project.identifier})

	def remove_shots(self, title):
		self.context.shots.delete_many({"titleId": title["_id"]})

	def cinemetrics(self, movie_id):
		movie = self.context.cinemetrics.find_one({"id": movie_id})

		def cinemetric_to_shot(metrics, index):

			return Shot(start_index=metrics["TC"], id=index)