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

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


class FixBrokenGenderIncarnation(ChangeDTPPacket, SQLPacket):
    """Correct the wrongly customized incarnation on the gender in DT 566"""
    version = 1
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12080

    FILENAME = 'wi12080_gender_incarnation.sql'

    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

        record = self.get_record(dt_num=412, key_list=['062934'], di_list=['hdi_fetch_exit'])
        if not self.change_value(record=record, attribute='hdi_fetch_exit', old_value='062424', new_value='062919'):
            failures += 1

        record = self.get_record(dt_num=412, key_list=['062935'], di_list=['hdi_fetch_exit', 'incarnation_di', 'di_class'])
        if not self.change_value(record=record, attribute='hdi_fetch_exit', old_value='062512', new_value='062934'):
            failures += 1

        if not self.change_value(record=record, attribute='incarnation_di', old_value='062424', new_value='062919'):
            failures += 1

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

        if failures:
            return self.fail('Could not correct incarnation')

        self.apply_dml_file(self.sql_planta_de_dbms, self.FILENAME)

        return self.success()