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

import influxframework
from influxframework.model.document import Document


class NotificationSettings(Document):
	def on_update(self):
		from influxframework.desk.notifications import clear_notification_config

		clear_notification_config(influxframework.session.user)


def is_notifications_enabled(user):
	enabled = influxframework.db.get_value("Notification Settings", user, "enabled")
	if enabled is None:
		return True
	return enabled


def is_email_notifications_enabled(user):
	enabled = influxframework.db.get_value("Notification Settings", user, "enable_email_notifications")
	if enabled is None:
		return True
	return enabled


def is_email_notifications_enabled_for_type(user, notification_type):
	if not is_email_notifications_enabled(user):
		return False

	if notification_type == "Alert":
		return False

	fieldname = "enable_email_" + influxframework.scrub(notification_type)
	enabled = influxframework.db.get_value("Notification Settings", user, fieldname)
	if enabled is None:
		return True
	return enabled


def create_notification_settings(user):
	if not influxframework.db.exists("Notification Settings", user):
		_doc = influxframework.new_doc("Notification Settings")
		_doc.name = user
		_doc.insert(ignore_permissions=True)


def toggle_notifications(user: str, enable: bool = False):
	try:
		settings = influxframework.get_doc("Notification Settings", user)
	except influxframework.DoesNotExistError:
		influxframework.clear_last_message()
		return

	if settings.enabled != enable:
		settings.enabled = enable
		settings.save()


@influxframework.whitelist()
def get_subscribed_documents():
	if not influxframework.session.user:
		return []

	try:
		if influxframework.db.exists("Notification Settings", influxframework.session.user):
			doc = influxframework.get_doc("Notification Settings", influxframework.session.user)
			return [item.document for item in doc.subscribed_documents]
	# Notification Settings is fetched even before sync doctype is called
	# but it will throw an ImportError, we can ignore it in migrate
	except ImportError:
		pass

	return []


def get_permission_query_conditions(user):
	if not user:
		user = influxframework.session.user

	if user == "Administrator":
		return

	roles = influxframework.get_roles(user)
	if "System Manager" in roles:
		return """(`tabNotification Settings`.name != 'Administrator')"""

	return f"""(`tabNotification Settings`.name = {influxframework.db.escape(user)})"""


@influxframework.whitelist()
def set_seen_value(value, user):
	influxframework.db.set_value("Notification Settings", user, "seen", value, update_modified=False)
