#!/usr/bin/python3
"""
lark

Verify and sort game ROM images.
"""
# TODO: Write decent UI
import hashlib
import sys
import os
import uuid
import xdg.BaseDirectory

import metadata

HASH_CHUNK_SIZE = 10485760 # 10mb
SQLITE_FILENAME = 'metadata.db'

data_path = os.path.join(xdg.BaseDirectory.xdg_data_home, 'lark')

def get_sha1sum(filename):
	sha1sum = hashlib.sha1()
	with open(filename, 'rb') as file_contents:
		while True:
			chunk = file_contents.read(HASH_CHUNK_SIZE)
			if not chunk:
				break
			sha1sum.update(chunk)

	return sha1sum.hexdigest()

# TODO: Write test code that doesn't depend on external resources.

def _kwargs_parse(kwargs_list):
	kwargs = {}
	for kwarg_string in kwargs_list:
		key, value = kwarg_string.split('=')
		kwargs[key] = value

	return kwargs

action_object = sys.argv[1]
action = sys.argv[2]

metadata.configure(os.path.join(data_path, SQLITE_FILENAME))

# TODO: Use a real UI library. This mess is just intended for development.
if action_object == 'platform':
	if action == 'add':
		print('add a platform')
		platform_shortcode = sys.argv[3]
		platform_name = sys.argv[4]

		platform_data = metadata.Platform(shortcode=platform_shortcode,
											fullname=platform_name)
		with metadata.get_db_session() as session:
			session.add(platform_data)

	elif action == 'list':
		# TODO: Filter support is exclusively limited to SQLAlchemy's filter.ilike function. Figure
		# out a good way to include other filters.
		filters = _kwargs_parse(sys.argv[3:])
		with metadata.get_db_session() as session:
			print(metadata.search(session, metadata.Platform, **filters))

	elif action == 'remove':
		constraints = sys.argv[3:]
		filters = _kwargs_parse(sys.argv[3:])

		with metadata.get_db_session() as session:
			platforms = metadata.search(session, metadata.Platform, **filters)
			for platform in platforms:
				print('Removing %s.' % platform.fullname)
				session.delete(platform)

	elif action == 'test':
		# TODO: Delete this action before merging into dev. It's just for ugly testing.
		platform_shortcode = sys.argv[3]
		platform_name = sys.argv[4]

		platform_data = metadata.Platform(shortcode=platform_shortcode,
											fullname=platform_name)

		with metadata.get_db_session() as session:
			print(metadata.search(session, metadata.Platform))

elif action_object == 'release-group':
	if action == 'add':
		properties = _kwargs_parse(sys.argv[3:])
		release_group = metadata.ReleaseGroup(name=properties['name'])

		with metadata.get_db_session() as session:
			if properties['platform']:
				platform = metadata.search(session, metadata.Platform,
											shortcode=properties['platform'])[0]
				release_group.platform = platform
			session.add(release_group)

	if action == 'list':
		# TODO: Filter support is exclusively limited to SQLAlchemy's filter.ilike function. Figure
		# out a good way to include other filters.
		print('Listing release groups.')
		filters = _kwargs_parse(sys.argv[3:])
		with metadata.get_db_session() as session:
			print(metadata.search(session, metadata.ReleaseGroup, **filters))

	elif action == 'remove':
		constraints = sys.argv[3:]
		filters = _kwargs_parse(sys.argv[3:])

		with metadata.get_db_session() as session:
			release_groups = metadata.search(session, metadata.ReleaseGroup, **filters)
			for release_group in release_groups:
				print('Removing %s.' % release_group.name)
				session.delete(release_group)

elif action_object == 'release':
	if action == 'add':
		properties = _kwargs_parse(sys.argv[3:])
		release = metadata.Release(**properties)

		with metadata.get_db_session() as session:
			if properties['release-group']:
				release_group = metadata.search(session, metadata.ReleaseGroup,
												name=properties['release-group'])[0]
				release.release_group = release_group
			session.add(release)

	if action == 'list':
		# TODO: Filter support is exclusively limited to SQLAlchemy's filter.ilike function. Figure
		# out a good way to include other filters.
		print('Listing releases.')
		filters = _kwargs_parse(sys.argv[3:])
		with metadata.get_db_session() as session:
			print(metadata.search(session, metadata.Release, **filters))

	elif action == 'remove':
		constraints = sys.argv[3:]
		filters = _kwargs_parse(sys.argv[3:])

		with metadata.get_db_session() as session:
			release_groups = metadata.search(session, metadata.Release, **filters)
			for release in release_groups:
				print('Removing %s.' % release.name)
				session.delete(release)
else:
	print('Unknown object.')
