# -*- coding: utf-8 -*-
from ppms.migration import ChangeDTPPacket, PacketCategory, MaxVersionDependency, VersionType, \
    PacketDependency, PacketState

PACKET_PATH = 'migration.hotfix.S_39_5_4.wi11533_port_universal_interface_VoE.PortCustomizingUniversalInterface'


class FixInterfaceModuleRelation(ChangeDTPPacket):
    """Changes the relations between 560 and 405"""
    version = 1
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13266

    def get_dependencies(self):
        return (PacketDependency(packet_path=PACKET_PATH, expected_state=PacketState.INSTALLED),
                MaxVersionDependency(version='39.5.3', version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):
        failures = 0
        for starting_di in ('062624', '062625', '062767'):
            record = self.get_record(dt_num=416, key_list=['560', '405', starting_di], di_list=['delete_flag'])

            if not self.change_value(record=record, attribute='delete_flag', old_value=0, new_value=1):
                failures += 1

        if failures:
            return self.fail('Could not set deletion flag from 0 to 1 for all 405 relations in 560')

        return self.success()