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

TEXT_CONSTANTS = "('001547', '001563', '001572', '001577', '001711', '001561', '001416', '001417', '001562')"
DIALOG_MESSAGES = "('1033', '1034', '1037', '1038', '0939', '0940', '0941', '0947', '0948', '0949', '0950', '0951', " \
                  "'0952')"

DELETE_QUERIES = [
    "DELETE DT435 WHERE DI023517 = '000296' AND DI001538 IN (SELECT DI000296 FROM DT425 WHERE DI000295 IN %s)" %
    TEXT_CONSTANTS,
    "DELETE DT435 WHERE DI023517 = '024662' AND DI001538 IN (SELECT DI024662 FROM DT425 WHERE DI000295 IN %s)" %
    TEXT_CONSTANTS,
    "DELETE DT425 WHERE DI000295 IN %s" % TEXT_CONSTANTS,

    "DELETE DT435 WHERE DI023517 = '001566' AND DI001538 IN (SELECT DI001566 FROM DT428 WHERE DI000357 IN %s)" %
    DIALOG_MESSAGES,
    "DELETE DT435 WHERE DI023517 = '001569' AND DI001538 IN (SELECT DI001569 FROM DT428 WHERE DI000357 IN %s)" %
    DIALOG_MESSAGES,
    "DELETE DT428 WHERE DI000357 IN %s" % DIALOG_MESSAGES]


class PortTextConstantsAndDialogMessages(SQLPacket):
    """Update a bunch of text constants and dialog messages in VoE"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13528

    FILENAME = 'wi13528_text_constants.sql'

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

    def fix(self, runner):
        for delete_query in DELETE_QUERIES:
            self.modify_and_log(delete_query)

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

        return self.success()