package ua.ansymo.hintco

import org.eclipse.core.runtime.Assert
import ua.ansymo.hintco.OutputPortInstance
import ua.ansymo.hintco.PowerBondAdaptation

class PowerOutAdaptedPort implements IAdaptedOutputPort {
	
	var protected double interPowerTerm
	
	var protected AdaptedFMUInstance fmu
	var protected PowerBondAdaptation adaptation;
	var protected StoreAdaptedPort powerTermStorage;
	double externalPowerTerm;
	
	new(PowerBondAdaptation a, StoreAdaptedPort s, AdaptedFMUInstance f) {
		fmu = f
		powerTermStorage = s
		adaptation = a
	}
	
	override recordReal(double t, double dt, double h, double H) {
		if (adaptation.effort instanceof OutputPortInstance){
			interPowerTerm = fmu.getInnerReal(adaptation.effort.identifier)
		} else {
			Assert.isTrue(adaptation.flow instanceof OutputPortInstance)
			interPowerTerm = fmu.getInnerReal(adaptation.flow.identifier)
		}
		// Record the external power term now, as this is the value that the inner fmu got in its step.
		// If this value were obtained in the getReal operation, it might have been updated already, which makes it wrong.
		externalPowerTerm = powerTermStorage.storedVal
	}
	
	override getReal() {
		interPowerTerm*externalPowerTerm
	}
	
	override startStep() {
	}
	
	override endStep() {
	}
	
	override startCosimStep(double time, double stepSize) {
	}
	
	override endCosimStep(double time, double stepSize) {
	}
	
}