package io.openems.edge.app.api;

import static io.openems.edge.app.common.props.CommonProps.alias;

import java.util.Map;
import java.util.function.Function;

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.Reference;

import com.google.common.collect.Lists;
import com.google.gson.JsonElement;

import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.common.exceptions.OpenemsException;
import io.openems.common.function.ThrowingTriFunction;
import io.openems.common.session.Language;
import io.openems.common.session.Role;
import io.openems.common.types.EdgeConfig;
import io.openems.common.utils.JsonUtils;
import io.openems.edge.app.api.AppEnerixControl.Property;
import io.openems.edge.common.component.ComponentManager;
import io.openems.edge.core.appmanager.AbstractOpenemsApp;
import io.openems.edge.core.appmanager.AbstractOpenemsAppWithProps;
import io.openems.edge.core.appmanager.AppConfiguration;
import io.openems.edge.core.appmanager.AppDef;
import io.openems.edge.core.appmanager.ComponentUtil;
import io.openems.edge.core.appmanager.ConfigurationTarget;
import io.openems.edge.core.appmanager.Nameable;
import io.openems.edge.core.appmanager.OpenemsApp;
import io.openems.edge.core.appmanager.OpenemsAppCardinality;
import io.openems.edge.core.appmanager.OpenemsAppCategory;
import io.openems.edge.core.appmanager.OpenemsAppPermissions;
import io.openems.edge.core.appmanager.OpenemsAppStatus;
import io.openems.edge.core.appmanager.Type;
import io.openems.edge.core.appmanager.Type.Parameter.BundleParameter;
import io.openems.edge.core.appmanager.dependency.Tasks;
import io.openems.edge.core.appmanager.dependency.aggregatetask.SchedulerByCentralOrderConfiguration.SchedulerComponent;

@Component(name = "App.Cloud.EnerixControl")
public class AppEnerixControl extends AbstractOpenemsAppWithProps<AppEnerixControl, Property, BundleParameter>
		implements OpenemsApp {

	public static enum Property implements Type<Property, AppEnerixControl, BundleParameter>, Nameable {
		CONTROLLER_ID(AppDef.componentId("ctrlEnerixControl0")), //
		ALIAS(alias()), //
		URL(CleverPvProps.url(CONTROLLER_ID, "App.Cloud.EnerixControl.url.description")), //
		PRIVACY_POLICY(CleverPvProps.privacyPolicy(CONTROLLER_ID)), //
		;

		private final AppDef<? super AppEnerixControl, ? super Property, ? super BundleParameter> def;

		private Property(AppDef<? super AppEnerixControl, ? super Property, ? super BundleParameter> def) {
			this.def = def;
		}

		@Override
		public Type<Property, AppEnerixControl, BundleParameter> self() {
			return this;
		}

		@Override
		public AppDef<? super AppEnerixControl, ? super Property, ? super BundleParameter> def() {
			return this.def;
		}

		@Override
		public Function<GetParameterValues<AppEnerixControl>, BundleParameter> getParamter() {
			return Parameter.functionOf(AbstractOpenemsApp::getTranslationBundle);
		}
	}

	@Activate
	public AppEnerixControl(@Reference ComponentManager componentManager, ComponentContext componentContext,
			@Reference ConfigurationAdmin cm, @Reference ComponentUtil componentUtil) {
		super(componentManager, componentContext, cm, componentUtil);
	}

	@Override
	public OpenemsAppCategory[] getCategories() {
		return new OpenemsAppCategory[] { OpenemsAppCategory.API };
	}

	@Override
	public OpenemsAppCardinality getCardinality() {
		return OpenemsAppCardinality.SINGLE;
	}

	@Override
	protected AppEnerixControl getApp() {
		return this;
	}

	@Override
	protected ThrowingTriFunction<ConfigurationTarget, Map<Property, JsonElement>, Language, AppConfiguration, OpenemsNamedException> appPropertyConfigurationFactory() {
		return (t, p, l) -> {
			final var id = this.getId(t, p, Property.CONTROLLER_ID);
			final var alias = this.getString(p, l, Property.ALIAS);
			final var url = this.getValueOrDefault(p, Property.URL, null);
			final var isNewUrl = CleverPvUrl.isNewUrl(url);

			if (t == ConfigurationTarget.ADD || t == ConfigurationTarget.UPDATE && isNewUrl) {
				validateUrl(url, l);
			}

			final var components = Lists.newArrayList(//
					new EdgeConfig.Component(id, alias, "Controller.Clever-PV", //
							JsonUtils.buildJsonObject()//
									.onlyIf(isNewUrl, b -> b.addProperty("url", url)) //
									.addProperty("readOnly", false)//
									.build()));

			return AppConfiguration.create() //
					.addTask(Tasks.component(components)) //
					.addTask(Tasks.schedulerByCentralOrder(//
							new SchedulerComponent(id, "Controller.Clever-PV", this.getAppId()))) //
					.build();
		};
	}

	private static void validateUrl(String url, Language language) throws OpenemsException {
		if (CleverPvUrl.isValid(url)) {
			return;
		}
		throw new OpenemsException(getTranslation(language, "App.Cloud.EnerixControl.url.invalid"));
	}

	@Override
	protected Property[] propertyValues() {
		return Property.values();
	}

	@Override
	public OpenemsAppPermissions getAppPermissions() {
		return OpenemsAppPermissions.create() //
				.setCanInstall(Role.ADMIN, Role.OWNER) //
				.build();
	}

	@Override
	protected OpenemsAppStatus getStatus() {
		return OpenemsAppStatus.BETA;
	}
}
