from parallels.core.actions.base.common_action import CommonAction
from parallels.plesk import messages
from parallels.plesk.converter.ip_mapping import IPMapper
from parallels.plesk.target_data_model import PleskTargetSubscriptionData


class ConvertIPAddressesAction(CommonAction):
    def get_description(self):
        """Get short description of action as string

        :rtype: str | unicode
        """
        return messages.ACTION_CONVERT_IP_ADDRESSES_DESCRIPTION

    def get_failure_message(self, global_context):
        """Get message for situation when action failed

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :rtype: str | unicode
        """
        return messages.ACTION_CONVERT_IP_ADDRESSES_FAILURE

    def run(self, global_context):
        """Run action on given subscription

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :rtype: None
        """
        for subscription in global_context.iter_all_subscriptions():
            subscription.model.target_data = PleskTargetSubscriptionData(
                web_ip_type=subscription.raw_dump.ip_type,
                web_ipv6_type=subscription.raw_dump.ipv6_type
            )

        ip_mapper = IPMapper()
        ip_mapper.map_ip_addresses(
            list(global_context.iter_all_subscriptions()),
            global_context.target_existing_objects,
            global_context.ip_mapping,
            global_context.migration_list_data.subscriptions_mapping,
            global_context.convert_report
        )