# -*- coding: utf-8 -*-
from ppms.migration import SQLPacket, ChangeDTPPacket, PacketCategory, MaxVersionDependency, VersionType, \
    PacketDependency, PacketState

PACKET_PATH = 'migration.hotfix.S_39_5_4.wi11533_port_universal_interface_VoE.PortCustomizingUniversalInterface'


class PortInterfaceTemplateNewDesign(SQLPacket):
    """Port new customizing for the PLANTA Link"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12999

    FILENAME = 'wi12999_interface_template_design.sql'

    def get_dependencies(self):
        return (PacketDependency(packet_path=PACKET_PATH, expected_state=PacketState.INSTALLED),
                MaxVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):
        self.apply_dml_file(self.sql_planta_de_dbms, self.FILENAME)

        return self.success()


class DeleteOldInterfaceCustomizing(ChangeDTPPacket):
    """Delete old, unused PLANTA Link customizing"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12999

    def get_dependencies(self):
        return (PacketDependency(packet_path=PACKET_PATH, expected_state=PacketState.INSTALLED),
                MaxVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):
        for da_assignment_number in (344438, 344437, 344183, 344181, 344176, 344158, 344150,
                                     344189, 344190, 344159, 344157):
            record = self.get_record(dt_num=406, key_list=[da_assignment_number], di_list=['id'])
            if record is None:
                continue

            self.log('Trying to delete it...')
            if not record.delete():
                return self.fail('Could not delete da assignment %d' % da_assignment_number)
            self.log('Deleted!')

        return self.success()


class PortTemplateBackground(ChangeDTPPacket):
    """Port the new backgrounds for templates to the PLANTA Link"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12999

    def get_dependencies(self):
        return (PacketDependency(packet_path=PACKET_PATH, expected_state=PacketState.INSTALLED),
                MaxVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):
        failures = 0

        for key_list in (['053817', 746920], ['053828', 745853]):
            record = self.get_record(dt_num=411, key_list=key_list, di_list=['traffic_light_di_id', 'effect_id'])
            if not self.change_value(record=record, attribute='traffic_light_di_id', old_value='', new_value='063242'):
                failures += 1

            if not self.change_value(record=record, attribute='effect_id', old_value='', new_value='0110004'):
                failures += 1

        if failures:
            return self.fail('Could not update all fields')

        return self.success()