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

NEW_CODE = r"""def executeProcessAction(mts_rec=None,object_id=None):
    from ppms.processrules import approve_process_phase

    approve_process_phase(project_id=object_id, mts_record=mts_rec)

    return True

ppms.get_planta_session_dict()["planta_functions"]=[executeProcessAction]"""

OLD_CODE = r"""def executeProcessAction(mts_rec=None,object_id=None):
    from ppms import ppms_cu
    from ppms import processes
    from ppms import project_rights
    import imp
    imp.reload(processes)

    # Moduldaten
    mod_obj=ppms.get_target_module()
    mod_obj.save_and_actualise()

    pr_id=object_id
    step_id=mts_rec.get_dtp_record().step_id.get_value()
    object_type=mts_rec.get_dtp_record().object_type.get_value()
    phase=mts_rec.get_dtp_record().phase.get_value()

    rec461=ppms.search_record(461, [pr_id], [1001,1052,1003,23220,264,29448,41200], True)
    rights=project_rights.get_project_rights(pr_id)

    if rights == 0:
        ppms.ui_message_id("0051")
    else:
        #Check for Devlivery date
        if processes.get_class_step(mod_obj,"c1",phase).delivery_date.get_raw_value() == 0:
            ppms.ui_message_id("0773")
        else:
            #Check state of child records of delivery step
            child_recs = processes.get_step_child_ids(step_id,object_type,object_id)
            open_child_steps=0
            for child in child_recs:
                rec499=ppms.search_record(499, [child[0]], [58037], True)
                if rec499.step_status.get_value() in (214,):
                    open_child_steps+=1

            if open_child_steps > 0:
                ppms.ui_message_id("0770")
            else:
                # Create option message
                title=ppms_cu.Helper.get_const_title("001174")
                ole_id=ppms_cu.Helper.get_global_setting("ole_question_mark").alpha120.get_value()
                button_list=[ppms_cu.Helper.get_const_title("001239"),ppms_cu.Helper.get_const_title("001240"),ppms_cu.Helper.get_const_title("001241"),ppms_cu.Helper.get_const_title("000884")]
                message=ppms_cu.Helper.get_const_title("001238")
                ppms.ui_message_box(title,message,1,ole_id,button_list)
                msg_obj=ppms.msg_pop()

                # Approval
                if msg_obj.get_reply() == 1:
                    mts_rec.step_status.set_raw_value(219)
                    processes.set_date_for_steps_of_phase(mod_obj,phase,"c3")
                    mod_obj.menu(34)

                    next_phase=processes.get_next_phase(pr_id)
                    processes.set_new_active_phase(pr_id,next_phase)

                    processes.send_message(mod_obj,pr_id,ppms_cu.Helper.get_const_title("001176"))

                    mod_obj.refresh_module()

                    processes.execute_phase_action(phase,mts_rec,pr_id)

                # Decline
                elif msg_obj.get_reply() == 2:
                    continue_process=True
                    if mts_rec.comment.get_raw_value() == "":
                        ppms.ui_message_id("0761")
                        if ppms.msg_pop().get_reply() == 1:
                            mts_rec.comment.focus()
                            continue_process=False

                    if continue_process == True:
                        processes.set_finished_refused(mod_obj,phase)
                        processes.set_date_for_steps_of_phase(mod_obj,phase,"c2")
                        processes.set_phasestatus(object_id,0)
                        mod_obj.menu(34)

                        processes.send_message(mod_obj,pr_id,ppms.search_record(425, ["001177"], [296], True).title.get_value())

                        mod_obj.refresh_module()

                # Abort project
                elif msg_obj.get_reply() == 3:
                    #+mts_rec.step_status.set_raw_value(219)
                    #processes.set_date_for_steps_of_phase(mod_obj,phase,"c3")
                    mod_obj.menu(34)
                    processes.abort_project(pr_id,mts_rec)
                    mod_obj.menu(19)

                    processes.send_message(mod_obj,pr_id,ppms.search_record(425, ["001192"], [296], True).title.get_value())

                    #Close project workflow
                    #ppms.ui_message_id("0708")
                    #mod_obj.get_panel().get_main_module().menu(49)

    return True

ppms.get_session_dict()["planta_functions"]=[executeProcessAction]"""


class ReplaceApprovalProcessStepCode(ChangeDTPPacket):
    """Replace the approval step code in the process model"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13733

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

    def fix(self, runner):
        record = self.get_record(dt_num=489, key_list=['10'], di_list=['sql_rule'])
        if not self.change_value(record=record, attribute='sql_rule', old_value=OLD_CODE, new_value=NEW_CODE):
            return self.fail('Could not change process rule 10 - was it individualized?')

        return self.success()