# -*- coding: utf-8 -*-
from ppms.migration.packet import *
from . import DEFAULT_CUSTOMIZING_DEPENDENCY

NEW_VALUE = """import time

def processInput(di, oldValue):
    value = di.get_value()
    dtp_record = di.get_dtp_record()

    transferred_on = dtp_record.transferred_on

    if value:
        transferred_on.set_value(str(time.time()))
    else:
        transferred_on.set_value('')

    return value

processInput.deps = ('DI062825',)"""


class PortFinishedValueRange(ChangeDTPPacket):
    '''Port the new finished valuerange from Venus'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12043

    def get_dependencies(self):
        return DEFAULT_CUSTOMIZING_DEPENDENCY

    def fix(self, runner):
        record = self.get_record(412, ['062768'], [1990, 27352])
        if not self.change_value(record, 'valuerange', '', NEW_VALUE):
            return self.fail()

        if not self.change_value(record, 'wb_type', '', 'PY'):
            return self.fail()

        return self.success()
