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

NEW_VALUERANGE = """def checkInput(di, oldvalue):
    value = di.get_value()

    if not value:
        return True

    rec_405 = ppms.search_record(405, [value], ['id'])
    if rec_405 is None:
        return False

    return True"""


class PortInterfaceSubclassIndicator(ChangeDTPPacket, SQLPacket):
    '''Port the subclass validity indicators and valueranges to VoE'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12337

    fname = 'WI12337_interface_subclass_indicator.sql'

    def get_dependencies(self):
        return DEFAULT_CUSTOMIZING_DEPENDENCY

    def fix(self, runner):
        # Port the SQL stuff

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

        # Add the new valueranges

        # Source Module
        record = self.get_record(412, ['062624'], ['valuerange', 'wb_type'])
        if not self.change_value(record, 'valuerange', '', NEW_VALUERANGE):
            return self.fail()

        if not self.change_value(record, 'wb_type', '', 'PY'):
            return self.fail()

        # Pool Module
        record = self.get_record(412, ['062767'], ['valuerange', 'wb_type'])
        if not self.change_value(record, 'valuerange', '', NEW_VALUERANGE):
            return self.fail()

        if not self.change_value(record, 'wb_type', '', 'PY'):
            return self.fail()

        # Target Module
        record = self.get_record(412, ['062625'], ['valuerange', 'wb_type'])
        if not self.change_value(record, 'valuerange', '', NEW_VALUERANGE):
            return self.fail()

        if not self.change_value(record, 'wb_type', '', 'PY'):
            return self.fail()

        # Add the traffic lights

        # Source Module
        record = self.get_record(411, ['053828', 746426], ['traffic_light_di_id'])
        if not self.change_value(record, 'traffic_light_di_id', '', '063181'):
            return self.fail()

        # Pool Module
        record = self.get_record(411, ['053828', 746428], ['traffic_light_di_id'])
        if not self.change_value(record, 'traffic_light_di_id', '', '063182'):
            return self.fail()

        # Target Module
        record = self.get_record(411, ['053828', 746427], ['traffic_light_di_id'])
        if not self.change_value(record, 'traffic_light_di_id', '', '063183'):
            return self.fail()

        return self.success()
