# Copyright (c) 2015, JINGROW and Contributors
# License: MIT. See LICENSE
import jingrow
import jingrow.desk.form.assign_to
from jingrow.automation.pagetype.assignment_rule.test_assignment_rule import (
	TEST_PAGETYPE,
	_make_test_record,
	create_test_pagetype,
)
from jingrow.desk.form.load import get_assignments
from jingrow.desk.listview import get_group_by_count
from jingrow.tests.utils import JingrowTestCase


class TestAssign(JingrowTestCase):
	@classmethod
	def setUpClass(cls):
		super().setUpClass()
		create_test_pagetype(TEST_PAGETYPE)

	def test_assign(self):
		todo = jingrow.get_pg({"pagetype": "ToDo", "description": "test"}).insert()
		if not jingrow.db.exists("User", "test@example.com"):
			jingrow.get_pg({"pagetype": "User", "email": "test@example.com", "first_name": "Test"}).insert()

		self._test_basic_assign_on_document(todo)

	def _test_basic_assign_on_document(self, pg):
		added = assign(pg, "test@example.com")

		self.assertTrue("test@example.com" in [d.owner for d in added])

		jingrow.desk.form.assign_to.remove(pg.pagetype, pg.name, "test@example.com")

		# assignment is cleared
		assignments = jingrow.desk.form.assign_to.get(dict(pagetype=pg.pagetype, name=pg.name))
		self.assertEqual(len(assignments), 0)

	def test_assign_single(self):
		c = jingrow.get_pg("Contact Us Settings")
		self._test_basic_assign_on_document(c)

	def test_assignment_count(self):
		jingrow.db.delete("ToDo")

		if not jingrow.db.exists("User", "test_assign1@example.com"):
			jingrow.get_pg(
				{
					"pagetype": "User",
					"email": "test_assign1@example.com",
					"first_name": "Test",
					"roles": [{"role": "System Manager"}],
				}
			).insert()

		if not jingrow.db.exists("User", "test_assign2@example.com"):
			jingrow.get_pg(
				{
					"pagetype": "User",
					"email": "test_assign2@example.com",
					"first_name": "Test",
					"roles": [{"role": "System Manager"}],
				}
			).insert()

		note = _make_test_record()
		assign(note, "test_assign1@example.com")

		note = _make_test_record(public=1)
		assign(note, "test_assign2@example.com")

		note = _make_test_record(public=1)
		assign(note, "test_assign2@example.com")

		note = _make_test_record()
		assign(note, "test_assign2@example.com")

		data = {d.name: d.count for d in get_group_by_count(TEST_PAGETYPE, "[]", "assigned_to")}

		self.assertTrue("test_assign1@example.com" in data)
		self.assertEqual(data["test_assign1@example.com"], 1)
		self.assertEqual(data["test_assign2@example.com"], 3)

		data = {d.name: d.count for d in get_group_by_count(TEST_PAGETYPE, '[{"public": 1}]', "assigned_to")}

		self.assertFalse("test_assign1@example.com" in data)
		self.assertEqual(data["test_assign2@example.com"], 2)

		jingrow.db.rollback()

	def test_assignment_removal(self):
		todo = jingrow.get_pg({"pagetype": "ToDo", "description": "test"}).insert()
		if not jingrow.db.exists("User", "test@example.com"):
			jingrow.get_pg({"pagetype": "User", "email": "test@example.com", "first_name": "Test"}).insert()

		new_todo = assign(todo, "test@example.com")

		# remove assignment
		jingrow.db.set_value("ToDo", new_todo[0].name, "allocated_to", "")

		self.assertFalse(get_assignments("ToDo", todo.name))


def assign(pg, user):
	return jingrow.desk.form.assign_to.add(
		{
			"assign_to": [user],
			"pagetype": pg.pagetype,
			"name": pg.name,
			"description": "test",
		}
	)
