package io.openems.edge.controller.ess.delayedselltogrid;

import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ReferencePolicyOption;
import org.osgi.service.metatype.annotations.Designate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.edge.common.component.AbstractOpenemsComponent;
import io.openems.edge.common.component.OpenemsComponent;
import io.openems.edge.controller.api.Controller;
import io.openems.edge.ess.api.ManagedSymmetricEss;
import io.openems.edge.meter.api.ElectricityMeter;

@Designate(ocd = Config.class, factory = true)
@Component(//
		name = "Controller.Ess.DelayedSellToGrid", //
		immediate = true, //
		configurationPolicy = ConfigurationPolicy.REQUIRE //
)
public class ControllerEssDelayedSellToGridImpl extends AbstractOpenemsComponent
		implements ControllerEssDelayedSellToGrid, Controller, OpenemsComponent {

	private final Logger log = LoggerFactory.getLogger(ControllerEssDelayedSellToGridImpl.class);

	@Reference
	private ConfigurationAdmin cm;

	@Reference(policy = ReferencePolicy.STATIC, policyOption = ReferencePolicyOption.GREEDY, cardinality = ReferenceCardinality.MANDATORY)
	private ManagedSymmetricEss ess;

	@Reference(policy = ReferencePolicy.STATIC, policyOption = ReferencePolicyOption.GREEDY, cardinality = ReferenceCardinality.MANDATORY)
	private ElectricityMeter meter;

	private Config config;

	public ControllerEssDelayedSellToGridImpl() {
		super(//
				OpenemsComponent.ChannelId.values(), //
				Controller.ChannelId.values(), //
				ControllerEssDelayedSellToGrid.ChannelId.values());
	}

	@Activate
	private void activate(ComponentContext context, Config config) {
		super.activate(context, config.id(), config.alias(), config.enabled());
		this.config = config;
		if (OpenemsComponent.updateReferenceFilter(this.cm, this.servicePid(), "meter", config.meter_id())) {
			return;
		}
		if (OpenemsComponent.updateReferenceFilter(this.cm, this.servicePid(), "ess", config.ess_id())) {
			return;
		}
	}

	@Override
	@Deactivate
	protected void deactivate() {
		super.deactivate();
	}

	@Override
	public void run() throws OpenemsNamedException {
		// Check that we are On-Grid (and warn on undefined Grid-Mode)
		if (!this.ess.isOnGridOrUndefined(m -> this.logWarn(this.log, m))) {
			return;
		}

		int essPower = this.ess.getActivePower().getOrError();/* current charge/discharge Ess */
		// Calculate 'real' grid-power (without current ESS charge/discharge)
		var gridPower = this.meter.getActivePower().getOrError() + essPower;

		int calculatedPower;
		if (gridPower <= -this.config.sellToGridPowerLimit()) {
			/*
			 * Exceeds the Sell To Grid Power Limit
			 */
			calculatedPower = gridPower - (-this.config.sellToGridPowerLimit());

		} else if (gridPower >= -this.config.continuousSellToGridPower()) {

			/*
			 * Continuous Sell To Grid
			 */
			calculatedPower = gridPower - (-this.config.continuousSellToGridPower());

		} else {
			/*
			 * Do nothing
			 */
			calculatedPower = 0;
		}
		/*
		 * set result
		 */
		this.ess.setActivePowerEqualsWithFilter(calculatedPower);
		this.ess.setReactivePowerEqualsWithoutFilter(0);
	}
}
