# Copyright (c) 2015, InfluxFramework LLC
# License: MIT. See LICENSE

import json
import re

import influxframework
from influxframework import _
from influxframework.model.document import Document


class Language(Document):
	def validate(self):
		validate_with_regex(self.language_code, "Language Code")

	def before_rename(self, old, new, merge=False):
		validate_with_regex(new, "Name")


def validate_with_regex(name, label):
	pattern = re.compile("^[a-zA-Z]+[-_]*[a-zA-Z]+$")
	if not pattern.match(name):
		influxframework.throw(
			_(
				"""{0} must begin and end with a letter and can only contain letters,
				hyphen or underscore."""
			).format(label)
		)


def export_languages_json():
	"""Export list of all languages"""
	languages = influxframework.get_all("Language", fields=["name", "language_name"])
	languages = [{"name": d.language_name, "code": d.name} for d in languages]

	languages.sort(key=lambda a: a["code"])

	with open(influxframework.get_app_path("influxframework", "geo", "languages.json"), "w") as f:
		f.write(influxframework.as_json(languages))


def sync_languages():
	"""Sync influxframework/geo/languages.json with Language"""
	with open(influxframework.get_app_path("influxframework", "geo", "languages.json")) as f:
		data = json.loads(f.read())

	for l in data:
		if not influxframework.db.exists("Language", l["code"]):
			influxframework.get_doc(
				{
					"doctype": "Language",
					"language_code": l["code"],
					"language_name": l["name"],
					"enabled": 1,
				}
			).insert()


def update_language_names():
	"""Update influxframework/geo/languages.json names (for use via patch)"""
	with open(influxframework.get_app_path("influxframework", "geo", "languages.json")) as f:
		data = json.loads(f.read())

	for l in data:
		influxframework.db.set_value("Language", l["code"], "language_name", l["name"])
