# -*- coding: utf-8 -*-
import os

from ppms import ppms
from ppms.migration.packet import *


class PortCustomizingUniversalInterface(SQLPacket):
    '''Port the necessary sql statements for Universal Interface VoE'''
    version = 1
    done_after_success = True
    run_on_startup = False
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 11533

    fname = 'wi11533_universal_interface_voe.sql'

    def fix(self, runner):
        sqlpath = self.get_sql_file_path(self.sql_planta_de_dbms, self.fname)
        queries = self.get_queries_from_file(sqlpath)

        old_state = ppms.db_get_auto_commit()
        ppms.db_set_auto_commit(False)
        
        try:
            self.modify_and_log("DELETE DT510 WHERE DI010320 = '01100043'")
            
            for query in queries:
                self.modify_and_log(query)

            self.modify_and_log(r"UPDATE INTERFACE_PARAMETER SET VALUE = '\' WHERE VALUE = '\\'") 
            
            ppms.db_commit()
        except:
            ppms.db_rollback()
            raise
        finally:
            ppms.db_set_auto_commit(old_state)
            
        return self.success()
        
        
class CreateTablesUniversalInterface(SQLPacket):
    '''New tables for universal interface VoE'''
    version = 1
    done_after_success = True
    run_on_startup = False
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 11533

    fname = 'wi11533_universal_interface_tables.sql'

    def fix(self, runner):
        sqlpath = self.get_sql_file_path(self.sql_planta_de_dbms, self.fname)
        queries = self.get_queries_from_file(sqlpath)
        
        for query in queries:
            self.modify_and_log(query)

        return self.success()
        
        
class InsertNewInterfaceOLEs(ChangeDTPPacket):
    '''Inserting new OLEs for the Universal Interface'''
    version = 1
    done_after_success = True
    run_on_startup = False
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 11533

    def fix(self, runner):
        FILES = {'001360': 'link.png',
                 '001361': '0320-tools.png',
                 '001362': '0187-file_room.png',
                 '001363': 'copy_small.png'}
        
        errors = 0
        
        for ident, filename in FILES.items():
            f_path = os.path.join(os.path.dirname(__file__),filename)
            ole_rec = ppms.search_record(31, [ident], [26169], True)

            hl_di = ole_rec.standard_ole
            if not hl_di and not hl_di.is_hyperlink():
                errors += 1

            # Save ole as a hyperlink
            hyperlink = hl_di.get_hyperlink()
            if not hyperlink:
                errors += 1
            
            with open(f_path,'rb') as f:
                barray = bytearray(f.read())
                assert hyperlink.set_value(binary=barray, url=filename)
                assert ole_rec.save()
                        
        if errors:
            return self.fail()
        return self.success()
        