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

NEW_VALUERANGE = """def get_counter_state_record(current_rec=None):
    dtpRec = current_rec

    #dataitem information for counter state
    lic = dtpRec.license_id.get_value()
    di_id = dtpRec.di_id.get_value()

    #get db schema settings
    dt_id = ppms.search_record(412, [di_id], [239], True).dt.get_value()
    dt_rec = ppms.search_record(415, [dt_id], [266, 60097], True)
    dt_enabled = dt_rec.enabled.get_value()
    if not dt_enabled:
        return None
    db_schema_id = dt_rec.db_schema_id.get_value()
    db_schema_counter_di_id = ppms.search_record(420, [db_schema_id], [8149], True).di_counter_val_di.get_value()

    db_schema_counter_dt_rec = ppms.search_record(412, [db_schema_counter_di_id], [239], True)

    if not db_schema_counter_dt_rec:
        return None

    db_schema_counter_dt_id = db_schema_counter_dt_rec.dt.get_value()

    db_schema_counter_dt_di_list = ppms.db_select(ppms.get_query("000240").format(db_schema_counter_dt_id))
    dependencies=[]
    [dependencies.append(int(di_rec[0])) for di_rec in db_schema_counter_dt_di_list]

    #get counter state record
    if db_schema_id in ["P70B","K000"]:
        crec = None
    else:
        crec = ppms.search_record(int(db_schema_counter_dt_id), [lic, di_id], dependencies, True)

    return crec

def processInput(di, oldvalue):
    rec = di.get_dtp_record()
    changed=False

    counter_record=get_counter_state_record(rec)
    if counter_record:
        counter_record.counter_value_1.set_value(di.get_value())
        changed=counter_record.save()

    if not changed: return oldvalue

    return di.get_value()

processInput.deps = ("*",)

def computeOutput(di):
    dtpRec = di.get_dtp_record()

    rec=get_counter_state_record(dtpRec)

    if rec:
        try:
            count = rec.counter_value_1.get_value()
            return count
        except:
            return ''
    else:
        return '0'

computeOutput.deps = ('*',)"""

OLD_VALUERANGE = """def get_counter_state_record(current_rec=None):
    dtpRec = current_rec

    #dataitem information for counter state
    lic = dtpRec.license_id.get_value()
    di_id = dtpRec.di_id.get_value()

    #get db schema settings
    dt_id = ppms.search_record(412, [di_id], [239], True).dt.get_value()
    db_schema_id = ppms.search_record(415, [dt_id], [266], True).db_schema_id.get_value()
    db_schema_counter_di_id = ppms.search_record(420, [db_schema_id], [8149], True).di_counter_val_di.get_value()

    db_schema_counter_dt_rec = ppms.search_record(412, [db_schema_counter_di_id], [239], True)

    if not db_schema_counter_dt_rec:
        return None

    db_schema_counter_dt_id = db_schema_counter_dt_rec.dt.get_value()

    db_schema_counter_dt_di_list = ppms.db_select(ppms.get_query("000240").format(db_schema_counter_dt_id))
    dependencies=[]
    [dependencies.append(int(di_rec[0])) for di_rec in db_schema_counter_dt_di_list]

    #get counter state record
    if db_schema_id in ["P70B","K000"]:
        crec = None
    else:
        crec = ppms.search_record(int(db_schema_counter_dt_id), [lic, di_id], dependencies, True)

    return crec

def processInput(di, oldvalue):
    rec = di.get_dtp_record()
    changed=False

    counter_record=get_counter_state_record(rec)
    if counter_record:
        counter_record.counter_value_1.set_value(di.get_value())
        changed=counter_record.save()

    if not changed: return oldvalue

    return di.get_value()

processInput.deps = ("*",)

def computeOutput(di):
    dtpRec = di.get_dtp_record()

    rec=get_counter_state_record(dtpRec)

    if rec:
        try:
            count = rec.counter_value_1.get_value()
            return count
        except:
            return ''
    else:
        return '0'

computeOutput.deps = ('*',)"""

class PortCounterValueRange(ChangeDTPPacket):
    '''Port the valuerange on DI 000006 to VoE'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 11714

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

    def fix(self, runner):
        record = self.get_record(412, ['000006'], ['valuerange'])
        if not self.change_value(record, 'valuerange', OLD_VALUERANGE, NEW_VALUERANGE):
            return self.fail()

        return self.success()
