from parallels.ppa.converter.business_objects.clients import POAClientsConverter
from parallels.pvps import messages
import logging

from parallels.core import MigrationError
from parallels.core.actions.base.common_action import CommonAction
from parallels.core.checking import Report, PlainReport, Problem
from parallels.core.converter.business_objects.common import SOURCE_TARGET
from parallels.core.converter.business_objects.resellers import ResellersConverter
from parallels.core.target_data_model import Model
from parallels.pvps.converter.pvps_subscription import PVPSSubscriptionConverter
from parallels.core.utils.common import group_by_id, find_first, format_list

logger = logging.getLogger(__name__)


class Convert(CommonAction):
	def get_description(self):
		return messages.CONVERT_CLIENTS_AND_SUBSCRIPTIONS

	def get_failure_message(self, global_context):
		"""
		:type global_context: parallels.core.global_context.GlobalMigrationContext
		"""
		return messages.FAILED_TO_CONVERT_CLIENTS_AND_SUBSCRIPTIONS

	def run(self, global_context):
		"""
		:type global_context: parallels.core.global_context.GlobalMigrationContext
		"""
		target_model = Model(plans={}, resellers={}, clients={})
		report = Report(messages.REPORT_TITLE_CONVERSION_STATUS, None)

		self._convert_resellers(global_context, report, target_model)
		self._convert_clients(global_context, report, target_model)
		self._convert_vps_subscriptions(global_context, report, target_model)
		self._check_plan_compatibility(global_context, report, target_model)
		self._check_report_errors(global_context, report)
		global_context.migrator._write_target_model(target_model)

	@staticmethod
	def _convert_resellers(global_context, report, target_model):
		"""Convert resellers and put them into target model

		:type global_context: parallels.core.global_context.GlobalMigrationContext
		:type report parallels.core.checking.Report
		:type target_model parallels.core.target_data_model.Model
		"""
		converted_resellers = ResellersConverter().convert_resellers(
			global_context.get_source_servers_info(),
			global_context.target_existing_objects.resellers,
			global_context.migration_list_data.resellers,
			report, global_context.password_holder
		)
		converted_resellers_filtered = [
			reseller for reseller in converted_resellers if reseller.source == SOURCE_TARGET
		]
		target_model.resellers.update(group_by_id(converted_resellers_filtered, lambda r: r.login))

	@staticmethod
	def _convert_clients(global_context, report, target_model):
		"""Convert clients and put them into target model

		:type global_context: parallels.core.global_context.GlobalMigrationContext
		:type report parallels.core.checking.Report
		:type target_model parallels.core.target_data_model.Model
		"""
		converted_clients = POAClientsConverter().convert_clients(
			global_context.get_source_servers_info(),
			global_context.target_existing_objects.customers,
			global_context.migration_list_data.customers_mapping,
			report, global_context.password_holder
		)
		POAClientsConverter().assign_clients_to_resellers(
			target_model.clients, target_model.resellers.values(), converted_clients,
			global_context.migration_list_data.customers_mapping, report
		)

	@staticmethod
	def _convert_vps_subscriptions(global_context, report, target_model):
		"""Convert VPS subscriptions and put them into target model

		:type global_context: parallels.core.global_context.GlobalMigrationContext
		:type report parallels.core.checking.Report
		:type target_model parallels.core.target_data_model.Model
		"""
		PVPSSubscriptionConverter().convert_subscriptions(
			global_context.get_source_servers_info(),
			global_context.target_existing_objects,
			global_context.migration_list_data.subscriptions_mapping,
			target_model,
			PlainReport(report, *global_context.migrator._extract_source_objects_info())
		)

	@staticmethod
	def _check_report_errors(global_context, report):
		"""Check report and exit in case of errors

		:type global_context: parallels.core.global_context.GlobalMigrationContext
		:type report parallels.core.checking.Report
		"""
		if report.has_errors():
			global_context.migrator.print_report(
				report, "convert_report_tree", show_no_issue_branches=False
			)
			raise MigrationError(
				messages.UNABLE_TO_CONTINUE_MIGRATION)

	def _check_plan_compatibility(self, global_context, report, target_model):
		"""
		:type global_context: parallels.core.global_context.GlobalMigrationContext
		:type report parallels.core.checking.Report
		:type target_model parallels.core.target_data_model.Model
		"""
		for subscription in target_model.iter_all_subscriptions():
			service_template = find_first(
				global_context.target_existing_objects.vps_service_templates,
				lambda x: x.id == subscription.plan_id
			)
			service_template_report = report.subtarget(u'Subscription', subscription.name) \
				.subtarget(u'Service Template', service_template.name)

			pvps_configs = []
			for resource in service_template.resource_types:
				self._check_resource_limit(resource, service_template_report)
				if self._is_pvps_config_resource(resource):
					pvps_configs.append(resource)

			self._check_pvps_configs(pvps_configs, service_template_report)

	resource_requirements = [
		({'name': 'PVPS_Cloud', 'resclass_name': 'rc.saas.service.link'}, 1),
		({'name': 'PVPS_Panel', 'resclass_name': 'rc.saas.application'}, 1),
		({'name': 'PVPS_License', 'resclass_name': 'rc.saas.service'}, 1),
		({'resclass_name': 'rc.saas.service', 'service_id': 'ip_addresses'}, 1),
	]

	def _check_resource_limit(self, resource, report):
		"""
		:type resource: parallels.pvps.models.existing_objects_model.PVPSResource
		:type report: parallels.core.checking.Report
		"""
		resource_to_check = {
			'name': resource.name,
			'resclass_name': resource.resclass_name,
			'service_id': resource.act_params.get('service_id')
		}

		def is_valid_resource_limit():
			def is_expected_resource(expected_resource):
				for name, value in expected_resource.iteritems():
					if resource_to_check.get(name) != value:
						return False
				return True

			for expected_resource, limit in self.resource_requirements:
				if is_expected_resource(expected_resource):
					return (resource.limit == -1 or resource.limit >= limit), limit
			return True, None

		is_valid, expected_limit = is_valid_resource_limit()
		if not is_valid:
			report.add_issue(
				Problem(
					'wrong_limit_on_service_template_resource',
					Problem.ERROR,
					messages.WRONG_RESOURCE_LIMIT_VALUE.format(
						r_name=resource.name,
						r_id=resource.rt_id,
						e_limit=expected_limit,
						a_limit=resource.limit
					)
				),
				messages.HOW_TO_FIX_LIMIT)

	@staticmethod
	def _check_pvps_configs(pvps_configs, report):
		"""
		:type pvps_configs: list[parallels.pvps.models.existing_objects_model.PVPSResource]
		:type report: parallels.core.checking.Report
		"""
		resources = [(resource.name, resource.rt_id, resource.limit) for resource in pvps_configs]  # todo drop
		resources_with_unzero_limit = filter(lambda x: x[2] != 0, resources)
		if len(resources_with_unzero_limit) > 1:
			report.add_issue(
				Problem(
					'wrong_limit_on_service_template_resource_pvps_config',
					Problem.ERROR,
					messages.MORE_THAN_ONE_PVPS_CONFIG_RESOURCES % format_list(
						[r[0] for r in resources_with_unzero_limit]
					)
				),
				messages.HOW_TO_FIX_EXCESS_LIMIT)
		elif len(resources_with_unzero_limit) == 0:
			report.add_issue(
				Problem(
					'wrong_limit_on_service_template_resource_pvps_config',
					Problem.ERROR,
					messages.WRONG_RESOURCE_LIMIT_VALUE.format(
						r_name=resources[0][0],
						r_id=resources[0][1],
						e_limit=1,
						a_limit=resources[0][2]
					)
				),
				messages.HOW_TO_FIX_LIMIT)

	def _is_pvps_config_resource(self, resource):
		"""
		:type resource: parallels.pvps.models.existing_objects_model.PVPSResource
		"""
		return resource.name.startswith('PVPS_Config_') and \
				resource.resclass_name == 'rc.saas.service.link' and \
				resource.act_params.get('service_id') == 'configurations'
