'''*****************************************************************************
***  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 xml.etree import ElementTree

from Iso4217 import Currency


def parse(filePathName: str):
	"""
	Parses the XML file supplied by the ISO from https://www.iso.org/iso-4217-currency-codes.html .
	This file does not contain the currency's symobl information.

	:raises ValueError: When an unexpected element tag is encountered.

	:param filePathName: The file-name with the absolute or relative path to the XML file to parse.

	:return list[Currency]: The extracted currencies from the XML file.
	"""
	currencies = []
	tree = ElementTree.parse(filePathName)

	for elements in tree.getroot().iter('CcyNtry'):
		country = None
		currency = None
		code = None
		number = None
		minor = None

		for info in elements.iter():

			if info.tag == 'CcyNtry':
				continue
			elif info.tag == 'CtryNm':
				country = info.text
			elif info.tag == 'CcyNm':
				currency = info.text
			elif info.tag == 'Ccy':
				code = info.text
			elif info.tag == 'CcyNbr':
				number = int(info.text)
			elif info.tag == 'CcyMnrUnts':
				minor = None if info.text == 'N.A.' else int(info.text)
			else:
				raise ValueError("Failed to process tag '{}'.".format(info.tag))
		currencies.append(Currency(country, currency, code, number, minor, None))
	return currencies
