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

NEW_CODE = r"""from ppms import ppms_cu
from ppms import project_rights

def check_source(inv_rec):
    from ppms import ppms_cu
    mapping_dict={}
    mapping_dict["465"]=["pr_successor_id","task_successor_id"]

    dt_id=ppms_cu.Access.get_dt_id_of_ddi(inv_rec.get_da())
    if dt_id in mapping_dict.keys():
        return mapping_dict[dt_id]
    else:
        return ["pr_id","task_id"]

# Moduldaten
mod_obj=ppms.get_target_module()

# invoker
inv_rec=ppms.get_context_df().get_record()

source_attributes=check_source(inv_rec)
pr_id_attribute=source_attributes[0]
task_id_attribute=source_attributes[1]

# Get invoker values
if hasattr(inv_rec, 'get_dtp_record'):
    pr_id = inv_rec.get_dtp_record().get_di(pr_id_attribute).get_value()
    task_id=inv_rec.get_dtp_record().get_di(task_id_attribute).get_value()
else:
    pr_id=inv_rec.get_df(pr_id_attribute).get_raw_value()
    task_id=inv_rec.get_df(task_id_attribute).get_raw_value()

# Module call settings
target=ppms_cu.Helper.get_global_setting("edit_task").alpha120.get_value()
skip_macro=0
forced_status=2
dock_to=-1
dock_style=3
dock_proportion=0.45
foreground=1
auto_hide=0

# Procedure MOD-PANELS
open_mods = ppms_cu.Access(mod_obj).get_submodules()

# set @D678
d_678 = ppms.uvar_get('@D678')
if d_678 == '':
    ppms.uvar_set('@D678', pr_id)
else:
    if d_678 != pr_id:
        ppms.uvar_set('@D678', pr_id)

if pr_id and task_id:
    if target in open_mods.keys():
        # reset task module, which may have been changed to output from
        # project rights execution for the last task shown
        open_mods[target].reset_cu()
        open_mods[target].show_task(pr_id,task_id)
    else:
        target_mod_obj=ppms.get_target_module().open_module(target,skip_macro,forced_status,dock_to,dock_style,dock_proportion,foreground,auto_hide)
        target_mod_obj.show_task(pr_id,task_id)"""

OLD_CODE = r'''
from ppms import ppms_cu
from ppms import project_rights

def check_source(inv_rec):
    from ppms import ppms_cu
    mapping_dict={}
    mapping_dict["465"]=["pr_successor_id","task_successor_id"]

    dt_id=ppms_cu.Access.get_dt_id_of_ddi(inv_rec.get_da())
    if dt_id in mapping_dict.keys():
        return mapping_dict[dt_id]
    else:
        return ["pr_id","task_id"]

" Moduldaten "
mod_obj=ppms.get_target_module()

" invoker "
inv_rec=ppms.get_context_df().get_record()

source_attributes=check_source(inv_rec)
pr_id_attribute=source_attributes[0]
task_id_attribute=source_attributes[1]

" Get invoker values "
try:
    pr_id=inv_rec.get_dtp_record().__getattr__(pr_id_attribute).get_value()
    task_id=inv_rec.get_dtp_record().__getattr__(task_id_attribute).get_value()
except:
    pr_id=inv_rec.__getattr__(pr_id_attribute).get_raw_value()
    task_id=inv_rec.__getattr__(task_id_attribute).get_raw_value()

" Module call settings "
target=ppms_cu.Helper.get_global_setting("edit_task").alpha120.get_value()
skip_macro=0
forced_status=2
dock_to=-1
dock_style=3
dock_proportion=0.45
foreground=1
auto_hide=0

" Procedure MOD-PANELS "
open_mods = ppms_cu.Access(mod_obj).get_submodules()

if target in open_mods.keys():
    " reset task module, which may have been changed to output from "
    " project rights execution for the last task shown "
    open_mods[target].reset_cu()
    open_mods[target].show_task(pr_id,task_id)
else:
    target_mod_obj=ppms.get_target_module().open_module(target,skip_macro,forced_status,dock_to,dock_style,dock_proportion,foreground,auto_hide)
    target_mod_obj.show_task(pr_id,task_id)'''


class ReplaceEditTaskMacro(ChangeDTPPacket):
    """Replace the edit task macro, fixing a bug"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 14133

    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=['009AHC'], di_list=['py_macro_text'])
        if not self.change_value(record=record, attribute='py_macro_text', old_value=OLD_CODE, new_value=NEW_CODE):
            return self.fail('Could not change macro code - was it individualized?')

        return self.success()