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

DELETE_STATEMENTS = ["DELETE FROM DT435 WHERE DI023517 = '001245' AND DI001538 IN (SELECT DI001245 FROM DT453 WHERE DI000916 IN ('00000132'))",
                     "DELETE FROM DT435 WHERE DI023517 = '001251' AND DI001538 IN (SELECT DI001251 FROM DT454 WHERE DI001065 IN ('00000132'))",
                     "DELETE FROM DT435 WHERE DI023517 = '001217' AND DI001538 IN (SELECT DI001217 FROM DT454 WHERE DI001065 IN ('00000132'))",
                     "DELETE FROM DT435 WHERE DI023517 = '001172' AND DI001538 IN (SELECT DI001172 FROM DT454 WHERE DI001065 IN ('00000132'))",
                     "DELETE FROM DT435 WHERE DI023517 = '041276' AND DI001538 IN (SELECT DI041276 FROM DT447 WHERE DI041284 IN ('00000132'))",
                     "DELETE FROM DT435 WHERE DI023517 = '041277' AND DI001538 IN (SELECT DI041277 FROM DT447 WHERE DI041284 IN ('00000132'))",
                     "DELETE FROM DT447 WHERE DI041284 = '00000132' AND DI041264 = 541",
                     "DELETE FROM DT435 WHERE DI023517 = '001566' AND DI001538 IN (SELECT DI001566 FROM DT428 WHERE DI000357 = '1171')",
                     "DELETE FROM DT435 WHERE DI023517 = '001569' AND DI001538 IN (SELECT DI001569 FROM DT428 WHERE DI000357 = '1171')",
                     "DELETE FROM DT435 WHERE DI023517 = '026382' AND DI001538 IN (SELECT DI026382 FROM DT428 WHERE DI000357 = '1171')",
                     "DELETE FROM DT428 WHERE DI000357 = '1171'"]


class PortStatusbarMessage(SQLPacket):
    """Ports the new "permanent status bar" message"""
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 18496

    filename = 'wi18496_port_statusbar_message.sql'

    def get_dependencies(self):
        return (MaxVersionDependency(version='39.5.9'), )

    def delete_old_data(self):
        self.log_heading('Wiping data that could cause conflicts')

        for statement in DELETE_STATEMENTS:
            self.modify_and_log(statement)

    def insert_new_data(self):
        self.log_heading('Inserting new data')
        self.apply_dml_file(self.sql_planta_de_dbms, self.filename)

    def fix(self, runner):
        with ppms.autocommit_disabled():
            self.delete_old_data()
            self.insert_new_data()

        return self.success()
