import copy
from ucsschool.importer.utils.post_read_pyhook import PostReadPyHook


class SwitchGivenNameCnHook(PostReadPyHook):
	priority = {
		"entry_read": 1,
		"all_entries_read": 1,
	}

	my_custom_class_variable = 0

	def entry_read(self, entry_count, input_data, input_dict):
		"""
		Run code after an entry has been read and saved in
		input_data and input_dict. This hook may alter input_data
		and input_dict to modify the input data.

		:param int entry_count: index of the data entry (e.g. line of the CSV file)
		:param list[str] input_data: input data as raw as possible (e.g. raw CSV columns). The input_data may be changed.
		:param input_dict: input data mapped to column names. The input_dict may be changed.
		:type input_dict: dict[str, str]
		:return: None
		"""
		self.logger.info('Switching firstname and lastname...')
		self.logger.debug('Before: entry_count=%r input_data=%r input_dict=%r', entry_count, input_data, input_dict)

		firstname = input_dict['Vor']
		lastname = input_dict['Nach']
		for num, entry in enumerate(copy.copy(input_data)):
			if entry == firstname:
				input_data[num] = lastname
			elif entry == lastname:
				input_data[num] = firstname
		input_dict['Vor'] = lastname
		input_dict['Nach'] = firstname

		self.__class__.my_custom_class_variable += 1

		self.logger.debug('Result: entry_count=%r input_data=%r input_dict=%r', entry_count, input_data, input_dict)

	def all_entries_read(self, imported_users, errors):
		"""
		Run code after all entries have been read. ImportUser objects for all
		lines are passed to the hook. Also errors are passed. Please note that
		the "entry_read" hook method may skip one or several input records, so
		they may be missing in imported_users.
		errors contains a list of catched errors/exceptions.

		:param list[ImportUser] imported_users: list of ImportUser objects created from the input records
		:param list[Exception] errors: list of exceptions that are caught during processing the input records
		:return: None
		"""
		self.logger.info('Number of imported users is %s', len(imported_users))
		self.logger.info('Number of errors is %s', len(errors))
		for i, user in enumerate(imported_users):
			user.birthday = '2018-08-27'

		assert self.my_custom_class_variable != 0, "cannot store data as class variables of PostReadPyHook"
