# Copyright (c) 2019, JINGROW and Contributors
# License: MIT. See LICENSE
import json

import jingrow
from jingrow.desk.listview import get_group_by_count, get_list_settings, set_list_settings
from jingrow.desk.reportview import get
from jingrow.tests.utils import JingrowTestCase


class TestListView(JingrowTestCase):
	def setUp(self):
		if jingrow.db.exists("List View Settings", "PageType"):
			jingrow.delete_pg("List View Settings", "PageType")

	def test_get_list_settings_without_settings(self):
		self.assertIsNone(get_list_settings("PageType"), None)

	def test_get_list_settings_with_default_settings(self):
		jingrow.get_pg({"pagetype": "List View Settings", "name": "PageType"}).insert()
		settings = get_list_settings("PageType")
		self.assertIsNotNone(settings)

		self.assertEqual(settings.disable_auto_refresh, 0)
		self.assertEqual(settings.disable_count, 0)
		self.assertEqual(settings.disable_comment_count, 0)
		self.assertEqual(settings.disable_sidebar_stats, 0)

	def test_get_list_settings_with_non_default_settings(self):
		jingrow.get_pg({"pagetype": "List View Settings", "name": "PageType", "disable_count": 1}).insert()
		settings = get_list_settings("PageType")
		self.assertIsNotNone(settings)

		self.assertEqual(settings.disable_auto_refresh, 0)
		self.assertEqual(settings.disable_count, 1)
		self.assertEqual(settings.disable_comment_count, 0)
		self.assertEqual(settings.disable_sidebar_stats, 0)

	def test_set_list_settings_without_settings(self):
		set_list_settings("PageType", json.dumps({}))
		settings = jingrow.get_pg("List View Settings", "PageType")

		self.assertEqual(settings.disable_auto_refresh, 0)
		self.assertEqual(settings.disable_count, 0)
		self.assertEqual(settings.disable_comment_count, 0)
		self.assertEqual(settings.disable_sidebar_stats, 0)

	def test_set_list_settings_with_existing_settings(self):
		jingrow.get_pg({"pagetype": "List View Settings", "name": "PageType", "disable_count": 1}).insert()
		set_list_settings("PageType", json.dumps({"disable_count": 0, "disable_auto_refresh": 1}))
		settings = jingrow.get_pg("List View Settings", "PageType")

		self.assertEqual(settings.disable_auto_refresh, 1)
		self.assertEqual(settings.disable_count, 0)
		self.assertEqual(settings.disable_comment_count, 0)
		self.assertEqual(settings.disable_sidebar_stats, 0)

	def test_list_view_child_table_filter_with_created_by_filter(self):
		if jingrow.db.exists("Note", "Test created by filter with child table filter"):
			jingrow.delete_pg("Note", "Test created by filter with child table filter")

		pg = jingrow.get_pg(
			{"pagetype": "Note", "title": "Test created by filter with child table filter", "public": 1}
		)
		pg.append("seen_by", {"user": "Administrator"})
		pg.insert()

		data = {
			d.name: d.count
			for d in get_group_by_count("Note", '[["Note Seen By","user","=","Administrator"]]', "owner")
		}
		self.assertEqual(data["Administrator"], 1)

	def test_get_group_by_invalid_field(self):
		self.assertRaises(
			ValueError,
			get_group_by_count,
			"Note",
			'[["Note Seen By","user","=","Administrator"]]',
			"invalid_field",
		)

	def test_list_view_comment_count(self):
		jingrow.form_dict.pagetype = "PageType"
		jingrow.form_dict.limit = "1"
		jingrow.form_dict.fields = [
			"`tabPageType`.`name`",
		]

		for with_comment_count in (1, True, "1"):
			jingrow.form_dict.with_comment_count = with_comment_count
			self.assertEqual(len(get()["values"][0]), 2)

		for with_comment_count in (0, False, "0", None):
			jingrow.form_dict.with_comment_count = with_comment_count
			self.assertEqual(len(get()["values"][0]), 1)
