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


class PortInterfaceConfigurationListbox(SQLPacket, ChangeDTPPacket):
    '''Port the new Interface configuration listbox to VoE '''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12462

    fname = 'WI12462_interface_listbox.sql'

    DBMS_QUERY = {'000002': 'alter table INTERFACE_CONFIG add COPIED_ON_TIME varchar2(80 char)',
                  '000004': 'alter table INTERFACE_CONFIG add COPIED_ON_TIME varchar(80)'}

    def get_dependencies(self):
        return DEFAULT_CUSTOMIZING_DEPENDENCY

    def fix(self, runner):
        query = self.DBMS_QUERY[ppms.db_get_dbms_name()]
        self.modify_and_log(query)

        self.apply_dml_file(self.sql_planta_de_dbms, self.fname)

        # Configuration
        record = self.get_record(411, ['053828', 745459], ['lb_module'])
        if not self.change_value(record, 'lb_module', '009C2V', '009CC5'):
            return self.fail()

        # Transaction
        record = self.get_record(411, ['053842', 745520], ['lb_module'])
        if not self.change_value(record, 'lb_module', '009C2V', '009CC5'):
            return self.fail()

        # Archive
        record = self.get_record(411, ['053994', 745459], ['lb_module'])
        if not self.change_value(record, 'lb_module', '009C65', '009CC6'):
            return self.fail()

        # Delete the old listboxes
        record = self.get_record(405, ['009C2V'], ['id'])
        record.delete()

        record = self.get_record(405, ['009C65'], ['id'])
        record.delete()

        return self.success()
