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

OLD_CODE = {'sql': r"""SELECT DI024148 from DT500 WHERE DI024150= '{0}' AND (DI024149 IS NULL OR DI024149 IN ('{1}', '-'))""",
            'macro': r"""from ppms import ppms_cu

#Macro module
macro_obj = ppms.get_macro_module()

#Modul module
mod_obj = ppms.get_target_module() #invoker
active_mod= ppms.get_active_module().get_id()

#Get module
mod_no = macro_obj.get_invoker_rec().mod_id.get_text_value()

#Check if Modul id is available
if mod_no == '':
    ppms.ui_message_id('0161')
elif len(ppms.db_select(ppms.get_query("000269").format(mod_no, ppms.uvar_get('@1')))) == 0:
    ppms.ui_message_id('0326')
else:
    #Create object of ppms_cu.Action class
    ppms_cu_action = ppms_cu.Action(mod_obj)

    #Create sub_mod_dictionary
    sub_mod_dict={}
    sub_mod_dict['009A7X'] = ["bottom",0.4,{'@L4':[mod_no]}]

    #Call submodule
    ppms_cu_action.call_SUB(sub_mod_dict=sub_mod_dict)"""}
NEW_CODE = {'sql': r"""SELECT count(*) from DT500 WHERE DI024150= '{0}'""",
            'macro': r"""from ppms import ppms_cu

#Macro module
macro_obj = ppms.get_macro_module()

#Modul module
mod_obj = ppms.get_target_module() #invoker
active_mod= ppms.get_active_module().get_id()

#Get module
mod_no = macro_obj.get_invoker_rec().mod_id.get_text_value()

#Check if Modul id is available
if mod_no == '':
    ppms.ui_message_id('0161')
elif len(ppms.db_select(ppms.get_query("000269").format(mod_no))) == 0:
    ppms.ui_message_id('0326')
else:
    #Create object of ppms_cu.Action class
    ppms_cu_action = ppms_cu.Action(mod_obj)

    #Create sub_mod_dictionary
    sub_mod_dict={}
    sub_mod_dict['009A7X'] = ["bottom",0.4,{'@L4':[mod_no]}]

    #Call submodule
    ppms_cu_action.call_SUB(sub_mod_dict=sub_mod_dict)"""}

class FixViewMVsButton(ChangeDTPPacket):
    """Removes user check from "View MVs" button in module customizing
    """
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13779

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

    def fix(self, runner):
        sql_record = self.get_record(343, ['-', '000269'], ['source_text_clob'])
        macro_record = self.get_record(405, ['009A7Z'], ['py_macro_text'])

        if not self.change_value(record=sql_record, attribute='source_text_clob', old_value=OLD_CODE['sql'], new_value=NEW_CODE['sql']):
            return self.fail('Could not change code of SQL statement 000269 - maybe it was individualized?')

        if not self.change_value(record=macro_record, attribute='py_macro_text', old_value=OLD_CODE['macro'], new_value=NEW_CODE['macro']):
            return self.fail('Could not change macro code code of MOD009A7Z - maybe it was individualized?')

        return self.success()