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


class ReplaceStatusReportCustomizing(ChangeDTPPacket):
    """Replace the existing status report customizing with module subclasses

    If you have individual status report customizing they need to be integrated
    into the new API
    """
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13736

    MODULE_IDS_FOR_SUBCLASS = ('0099JU', '009A52', '009AHR', '009BF3')
    MODULE_SUBCLASS = 'ppms.module_subclasses.report.ProjectReportModule'

    DA_FIELDNUMBER_PAIRS = (  # Revoke
                              ('045874', 713512, '009AHY', 'revoke_report'),
                              ('045874', 713513, '009AHY', 'revoke_report'),
                              # Create
                              ('046104', 719443, '009A0Q', 'create_report'),
                              ('046104', 727810, '009A0Q', 'create_report'),
                              ('046104', 727811, '009A0Q', 'create_report'),
                              # Approve
                              ('045482', 711856, '009ADK', 'approve_report'))

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

    def add_subclass_to_modules(self):
        for module_id in self.MODULE_IDS_FOR_SUBCLASS:
            record = self.get_record(dt_num=405, key_list=[module_id], di_list=['python_module_subclass'])
            if not self.change_value(record=record, attribute='python_module_subclass',
                                     old_value='', new_value=self.MODULE_SUBCLASS) and \
                    not self.change_value(record, 'python_module_subclass', 'ppms.module_subclasses.base_class.Base',
                                          self.MODULE_SUBCLASS):
                return self.fail('Could not add module subclass to module "%s" - was it individualized?' % module_id)

        return True

    def replace_macros_with_new_action(self):
        for dataarea_id, fieldnumber, old_value, datafield_config in self.DA_FIELDNUMBER_PAIRS:
            record = self.get_record(411, key_list=[dataarea_id, fieldnumber], di_list=['action', 'df_script_settings'])
            if not self.change_value(record=record, attribute='action',
                                     old_value=old_value, new_value='009CF2'):
                return self.fail('Could not update action module - was it individualized?')

            if not self.change_value(record=record, attribute='df_script_settings',
                                     old_value='', new_value=datafield_config):
                return self.fail('Could not update datafield config - was it individualized?')

        return True

    def fix(self, runner):
        if self.add_subclass_to_modules() is None:
            return

        if self.replace_macros_with_new_action() is None:
            return

        return self.success()


class PortStatusReportTranslations(SQLPacket, ChangeDTPPacket):
    """Port the new translations for the status report customizing"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13736

    FILENAME = 'wi13736_report_api.sql'
    DA_FIELDNUMBER_PAIRS = ((713512, 802078, 755386),
                            (713513, 802079, 802080))

    UPDATE_HEADING_QUERY = """UPDATE DT411 SET DI010200 = {number} WHERE DI000306 = '045874' AND DI000307 = {fieldnumber}"""
    UPDATE_TOOLTIP_QUERY = """UPDATE DT411 SET DI040918 = {number} WHERE DI000306 = '045874' AND DI000307 = {fieldnumber}"""

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

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

        for fieldnumber, heading, tooltip in self.DA_FIELDNUMBER_PAIRS:
            self.modify_and_log(self.UPDATE_HEADING_QUERY.format(number=heading, fieldnumber=fieldnumber))
            self.modify_and_log(self.UPDATE_TOOLTIP_QUERY.format(number=tooltip, fieldnumber=fieldnumber))

        return self.success()