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

from ppms import ppms
from ppms.constants import *
from ppms.migration import SQLPacket, PacketCategory, MinVersionDependency, MaxVersionDependency, VersionType
from ppms.migration.packet import *
from ppms.migration.utilities import *


class FixFetchSizeMessages(SQLPacket):
    """Updates dialog messages for fetch size limits and adds translations"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 14701

    OLD_PACKETS = ('migration.server.S_39_5_9.WI12982_fetch_size_limits.FetchSizeLimits',
                   'migration.server.S_39_5_10.WI12982_update_fetch_size_message.FetchSizeMessages')

    SQL_FILE = 'WI16510_fix_fetch_size_messages.sql'

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

    def fix(self, runner):
        failed_packets = list()

        for old_packet in self.OLD_PACKETS:
            packet = get_packet_instance_from_path(old_packet)
            uuid = get_uuid_from_attributes(packet, old_packet)
            migrationrule_record = ppms.search_record(314, [uuid], ['done'])

            if not migrationrule_record.done.get_value():
                failed_packets.append(migrationrule_record)

        self.apply_dml_file(self.sql_planta_de_dbms, self.SQL_FILE)

        for record in failed_packets:
            record.done.set_value(1)
            record.save('done')

        return self.success()