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

BEFORE_HASH_PY_EDITOR = '5b505e95540e3b7eba8fa8c7330e46c3'
AFTER_HASH_PY_EDITOR = 'b5c922b52ee1789fd9bcae16e89e0871'

NEW_CODE = '''from ppms.client_editor import py_editor

py_editor.main()'''

class FixPythonEditor(MigratePythonPacket, ChangeDTPPacket):
    r'''Port the new Python Editor launch code to VoE

    This packet will also try to change the value of the py_editor global
    setting from C:\Program Files\Notepad++\Notepad++.exe to
    C:\Program Files (x86)\Notepad++\Notepad++.exe. This will fail if
    the setting was individualized, so the packet will still succeed.
    '''
    wi_number = 11079
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY

    def fix(self, runner):
        fix_func = lambda code: NEW_CODE

        if self.migrate_python(BEFORE_HASH_PY_EDITOR, AFTER_HASH_PY_EDITOR,
                               self.CU_TYPE_MODULE, '0099Y6', fix_func):

            record = self.get_record(333, ['b733c1c1-68bb-4ed2-82d9-04e1968efc38'], [51828])
            self.change_value(record, 'alpha120',
                              r'C:\Program Files\Notepad++\notepad++.exe',
                              r'C:\Program Files (x86)\Notepad++\Notepad++.exe')

            return self.success()


        return self.fail()

BEFORE_HASH_PRETTIFY = '68295d1c47db9b5670a1fea8f93fbc9f'
AFTER_HASH_PRETTIFY = '05fc13bc0136fd3e7f83d1d2454c5cb2'
        
class FixPrettifyScript(MigratePythonPacket):
    '''Fix the prettify script that gives valueranges color not working'''
    wi_number = 11079
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY

    def fix(self, runner):
        fix_func = lambda code: code.replace('sys.path[1]', 'sys.path[2]')

        if self.migrate_python(BEFORE_HASH_PRETTIFY, AFTER_HASH_PRETTIFY,
                               self.CU_TYPE_IRONPYTHON, ['027139', 716659], fix_func):
            return self.success()


        return self.fail()