/*-
 * ~~~~~~licensing~~~~~~
 * artifact-management
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.core.artifact.installer.builder.impl;

import java.util.HashMap;
import java.util.Map;

import ru.entaxy.platform.core.artifact.installer.ArtifactInstaller;
import ru.entaxy.platform.core.artifact.installer.builder.ClusterInstaller;
import ru.entaxy.platform.core.artifact.installer.builder.Installer;
import ru.entaxy.platform.core.artifact.installer.builder.Installers;
import ru.entaxy.platform.core.artifact.installer.builder.InstallersWithDefaults;
import ru.entaxy.platform.core.artifact.installer.builder.LocalInstaller;
import ru.entaxy.platform.core.artifact.installer.builder.impl.cluster.ClusterInstallerWrapperImpl;

public class InstallersWithDefaultsImpl implements InstallersWithDefaults {

	protected InstallersImpl defaults = null;
	
	protected Map<String, ArtifactInstaller<?>> installers = new HashMap<>();
	
	protected ArtifactInstaller<LocalInstaller> local = null;
	
	protected ArtifactInstaller<ClusterInstaller> cluster = null;
	
	protected boolean localOnlyMode = false;
	
	@Override
	public LocalInstaller local() {
		if (this.local == null)
			if (defaults != null)
				return defaults.local();
		return this.local.createInstaller();
	}

	@Override
	public ClusterInstaller cluster(String... groupName) {
		if (this.localOnlyMode)
			return new ClusterInstallerWrapperImpl(this.local());
		if (this.cluster == null)
			if (defaults != null)
				return defaults.cluster(groupName);
		return this.cluster.createInstaller().groups(groupName);
	}

	@Override
	public Installer<?> byName(String name) {
		if (ArtifactInstaller.ARTIFACT_INSTALLER_LOCAL.equals(name))
			return local();
		if (ArtifactInstaller.ARTIFACT_INSTALLER_CLUSTER.equals(name))
			return cluster();
		if (!this.installers.containsKey(name))
			return null;
		return this.installers.get(name).createInstaller();
	}

	@SuppressWarnings("unchecked")
	public void addInstaller(ArtifactInstaller<?> installer) {
		installers.put(installer.getName(), installer);
		if (ArtifactInstaller.ARTIFACT_INSTALLER_LOCAL.equalsIgnoreCase(installer.getName()))
			this.local = (ArtifactInstaller<LocalInstaller>)installer; 
		if (ArtifactInstaller.ARTIFACT_INSTALLER_CLUSTER.equalsIgnoreCase(installer.getName()))
			this.cluster = (ArtifactInstaller<ClusterInstaller>)installer; 
	}
	
	@Override
	public Installers useDefaults() {
		if (defaults == null)
			return this;
		return defaults;
	}

	public void setDefaults(InstallersImpl defaults) {
		this.defaults = defaults;
	}

	public boolean isLocalOnlyMode() {
		return localOnlyMode;
	}

	public void setLocalOnlyMode(boolean localOnlyMode) {
		this.localOnlyMode = localOnlyMode;
	}
}

