# -*- coding: utf-8 -*-
from ppms.migration.packet import *
from . import DEFAULT_CUSTOMIZING_DEPENDENCY

OLD_VALUE = """from ppms.interface import UndefinedMapping, MAPPING_FUNCTIONS, get_mapping_instance_from_uuid

def computeOutput(di):
    record = di.get_dtp_record()

    _type = record.type.get_value()

    # Source/Pool/Target Mappings are always welcome
    if _type not in MAPPING_FUNCTIONS:
        return True

    uuid = record.uuid.get_value()
    instance = get_mapping_instance_from_uuid(uuid)

    return not isinstance(instance, UndefinedMapping)

computeOutput.deps = ('DI062652', 'DI062654')"""

NEW_VALUE = """from ppms.interface import UndefinedMapping, MAPPING_FUNCTIONS, get_mapping_instance_from_uuid

def computeOutput(di):
    record = di.get_dtp_record()

    _type = record.type.get_value()

    # Source/Pool/Target Mappings are always welcome
    if _type not in MAPPING_FUNCTIONS:
        return True

    uuid = record.uuid.get_value()
    instance = get_mapping_instance_from_uuid(uuid)

    return not isinstance(instance, UndefinedMapping)

computeOutput.deps = ('DI062652', 'DI062654', 'DI062651')"""


class FixInterfaceBorderNotUpdating(ChangeDTPPacket):
    '''Fix the mapping border not updating correctly when a function is inserted'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12311

    def get_dependencies(self):
        return DEFAULT_CUSTOMIZING_DEPENDENCY

    def fix(self, runner):
        record = self.get_record(412, ['062817'], [27352])
        if not self.change_value(record, 'valuerange', OLD_VALUE, NEW_VALUE):
            return self.fail()

        return self.success()
