# Copyright (c) 2019, JINGROW and Contributors
# License: MIT. See LICENSE
import jingrow
from jingrow.core.pagetype.data_export.exporter import DataExporter
from jingrow.tests.utils import JingrowTestCase


class TestDataExporter(JingrowTestCase):
	def setUp(self):
		self.pagetype_name = "Test PageType for Export Tool"
		self.pg_name = "Test Data for Export Tool"
		self.create_pagetype_if_not_exists(pagetype_name=self.pagetype_name)
		self.create_test_data()

	def create_pagetype_if_not_exists(self, pagetype_name, force=False):
		"""
		Helper Function for setting up pagetypes
		"""
		if force:
			jingrow.delete_pg_if_exists("PageType", pagetype_name)
			jingrow.delete_pg_if_exists("PageType", "Child 1 of " + pagetype_name)

		if jingrow.db.exists("PageType", pagetype_name):
			return

		# Child Table 1
		table_1_name = "Child 1 of " + pagetype_name
		jingrow.get_pg(
			{
				"pagetype": "PageType",
				"name": table_1_name,
				"module": "Custom",
				"custom": 1,
				"istable": 1,
				"fields": [
					{"label": "Child Title", "fieldname": "child_title", "reqd": 1, "fieldtype": "Data"},
					{"label": "Child Number", "fieldname": "child_number", "fieldtype": "Int"},
				],
			}
		).insert()

		# Main Table
		jingrow.get_pg(
			{
				"pagetype": "PageType",
				"name": pagetype_name,
				"module": "Custom",
				"custom": 1,
				"autoname": "field:title",
				"fields": [
					{"label": "Title", "fieldname": "title", "reqd": 1, "fieldtype": "Data"},
					{"label": "Number", "fieldname": "number", "fieldtype": "Int"},
					{
						"label": "Table Field 1",
						"fieldname": "table_field_1",
						"fieldtype": "Table",
						"options": table_1_name,
					},
				],
				"permissions": [{"role": "System Manager"}],
			}
		).insert()

	def create_test_data(self, force=False):
		"""
		Helper Function creating test data
		"""
		if force:
			jingrow.delete_pg(self.pagetype_name, self.pg_name)

		if not jingrow.db.exists(self.pagetype_name, self.pg_name):
			self.pg = jingrow.get_pg(
				pagetype=self.pagetype_name,
				title=self.pg_name,
				number="100",
				table_field_1=[
					{"child_title": "Child Title 1", "child_number": "50"},
					{"child_title": "Child Title 2", "child_number": "51"},
				],
			).insert()
		else:
			self.pg = jingrow.get_pg(self.pagetype_name, self.pg_name)

	def test_export_content(self):
		exp = DataExporter(pagetype=self.pagetype_name, file_type="CSV")
		exp.build_response()

		self.assertEqual(jingrow.response["type"], "csv")
		self.assertEqual(jingrow.response["pagetype"], self.pagetype_name)
		self.assertTrue(jingrow.response["result"])
		self.assertRegex(jingrow.response["result"], r"Child Title 1.*?,50")
		self.assertRegex(jingrow.response["result"], r"Child Title 2.*?,51")

	def test_export_type(self):
		for type in ["csv", "Excel"]:
			with self.subTest(type=type):
				exp = DataExporter(pagetype=self.pagetype_name, file_type=type)
				exp.build_response()

				self.assertEqual(jingrow.response["pagetype"], self.pagetype_name)
				self.assertTrue(jingrow.response["result"])

				if type == "csv":
					self.assertEqual(jingrow.response["type"], "csv")
				elif type == "Excel":
					self.assertEqual(jingrow.response["type"], "binary")
					self.assertEqual(
						jingrow.response["filename"], self.pagetype_name + ".xlsx"
					)  # 'Test PageType for Export Tool.xlsx')
					self.assertTrue(jingrow.response["filecontent"])

	def tearDown(self):
		pass
