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


class PortMissingEVAMessage(SQLPacket):
    '''Port missing dialogue messages for EVA model parameter'''
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13876
    
    DIALOG_IDS = {'0919': 752672,
                  '1068': 802153,
                  '1089': 804363}
    DML_FILENAME = 'WI13876_port_missing_eva_message.sql'

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

    def fix(self, runner):
        for msg, itext_id in self.DIALOG_IDS.items():
            dialog_rec = ppms.search_record(428, [msg], ['dialog_message'])
            if dialog_rec:
                dialog = dialog_rec.dialog_message.get_value()
                itext_rec = ppms.search_record(435, [itext_id, ppms.uvar_get(SYS_VAR_LANGUAGE)], ['i_text'])
                if not itext_rec:
                    return self.fail("Dialog message with ID '{dialog_id}' already exists, but does not have the expected itext '{itext}'.".format(dialog_id=msg, itext=itext_id))

                itext = itext_rec.i_text.get_value()
                if dialog != itext:
                    return self.fail("Dialog message with ID '{dialog_id}' already exists, but has the itext-value '{itext_current}' instead of the expected itext-value '{itext_expected}'.".format(dialog_id=msg, itext_current=dialog, itext_expected=itext_id))

                self.log("Dialog message with ID '{dialog_id}' already exists and has the expected itext-value '{itext}'.".format(dialog_id=msg, itext=itext))


        self.apply_dml_file(self.sql_planta_de_dbms, self.DML_FILENAME)

        return self.success()