# -*- coding: utf-8 -*-

from ppms import ppms
from ppms.migration.packet import *
from ppms.migration.utilities import *
from ppms.migration import SinglePacketRunner

class SetOldInterfacePacketAsSucceeded(SucceedOldPacket):
    '''Mark the old interface package as done in favor of the new one'''
    wi_number = 11942
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    
    PACKET_PATHS = ['migration.hotfix.S_39_5_2.wi11286_port_fixed_SAP_interface_to_VoE.ChangeButtonNameOfImportModule',
                    'migration.hotfix.S_39_5_2.wi11286_port_fixed_SAP_interface_to_VoE.PortSQLStatementsSAPInterface',
                    'migration.hotfix.S_39_5_2.wi11286_port_fixed_SAP_interface_to_VoE.UpdatefixesInSAPInterfaceMacroModulesVoE']
                         
    def fix(self, runner):
        errors = 0
        
        for packet_path in self.PACKET_PATHS:
            if not self.mark_packet_as_done(packet_path):
                errors = 1
                
        if errors != 0:
            return self.fail()
        return self.success()
        
class SetNewInterfaceMandatory(BasePacket):
    '''Set the new interface to MANDATORY'''
    wi_number = 11942
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    
    PACKET_PATHS = ['migration.hotfix.S_39_5_0.wi10139_customizer_flag.CreateCustomizerFlagPacket',
                    'migration.hotfix.S_39_5_4.wi11533_port_universal_interface_VoE.CreateTablesUniversalInterface',
                    'migration.hotfix.S_39_5_4.wi11533_port_universal_interface_VoE.PortCustomizingUniversalInterface',
                    'migration.hotfix.S_39_5_4.wi11533_port_universal_interface_VoE.InsertNewInterfaceOLEs']
    
    def fix(self, runner):
        errors = 0
        
        for packet_path in self.PACKET_PATHS:
            packet = get_packet_instance_from_path(packet_path)
            uuid = get_uuid_from_attributes(packet, packet_path)
                 
            rec314 = ppms.search_record(314,[uuid], [62157], True)
               
            if not rec314.done.get_value():
                runner = SinglePacketRunner(uuid)
                runner.run()
            
            if not rec314.done.get_value():
                self.log('Packet %s failed\n' % packet_path)
                errors = 1
            else:
                self.log('Packet %s successful' % packet_path)
    
        if errors != 0:
            return self.fail()
        return self.success()