# -*- coding: utf-8 -*-
from collections import namedtuple

from ppms.migration.packet import *
       
CorrectionTuple = namedtuple('CorrectionTuple', ('i_text_nr', 'language', 'expected_value', 'new_value'))

I_TEXT_TUPLES = [CorrectionTuple(754784, 'EN', 'Packets from file system', 'Special packets'), # DA name
                 CorrectionTuple(754793, 'EN', 'Read Packets from File System', 'Read packets from file system'), # Module
                 CorrectionTuple(754785, 'EN', 'Read packets from file system', 'Special packets'), # DF heading

                 CorrectionTuple(754784, 'DE', 'Pakete von Dateisystem lesen', 'Sonderfunktionen'), # DA name
                 CorrectionTuple(754785, 'DE', 'Pakete von Dateisystem lesen', 'Sonderfunktionen')] # DF heading 


class ChangeMigrationModuleTranslations(ChangeDTPPacket):
    '''Change translations of migration relevant customizings that have changed'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 11324
    
    def fix(self, runner):
        failures = 0
        for i_text_nr, language, expected_value, new_value in I_TEXT_TUPLES:
            record = self.get_record(435, [i_text_nr, language], [1549])

            if not self.change_value(record, 'i_text', expected_value, new_value):
                failures += 1

        if failures:
            return self.fail()

        return self.success()


class SucceedOldFillerPacket(SucceedOldPacket):
    '''Mark the old FillMigrationruleTablePacket as done in favor of the new one'''
    wi_number = 11324

    def fix(self, runner):
        if self.mark_packet_as_done('planta_de.ppms.migration.startup.FillMigrationruleTable'):
            return self.success()

        return self.fail()
    
class SucceedOldCreateFolderPacket(SucceedOldPacket):
    '''Mark the old CreateFolderPacket as done in favor of the new one'''
    wi_number = 11324

    def fix(self, runner):
        if self.mark_packet_as_done('migration.hotfix.S_39_5_0.wi10294_create_folder_structure.CreateFolderPacket'):
            return self.success()

        return self.fail()

class RedefineMagicPacket(MigratePythonPacket):
    '''Change the valuerange of the is_magic dataitem to reflect the new framework'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 11324
    
    def fix(self, runner):
        fix = lambda _: """from ppms.migration import get_packet_instance_from_path

def computeOutput(di):
    # No reason to turn this into a computeSQLValueRange
    # there's like 5 valueranges in 314 that look up
    # the python path
    dtp_rec = di.get_dtp_record()
    pythonpath = dtp_rec.pythonpath.get_value()

    packet = get_packet_instance_from_path(pythonpath)
    return packet.special_snowflake

computeOutput.deps = ('DI062155', )"""

        if self.migrate_python('a9978dc74067628c8aeed4e5f60ee3c5', '69941a1e5d8870860f70d699df3314f1', 
                               self.CU_TYPE_DATAITEM, '062202', fix):
            return self.success()

        return self.fail()
