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


OLD_CODE = """from ppms import ppms_cu
from ppms import project_rights

def mark_steps_by_id(inv_recs,inv_type="mts_recs",df_focus=None):
    record_dict={}
    ppms.echo_off()

    for rec in mod_obj.steps_current.get_records():
        rec.unmark()
        record_dict[rec.step_id.get_raw_value()]=rec

    if inv_type == "mts_recs":
        for rec in inv_recs:
            if rec.step_id.get_raw_value() in record_dict.keys():
                record_dict[rec.step_id.get_raw_value()].mark()
    elif inv_type == "ids":
        if df_focus != None and len(inv_recs) == 1:
            ppms.echo_on()
            record_dict[inv_recs[0]].__getattr__(df_focus).focus()
        else:
            for rec in inv_recs:
                record_dict[rec].mark()

def refresh_module():
    mod_obj.menu(19)

def save_and_actualise():
    mod_obj.menu(34)
    mod_obj.menu(22)

mod_obj=ppms.get_target_module()

#Define data area settings
right_setting={}
#watcher
right_setting[0]=[]

def set_init_model_to_output():
    ppms.echo_off()
    recs=mod_obj.init_model.get_records()
    for rec in recs:
        if rec.pm_model.get_raw_value() != "":
            pr_id=rec.pr_id.get_raw_value()
            pm_model=rec.pm_model.get_raw_value()
            check_db = ppms.db_select(ppms.get_query("000259").format(pr_id,pm_model))
            if check_db [0][0] > 0:
                mod_obj.init_model.get_customizing().set_to_output()
                mod_obj.init_model.get_customizing().button_activate_model.hide()
            else:
                mod_obj.init_model.get_customizing().set_to_input()
                mod_obj.init_model.get_customizing().button_activate_model.show(1)
        else:
            mod_obj.init_model.get_customizing().set_to_input()
            mod_obj.init_model.get_customizing().button_activate_model.show(1)
    ppms.echo_on()

def on_load():

    mod_obj.refresh_module = refresh_module
    mod_obj.save_and_actualise = save_and_actualise
    mod_obj.mark_steps_by_id = mark_steps_by_id
    project_rights.exec_project_rights(mod_obj,right_setting)

    refresh_module()


def on_initial_focus():
    ppms.echo_off()
    set_init_model_to_output()
    ppms.echo_on()

def on_focus():
    set_init_model_to_output()
    project_rights.exec_project_rights(mod_obj,right_setting)
    mod_obj.menu(22)


def on_reset():
    project_rights.exec_project_rights(mod_obj,right_setting)
    mod_obj.menu(12)
    set_init_model_to_output()"""
SUBCLASS_PATH = 'ppms.module_subclasses.processes.ProcessStatusModule'


class ReplaceProcessModelMacroWithSubclass(ChangeDTPPacket):
    """Replace the process model macro with a subclass"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13738

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

    def fix(self, runner):
        record = self.get_record(dt_num=405, key_list=['0099V3'], di_list=['py_macro_text', 'python_module_subclass'])
        if not self.change_value(record=record, attribute='py_macro_text', old_value=OLD_CODE, new_value=''):
            return self.fail('Could not change macro 0099V3 - was it individualized?')

        if not self.change_value(record=record, attribute='python_module_subclass', old_value='',
                                 new_value=SUBCLASS_PATH) and \
                not self.change_value(record, 'python_module_subclass', 'ppms.module_subclasses.base_class.Base',
                                      SUBCLASS_PATH):
            return self.fail('Could not change module subclass for module 0099V3 - was it individualized?')

        return self.success()
