# Copyright (c) 2015, JINGROW and Contributors
# License: MIT. See LICENSE
import jingrow
from jingrow.core.pagetype.domain_settings.domain_settings import get_active_modules
from jingrow.core.page.permission_manager.permission_manager import get_roles_and_pagetypes
from jingrow.desk.pagetype.desktop_icon.desktop_icon import (
	add_user_icon,
	clear_desktop_icons_cache,
	get_desktop_icons,
)
from jingrow.tests.utils import JingrowTestCase


class TestDomainification(JingrowTestCase):
	def setUp(self):
		# create test domain
		self.new_domain("_Test Domain 1")
		self.new_domain("_Test Domain 2")

		self.remove_from_active_domains(remove_all=True)
		self.add_active_domain("_Test Domain 1")

	def tearDown(self):
		jingrow.db.delete("Role", {"name": "_Test Role"})
		jingrow.db.delete("Has Role", {"role": "_Test Role"})
		jingrow.db.delete("Domain", {"name": ("in", ("_Test Domain 1", "_Test Domain 2"))})
		jingrow.delete_pg("PageType", "Test Domainification")
		self.remove_from_active_domains(remove_all=True)

	def add_active_domain(self, domain):
		"""add domain in active domain"""

		if not domain:
			return

		domain_settings = jingrow.get_pg("Domain Settings", "Domain Settings")
		domain_settings.append("active_domains", {"domain": domain})
		domain_settings.save()

	def remove_from_active_domains(self, domain=None, remove_all=False):
		"""remove domain from domain settings"""
		if not (domain or remove_all):
			return

		domain_settings = jingrow.get_pg("Domain Settings", "Domain Settings")

		if remove_all:
			domain_settings.set("active_domains", [])
		else:
			to_remove = []
			[to_remove.append(row) for row in domain_settings.active_domains if row.domain == domain]
			[domain_settings.remove(row) for row in to_remove]

		domain_settings.save()

	def new_domain(self, domain):
		# create new domain
		jingrow.get_pg({"pagetype": "Domain", "domain": domain}).insert()

	def new_pagetype(self, name):
		return jingrow.get_pg(
			{
				"pagetype": "PageType",
				"module": "Core",
				"custom": 1,
				"fields": [{"label": "Some Field", "fieldname": "some_fieldname", "fieldtype": "Data"}],
				"permissions": [{"role": "System Manager", "read": 1}],
				"name": name,
			}
		)

	def test_active_domains(self):
		self.assertTrue("_Test Domain 1" in jingrow.get_active_domains())
		self.assertFalse("_Test Domain 2" in jingrow.get_active_domains())

		self.add_active_domain("_Test Domain 2")
		self.assertTrue("_Test Domain 2" in jingrow.get_active_domains())

		self.remove_from_active_domains("_Test Domain 1")
		self.assertTrue("_Test Domain 1" not in jingrow.get_active_domains())

	def test_pagetype_and_role_domainification(self):
		"""
		test if pagetype is hidden if the pagetype's restrict to domain is not included
		in active domains
		"""

		test_pagetype = self.new_pagetype("Test Domainification")
		test_pagetype.insert()

		test_role = jingrow.get_pg({"pagetype": "Role", "role_name": "_Test Role"}).insert()

		# pagetype should be hidden in desktop icon, role permissions
		results = get_roles_and_pagetypes()
		self.assertTrue("Test Domainification" in [d.get("value") for d in results.get("pagetypes")])
		self.assertTrue("_Test Role" in [d.get("value") for d in results.get("roles")])

		self.add_active_domain("_Test Domain 2")
		test_pagetype.restrict_to_domain = "_Test Domain 2"
		test_pagetype.save()

		test_role.restrict_to_domain = "_Test Domain 2"
		test_role.save()

		results = get_roles_and_pagetypes()
		self.assertTrue("Test Domainification" in [d.get("value") for d in results.get("pagetypes")])
		self.assertTrue("_Test Role" in [d.get("value") for d in results.get("roles")])

		self.remove_from_active_domains("_Test Domain 2")
		results = get_roles_and_pagetypes()

		self.assertTrue("Test Domainification" not in [d.get("value") for d in results.get("pagetypes")])
		self.assertTrue("_Test Role" not in [d.get("value") for d in results.get("roles")])

	def test_desktop_icon_for_domainification(self):
		"""desktop icon should be hidden if pagetype's restrict to domain is not in active domains"""

		test_pagetype = self.new_pagetype("Test Domainification")
		test_pagetype.restrict_to_domain = "_Test Domain 2"
		test_pagetype.insert()

		self.add_active_domain("_Test Domain 2")
		add_user_icon("Test Domainification")

		icons = get_desktop_icons()

		pagetypes = [
			icon.get("_pagetype")
			for icon in icons
			if icon.get("_pagetype") == "Test Domainification" and icon.get("blocked") == 0
		]
		self.assertTrue("Test Domainification" in pagetypes)

		# pagetype should be hidden from the desk
		self.remove_from_active_domains("_Test Domain 2")
		clear_desktop_icons_cache()  # clear cache to fetch the desktop icon according to new active domains
		icons = get_desktop_icons()

		pagetypes = [
			icon.get("_pagetype")
			for icon in icons
			if icon.get("_pagetype") == "Test Domainification" and icon.get("blocked") == 0
		]
		self.assertFalse("Test Domainification" in pagetypes)

	def test_module_def_for_domainification(self):
		"""modules should be hidden if module def's restrict to domain is not in active domains"""

		test_module_def = jingrow.get_pg("Module Def", "Contacts")
		test_module_def.restrict_to_domain = "_Test Domain 2"
		test_module_def.save()

		self.add_active_domain("_Test Domain 2")

		modules = get_active_modules()
		self.assertTrue("Contacts" in modules)

		# pagetype should be hidden from the desk
		self.remove_from_active_domains("_Test Domain 2")
		modules = get_active_modules()
		self.assertTrue("Contacts" not in modules)

		test_module_def = jingrow.get_pg("Module Def", "Contacts")
		test_module_def.restrict_to_domain = ""
		test_module_def.save()
