# Copyright (c) 2022, JINGROW and Contributors
# See license.txt

import jingrow
from jingrow.core.pagetype.pagetype.test_pagetype import new_pagetype
from jingrow.core.pagetype.page_naming_settings.page_naming_settings import (
	PageNamingSettings,
)
from jingrow.model.naming import NamingSeries, get_default_naming_series
from jingrow.tests.utils import JingrowTestCase
from jingrow.utils import cint


class TestNamingSeries(JingrowTestCase):
	@classmethod
	def setUpClass(cls):
		super().setUpClass()
		cls.ns_pagetype = (
			new_pagetype(
				fields=[
					{
						"label": "Series",
						"fieldname": "naming_series",
						"fieldtype": "Select",
						"options": f"\n{jingrow.generate_hash()}-.###",
					}
				],
				autoname="naming_series:",
				is_submittable=1,
			)
			.insert()
			.name
		)

	def setUp(self):
		self.dns: PageNamingSettings = jingrow.get_pg("Page Naming Settings")

	def tearDown(self):
		jingrow.db.rollback()

	def get_valid_serieses(self):
		VALID_SERIES = ["SINV-", "SI-.{field}.", "SI-#.###", ""]
		exisiting_series = self.dns.get_transactions_and_prefixes()["prefixes"]
		return VALID_SERIES + exisiting_series

	def test_naming_preview(self):
		self.dns.transaction_type = self.ns_pagetype

		self.dns.try_naming_series = "AXBZ.####"
		serieses = self.dns.preview_series().split("\n")
		self.assertEqual(["AXBZ0001", "AXBZ0002", "AXBZ0003"], serieses)

		self.dns.try_naming_series = "AXBZ-.{currency}.-"
		serieses = self.dns.preview_series().split("\n")

	def test_get_transactions(self):
		naming_info = self.dns.get_transactions_and_prefixes()
		self.assertIn(self.ns_pagetype, naming_info["transactions"])

		existing_naming_series = jingrow.get_meta(self.ns_pagetype).get_field("naming_series").options

		for series in existing_naming_series.split("\n"):
			self.assertIn(NamingSeries(series).get_prefix(), naming_info["prefixes"])

	def test_default_naming_series(self):
		self.assertIsNone(get_default_naming_series("PageType"))

	def test_updates_naming_options(self):
		self.dns.transaction_type = self.ns_pagetype
		test_series = "KOOHBEW.###"
		self.dns.naming_series_options = self.dns.get_options() + "\n" + test_series
		self.dns.update_series()
		self.assertIn(test_series, jingrow.get_meta(self.ns_pagetype).get_naming_series_options())

	def test_update_series_counter(self):
		for series in self.get_valid_serieses():
			if not series:
				continue
			self.dns.prefix = series
			current_count = cint(self.dns.get_current())
			new_count = self.dns.current_value = current_count + 1
			self.dns.update_series_start()

			self.assertEqual(self.dns.get_current(), new_count, f"Incorrect update for {series}")

	def test_amended_naming(self):
		self.dns.amend_naming_override = []
		self.dns.default_amend_naming = "Amend Counter"
		self.dns.update_amendment_rule()

		submittable_pg = jingrow.get_pg(
			dict(pagetype=self.ns_pagetype, some_fieldname="test pg with submit")
		).submit()
		submittable_pg.cancel()

		amended_pg = jingrow.get_pg(
			dict(
				pagetype=self.ns_pagetype,
				some_fieldname="test pg with submit",
				amended_from=submittable_pg.name,
			)
		).insert()

		self.assertIn(submittable_pg.name, amended_pg.name)
		amended_pg.delete()

		self.dns.default_amend_naming = "Default Naming"
		self.dns.update_amendment_rule()

		new_amended_pg = jingrow.get_pg(
			dict(
				pagetype=self.ns_pagetype,
				some_fieldname="test pg with submit",
				amended_from=submittable_pg.name,
			)
		).insert()
		self.assertNotIn(submittable_pg.name, new_amended_pg.name)
