# -*- coding: utf-8 -*-

from ppms.migration import ChangeDTPPacket, PacketCategory, PacketDependency, PacketState, MinVersionDependency, MaxVersionDependency, VersionType


class UpdateMigrationModuleCustomizing(ChangeDTPPacket):
    """Update the migration module customizing to the newest version

    Allow packets that ran successfully but are not concluded to be shown in their respective modules
    """
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 17730

    def get_dependencies(self):
        return (MinVersionDependency(version='39.5.0', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5.7', version_type=VersionType.CUSTOMIZING), )

    def change_da_assignments(self):
        errors = 0

        for da_ident_number, old_value in [(345802, '055134'), (345867, '055194')]:
            record = self.get_record(dt_num=406, key_list=[da_ident_number], di_list=['id_410'])
            if not self.change_value(record=record, attribute='id_410', old_value=old_value, new_value='055213'):
                errors += 1

        return errors == 0

    def change_filter(self):
        errors = 0

        for da_id, ident_number in [('055131', 741207), ('055196', 741207)]:
            record = self.get_record(dt_num=411, key_list=[da_id, ident_number], di_list=['filter_to'])
            if not self.change_value(record=record, attribute='filter_to', old_value='', new_value='1'):
                errors += 1

        return errors == 0

    def fix(self, runner):
        if not self.change_da_assignments() or not self.change_filter():
            return self.fail('Not all customizings could be changed')

        return self.success()

