'''*****************************************************************************
***  This file is part of Pecunia.                                           ***
***                                                                          ***
***  Copyright (C) 2017, 2018, 2019, 2020, 2021, 2023                        ***
***  John Schneiderman <Licencing _AT_ Schneiderman _DOT_ me>                ***
***                                                                          ***
***  This program is free software: you can redistribute it and/or modify it ***
***  under the terms of the GNU Lesser General Public License as published   ***
***  by the Free Software Foundation, either version 3 of the License, or    ***
***  (at your option) any later version.                                     ***
***                                                                          ***
***  This program is distributed in the hope that it will be useful, but     ***
***  WITHOUT ANY WARRANTY; without even the implied warranty of              ***
***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    ***
***  See the GNU Lesser General Public License for more details.             ***
***                                                                          ***
***  You should have received a copy of the GNU Lesser General Public License***
***  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
*****************************************************************************'''
from Iso4217 import Currency


class CodesTemplateFiller(object):
	"""
	Fills in the template variables for the Codes declaration and definition files.
	"""

	def __init__(self, currencies):
		self.__currencies = currencies
		self.__codes_set = set()

		for currency in self.__currencies:
			if currency.code is None:
				continue
			self.__codes_set.add(currency.code)
		self.__codes_set = sorted(self.__codes_set)

	def filled_to_string(self):
		template = """	case Iso4217Codes::{code}:
		return "{code}";
"""
		to_iso4217 = ""

		for iso_code in self.__codes_set:
			to_iso4217 += template.format(code=iso_code)
		return to_iso4217[0:-1]


	def filled_to_code(self):
		template = """
	if ("{code}" == code)
		return Iso4217Codes::{code};
"""
		to_code = ""

		for iso_code in self.__codes_set:
			to_code += template.format(code=iso_code)
		return to_code[1:-1]


	def filled_to_symbol(self):
		map_code_to_symbol = {}

		for currency in self.__currencies:
			# HACK: symbols for the self.__currencies I use.
			if currency.symbol is not None and currency.code is not None:
				map_code_to_symbol[currency.code] = currency.symbol
			elif currency.code == "USD":
				map_code_to_symbol[currency.code] = "$"
			elif currency.code == "PLN":
				map_code_to_symbol[currency.code] = "zł"
			elif currency.code == "EUR":
				map_code_to_symbol[currency.code] = "€"
		template = """	case Iso4217Codes::{code}:
		return "{symbol}";
"""
		to_symbol = ""

		for (code, symbol) in sorted(map_code_to_symbol.items()):
			to_symbol += template.format(code=code, symbol=symbol)
		return to_symbol[0:-1]


	def filled_minor_unit_digits(self):
		map_minor_to_codes = {}

		for currency in self.__currencies:
			if currency.code is None:
				continue
			elif currency.minor is None:
				if 0 not in map_minor_to_codes:
					map_minor_to_codes[0] = set()
				map_minor_to_codes[0].add(currency.code)
				continue
			elif currency.minor not in map_minor_to_codes:
				map_minor_to_codes[currency.minor] = set()
			map_minor_to_codes[currency.minor].add(currency.code)
		template = """{codes}
		return {minor_unit}u;
"""
		minor_unit_digits = ""

		for minor, codes in sorted(map_minor_to_codes.items()):
			codeCases = ""

			for code in sorted(codes):
				codeCases += "\tcase Iso4217Codes::{code}:\n".format(code=code)

			minor_unit_digits += template.format(minor_unit=minor, codes=codeCases[0:-1])
		return minor_unit_digits[0:-1]


	def filled_all_iso_codes(self):
		template = """		Iso4217Codes::{code},
"""
		all_iso_codes = ""

		for iso_code in self.__codes_set:
			all_iso_codes += template.format(code=iso_code)
		return all_iso_codes[0:-2]


	def filled_enum_codes(self):
		template = """	/// The currency code for the "{name}" used in: {countries}.
	{code} = {number},
"""
		map_code_to_name = {}
		map_code_to_countries = {}
		map_code_to_number = {}

		for currency in self.__currencies:
			if currency.code is None:
				continue
			elif currency.code not in map_code_to_countries:
				map_code_to_countries[currency.code] = set()
			map_code_to_countries[currency.code].add(currency.country)
			map_code_to_number[currency.code] = currency.number
			map_code_to_name[currency.code] = currency.name
		enum_codes = ""

		for code, countries in sorted(map_code_to_countries.items()):
			enum_codes += template.format(
				name=map_code_to_name[code],
				countries='; '.join(sorted(countries)),
				code=code,
				number=map_code_to_number[code]
			)
		return enum_codes[0:-1]


	def filled_all_codes_to_countries(self):
		template = """		{{
			Iso4217Codes::{code},
			{{
				{{"{countries}"s}}
			}}
		}},
"""
		map_code_to_countries = {}

		for currency in self.__currencies:
			if currency.code is None:
				continue
			elif currency.code not in map_code_to_countries:
				map_code_to_countries[currency.code] = set()
			map_code_to_countries[currency.code].add(currency.country.replace('"', '\\"'))
		all_codes_to_countries = ""

		for code, countries in sorted(map_code_to_countries.items()):
			all_codes_to_countries += template.format(
				countries='"s, "'.join(sorted(countries)),
				code=code
			)
		return all_codes_to_countries [0:-2]


	def filled_all_codes_to_names(self):
		template = """		{{
			Iso4217Codes::{code},
			"{name}"s
		}},
"""
		map_code_to_names = {}

		for currency in self.__currencies:
			if currency.code is None:
				continue
			map_code_to_names[currency.code] = currency.name
		all_codes_to_names = ""

		for code, name in sorted(map_code_to_names.items()):
			all_codes_to_names += template.format(name=name, code=code)
		return all_codes_to_names [0:-2]


	def filled_number_of_codes(self):
		return len(self.__codes_set)
