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

class FixVersionString(ChangeDTPPacket):
    '''Update the system title and the I-Texts for the version string.'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 10960

    VALUES = (['EN', 709978, 'PLANTA Project 39.4.4.0', 'PLANTA Project'],
              ['DE', 709978, 'PLANTA Project 39.4.4.0', 'PLANTA Project'],
              ['FR', 709978, 'PLANTA Project 39.4.4.0', 'PLANTA Project'])

    def fix(self, runner):
        errors = 0

        for row in self.VALUES:
            if not self.update_itext(*row):
                errors += 1
        
        sys_title = self.get_record(345, [ppms.uvar_get("@3")], [25112])

        if not self.change_value(sys_title, 'title', 'PLANTA Project 39.4.4.0', 'PLANTA Project'):
            errors += 1

        if not errors:
            return self.success()

        return self.fail('See Above.')

    def update_itext(self, language, i_nr, old_text, new_text):
        rec = self.get_record(435, [i_nr, language], [1538, 1550, 1549])

        if self.change_value(rec, 'i_text', old_text, new_text):
            return True

        return False
