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

BEFORE_HASH_T_OFF = '2713fbd22d980a0263b7f1183593f1c8' # settings CHANGE_TITLE and APPEND_TITLE are off
AFTER_HASH_T_OFF = '52f20d93e6ac055777bc09596a403bb0'
BEFORE_HASH_T_ON = 'aff218374da17a87104164284798bf97' #settings CHANGE_TITLE and APPEND_TITLE are on
AFTER_HASH_T_ON = '462cbf77333834caf2fe20d653dfdee9'
ALREADY_CHANGED = 'ebc37ac203fa7d5ff4a011d762d23647' # usermenu has allready been changed, no changes necessary

class FixUsermenuMacro(MigratePythonPacket):
    '''Fix an error in the usermenu macro MOD0099QC.

    The error occured because of twiddled digits('25122' instead of '25112').
    Only needed with an Oracle database.

    Since different usermenu macro variants are checked this packet might report
    failures in the logfile, but it's only marked as failure if no expected
    macro variant was found.
    '''

    wi_number = 10260
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY

    def fix(self, runner):
        fix_func = lambda code: code.replace('25122', '25112')
        
        if self.migrate_python(BEFORE_HASH_T_ON, ALREADY_CHANGED,
                               self.CU_TYPE_MODULE, '0099QC', fix_func):
            return self.success()
        elif self.migrate_python(BEFORE_HASH_T_ON, AFTER_HASH_T_ON,
                               self.CU_TYPE_MODULE, '0099QC', fix_func):
            return self.success()

        elif self.migrate_python(BEFORE_HASH_T_OFF, AFTER_HASH_T_OFF,
                               self.CU_TYPE_MODULE, '0099QC', fix_func):
            return self.success()
        
        return self.fail()
        
