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

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


class PortLinkDocumentationTextConstant(SQLPacket):
    """Port a text constant for the PLANTA Link documentation"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13269

    FILENAME = 'wi13269_interface_documentation.sql'

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

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

        return self.success()


class PortLinkDocumentationSubclasses(ChangeDTPPacket):
    """Port module subclasses for the PLANTA Link documentation"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13269

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

    def fix(self, runner):
        failures = 0

        for module_id in ('009C7L', '009C45'):
            record = self.get_record(dt_num=405, key_list=[module_id], di_list=['python_module_subclass',
                                                                                'py_macro_text'])

            if not self.change_value(record=record, attribute='python_module_subclass', old_value='',
                                     new_value='ppms.interface.InterfaceParameterModule') and \
                    not self.change_value(record, 'python_module_subclass', 'ppms.module_subclasses.base_class.Base',
                                          'ppms.interface.InterfaceParameterModule'):
                failures += 1

            if not self.change_value(record=record, attribute='py_macro_text',
                                     old_value='ppms.get_target_module().menu(12)', new_value=''):
                failures += 1

        record = self.get_record(dt_num=405, key_list=['009C3I'], di_list=['python_module_subclass', 'py_macro_text'])
        if not self.change_value(record=record, attribute='python_module_subclass', old_value='',
                                 new_value='ppms.interface.InterfaceParameterModule') and \
                not self.change_value(record, 'python_module_subclass', 'ppms.module_subclasses.base_class.Base',
                                      'ppms.interface.InterfaceParameterModule'):
            failures += 1

        if failures:
            return self.fail()

        return self.success()