from ppms.migration import SQLPacket, ChangeDTPPacket, MinVersionDependency, MaxVersionDependency, VersionType, \
    PacketCategory
from ppms import ppms


class PortResourceIdentChangeToVenusOnEarth(SQLPacket, ChangeDTPPacket):
    """
    ports the resource-ident-change from venus to venus on earth

    creates module 009CHY for running resource-ident-change
    changes customizing to call MOD 009CHY on buttons
    """

    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 14573

    fname = 'wi14573_port_res_ident_change_to_voe.sql'

    def get_dependencies(self):
        return (MinVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.4.4', version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):
        if not ppms.search_record(405, ['009CHY'], []):
            self.apply_dml_file(sql_dialect=self.sql_planta_de_dbms, filename=self.fname, split=True)

        start_button_record = self.get_record(411, ['000468', 101039], ['action'])
        self.change_value(
            record=start_button_record,
            attribute='action',
            old_value='009A64',
            new_value='009CHY'
        )

        preview_button_record = self.get_record(411, ['000468', 149671], ['action', 'df_customizing_name'])

        self.change_value(
            record=preview_button_record,
            attribute='action',
            old_value='009A84',
            new_value='009CHY'
        )

        self.change_value(
            record=preview_button_record,
            attribute='df_customizing_name',
            new_value='id_change_preview',
            old_value=None
        )

        self.success()