# Copyright (c) 2015, JINGROW and Contributors
# License: MIT. See LICENSE

import os

import click

import jingrow
from jingrow.core.pagetype.data_import.data_import import export_json, import_pg


def sync_fixtures(app=None):
	"""Import, overwrite fixtures from `[app]/fixtures`"""
	if app:
		apps = [app]
	else:
		apps = jingrow.get_installed_apps()

	jingrow.flags.in_fixtures = True

	for app in apps:
		import_fixtures(app)
		import_custom_scripts(app)

	jingrow.flags.in_fixtures = False


def import_fixtures(app):
	fixtures_path = jingrow.get_app_path(app, "fixtures")
	if not os.path.exists(fixtures_path):
		return

	fixture_files = os.listdir(fixtures_path)

	for fname in fixture_files:
		if not fname.endswith(".json"):
			continue

		file_path = jingrow.get_app_path(app, "fixtures", fname)
		try:
			import_pg(file_path)
		except (ImportError, jingrow.DoesNotExistError) as e:
			# fixture syncing for missing pagetypes
			print(f"Skipping fixture syncing from the file {fname}. Reason: {e}")


def import_custom_scripts(app):
	"""Import custom scripts from `[app]/fixtures/custom_scripts`"""
	scripts_folder = jingrow.get_app_path(app, "fixtures", "custom_scripts")
	if not os.path.exists(scripts_folder):
		return

	for fname in os.listdir(scripts_folder):
		if not fname.endswith(".js"):
			continue

		click.secho(
			f"Importing Client Script `{fname}` from `{scripts_folder}` is not supported. Convert the client script to fixture.",
			fg="red",
		)


def export_fixtures(app=None):
	"""Export fixtures as JSON to `[app]/fixtures`"""
	if app:
		apps = [app]
	else:
		apps = jingrow.get_installed_apps()
	for app in apps:
		for fixture in jingrow.get_hooks("fixtures", app_name=app):
			filters = None
			or_filters = None
			if isinstance(fixture, dict):
				filters = fixture.get("filters")
				or_filters = fixture.get("or_filters")
				fixture = fixture.get("pagetype") or fixture.get("dt")
			print(f"Exporting {fixture} app {app} filters {(filters if filters else or_filters)}")
			if not os.path.exists(jingrow.get_app_path(app, "fixtures")):
				os.mkdir(jingrow.get_app_path(app, "fixtures"))

			export_json(
				fixture,
				jingrow.get_app_path(app, "fixtures", jingrow.scrub(fixture) + ".json"),
				filters=filters,
				or_filters=or_filters,
				order_by="idx asc, creation asc",
			)
