# -*- 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 hand_in

    hand_in(object_id, 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

    if mts_rec.delivery_date.get_raw_value() == 0:
        # Moduldaten
        mod_obj=ppms.get_target_module()

        # Action in process module """
        mod_obj.save_and_actualise()

        pr_id=object_id
        rights=project_rights.get_project_rights(pr_id)
        continue_process=True

        if rights == 0:
            ppms.ui_message_id("0051")
        else:
            #Read current settings
            phase=mts_rec.get_dtp_record().phase.get_value()

            #Get step "finished" ressource
            if processes.get_class_step(mod_obj,"c3",phase).res_id.get_text_value() == "":
                ppms.ui_message_id("0327")
            else:
                open_items=processes.set_finished_delivered(mod_obj,phase)
                if open_items == 0:
                    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:
                        mts_rec.step_status.set_raw_value(219)
                        processes.set_date_for_steps_of_phase(mod_obj,phase,"c1")
                        processes.set_phasestatus(pr_id,1)
                        mod_obj.menu(34)
                        mod_obj.refresh_module()

                        processes.send_message(mod_obj,pr_id,ppms_cu.Helper.get_const_title("001271"),recipients=[processes.get_class_step(mod_obj,"c3",phase).res_id.get_text_value()])

                else:
                    ppms.ui_message_id("0171")
    else:
        ppms.ui_message_id("0777")

    return True

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


class ReplaceHandInProcessStepCode(ChangeDTPPacket):
    """Replace the hand in step code in the process model"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13920

    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=['2'], 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 2 - was it individualized?')

        return self.success()