# -*- coding: utf-8 -*-
from ppms import ppms
from ppms.migration.packet import *

class ChangeMissingTrafficlightDialogMessage(SQLPacket):
    '''
    Change Missing Trafficlight Dialog Message

    changes the dialog message which pops up when a trafficlight di is missing
    '''
    wi_number = 20996  # The workitem number this packet belongs to. Only relevant for packets by PLANTA
    version = 1
    done_after_success = True  # Sets the "Erledigt" flag after running successfully
    run_on_startup = True  # Runs this migration packet when the server is started in migration mode
    category = PacketCategory.CATEGORY_MANDATORY  # Categorizes the packet

    def fix(self, runner):
        languages = {"DE": """Das Ampel-DI %s, das für das DI %s im Datenbereich %s hinterlegt ist, existiert nicht.""",
                     "EN": """The traffic light DI %s used for DI %s in data area %s does not exist.""",
                     "PT": """O DI de semáforo %s predefinido para o DI %s na área de dados %s não existe.""",
                     "FR": """ED Feu %s pour ED %s dans zone de données %s n''existe pas."""}


        query_template = """update DT435 set DI001549 = '{new_itext}' where DI001538 = 740453 and DI001550 = '{language}'"""

        for language, itext in languages.items():
            try:
                query = query_template.format(new_itext=itext,language=language)
                self.modify_and_log(query)
            except Exception as e:
                return self.fail(e)

        return self.success()