# -*- coding: utf-8 -*-
from distutils.version import StrictVersion

from ppms.migration import ChangeDTPPacket, PacketCategory, MaxVersionDependency

OLD_VALUE = """def get_effort_plan(di):

    rec = di.get_dtp_record()
    effort_plan = rec.effort_plan.get_value()
    if effort_plan > 0.0:
        return effort_plan
    return 0

def get_effort_act(di):

    rec = di.get_dtp_record()
    effort_act = rec.effort_act.get_value()
    if effort_act > 0.0:
        return effort_act
    return 0

def get_effort_rem(di):

    rec = di.get_dtp_record()
    effort_rem = rec.effort_rem.get_value()
    if effort_rem > 0.0:
        return effort_rem
    return 0    

def computeOutput(di):
    rec = di.get_dtp_record()
    duration=0.0
    # return new di value if load_profile is MAN
    if rec.load_profile.get_value() == "MAN": return di.get_value()
    
    # evaluate effort value
    if get_effort_act(di) > 0.0 or rec.actual_start.get_value() > 0:
        duration = get_effort_rem(di)
    else:
        duration = get_effort_plan(di)
    return duration

computeOutput.deps = ("DI001410","DI001416","DI001423","DI001420","DI001452")

def processInput(di, old_value):
    new_value = di.get_value()   
    rec = di.get_dtp_record()  
   
       
    #new_value=int(new_value)
    if get_effort_act(di) > 0.0 or rec.actual_start.get_value():
        rec.effort_rem.set_value(new_value)
    else:
        rec.effort_plan.set_value(new_value)
    

    return new_value

processInput.deps = ("DI001410","DI001416","DI001423","DI001420")"""

VOE_VALUE = """def get_effort_plan(di):

    rec = di.get_dtp_record()
    effort_plan = rec.effort_plan.get_value()
    if effort_plan > 0.0:
        return effort_plan
    return 0

def get_effort_act(di):

    rec = di.get_dtp_record()
    effort_act = rec.effort_act.get_value()
    if effort_act > 0.0:
        return effort_act
    return 0

def get_effort_rem(di):

    rec = di.get_dtp_record()
    effort_rem = rec.effort_rem.get_value()
    if effort_rem > 0.0:
        return effort_rem
    return 0    

def computeOutput(di):
    rec = di.get_dtp_record()
    duration=0.0
    # return new di value if load_profile is MAN
    if rec.load_profile.get_value() == "MAN": return di.get_value()
    
    # evaluate effort value
    if get_effort_act(di) > 0.0 or rec.actual_start.get_value() > 0:
        duration = get_effort_rem(di)
    else:
        duration = get_effort_plan(di)
    return duration

computeOutput.deps = ("DI001410","DI001416","DI001423","DI001420","DI001452")

def processInput(di, old_value):
    new_value = di.get_value()   
    rec = di.get_dtp_record()  
   
       
    #new_value=int(new_value)
    if get_effort_act(di) > 0.0 or rec.actual_start.get_value():
        rec.effort_rem.set_value(new_value)
    else:
        rec.effort_plan.set_value(new_value)
    

    return new_value
processInput.deps = ("DI001410","DI001416","DI001423","DI001420")"""

NEW_VALUE = """def get_effort_plan(di):

    rec = di.get_dtp_record()
    effort_plan = rec.effort_plan.get_value()
    if effort_plan > 0.0:
        return effort_plan
    return 0

def get_effort_act(di):

    rec = di.get_dtp_record()
    effort_act = rec.effort_act.get_value()
    if effort_act > 0.0:
        return effort_act
    return 0

def get_effort_rem(di):

    rec = di.get_dtp_record()
    effort_rem = rec.effort_rem.get_value()
    if effort_rem > 0.0:
        return effort_rem
    return 0

def computeOutput(di):
    rec = di.get_dtp_record()
    duration=0.0
    # return new di value if load_profile is MAN
    if rec.load_profile.get_value() == "MAN": return di.get_value()

    # evaluate effort value
    if get_effort_act(di) > 0.0 or rec.actual_start.get_value() > 0:
        duration = get_effort_rem(di)
    else:
        duration = get_effort_plan(di)
    return duration

computeOutput.deps = ("DI001410","DI001416","DI001423","DI001420","DI001452")

def processInput(di, old_value):
    new_value = di.get_value()
    act_value = get_effort_act(di)
    rec = di.get_dtp_record()  

    if act_value > 0.0 or rec.actual_start.get_value():
        # juba 04062018 (WI 22175): Scheduling would get in trouble if planned effort is not set
        # -> Handle this just like it would look if we had first set remaining effort, and, THEN, some actual effort later
        rec.effort_plan.set_value(new_value + act_value)
    else:
        rec.effort_plan.set_value(new_value)

    return new_value

processInput.deps = ("DI001410","DI001416","DI001423","DI001420")"""


class AlwaysSetPlannedEffortByRemainingEffort(ChangeDTPPacket):
    """
    Changing of remaining effort of resource assignment always sets its planned effort, even if resource assignment already has actual efforts (or actual date) set
    """
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 22175

    def fix(self, runner):
        current_release = MaxVersionDependency.get_customizing_version()
        record = self.get_record(dt_num=412, key_list=['001423'], di_list=['valuerange'])

        if current_release <= StrictVersion('39.4'):  # Venus on Earth
            if not self.change_value(record=record, attribute='valuerange', old_value=VOE_VALUE, new_value=NEW_VALUE):
                return self.fail('Could not change the value range! Was it individualized? Please merge manually!')

        else:  # Venus
            if not self.change_value(record=record, attribute='valuerange', old_value=OLD_VALUE, new_value=NEW_VALUE):
                return self.fail('Could not change the value range! Was it individualized? Please merge manually!')

        return self.success()
