/*-
 * ~~~~~~licensing~~~~~~
 * artifact-management-extensions
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.core.artifact.ext.features.impl;

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

import ru.entaxy.platform.core.artifact.DeployedArtifact;
import ru.entaxy.platform.core.artifact.ext.features.FeatureInstaller;
import ru.entaxy.platform.core.artifact.ext.impl.ExtendedTypedInstallerImpl;
import ru.entaxy.platform.core.artifact.installer.builder.InstallationResult;
import ru.entaxy.platform.core.artifact.installer.builder.typed.impl.TypedInstallerHelperImpl;

public class FeatureInstallerImpl
        extends ExtendedTypedInstallerImpl<FeatureInstallerConfigurableHelper, FeatureInstallerImpl>
        implements FeatureInstaller {

    protected static class FeatureInstallDescriptorImpl implements FeatureInstallDescriptor {

        FeatureInstallerImpl featureInstaller;

        String featureName;
        String featureVersion;

        boolean noStart = false;
        boolean noRefresh = false;
        boolean noManage = false;
        boolean upgrade = false;

        protected FeatureInstallDescriptorImpl(FeatureInstallerImpl featureInstaller, String name) {
            this.featureInstaller = featureInstaller;
            this.featureName = name;
        }

        @Override
        public void clearInstaller() {
            featureInstaller.clearInstaller();
        }

        @Override
        public void setSourceLocation(String sourceLocation) {}

        @Override
        public String getSourceLocation() {
            return featureInstaller.getSourceLocation();
        }

        @Override
        public void setArtifact(DeployedArtifact artifact) {
            featureInstaller.setArtifact(artifact);
        }

        @Override
        public DeployedArtifact getArtifact() {
            return featureInstaller.getArtifact();
        }

        @Override
        public String getTypedName() {
            return featureInstaller.getTypedName();
        }

        @Override
        public void setTypedName(String typedName) {
            featureInstaller.setTypedName(typedName);
        }

        @Override
        public InstallationResult install() {
            return featureInstaller.install();
        }

        @Override
        public InstallationResult uninstall() {
            return featureInstaller.uninstall();
        }

        @Override
        public FeatureInstallDescriptor version(String version) {
            this.featureVersion = version;
            return this;
        }

        @Override
        public FeatureInstallDescriptor noRefresh() {
            this.noRefresh = true;
            return this;
        }

        @Override
        public FeatureInstallDescriptor noStart() {
            this.noStart = true;
            return this;
        }

        @Override
        public FeatureInstallDescriptor noManage() {
            this.noManage = true;
            return this;
        }

        @Override
        public FeatureInstallDescriptor upgrade() {
            this.upgrade = true;
            return this;
        }

        @Override
        public FeatureInstallDescriptor feature(String name) {
            return featureInstaller.feature(name);
        }

    }

    protected final Map<String, FeatureInstallDescriptorImpl> descriptors = new HashMap<>();

    protected String sequenceId = null;

    @Override
    public FeatureInstaller inSequence(String sequenceId) {
        this.sequenceId = sequenceId;
        return this;
    }

    @Override
    public FeatureInstallDescriptor feature(String name) {
        if (descriptors.containsKey(name))
            return descriptors.get(name);
        FeatureInstallDescriptorImpl descriptor = new FeatureInstallDescriptorImpl(this, name);
        descriptors.put(name, descriptor);
        return descriptor;
    }

    @Override
    protected Class<FeatureInstallerConfigurableHelper> getHelperClass() {
        return FeatureInstallerConfigurableHelper.class;
    }

    @Override
    protected <H extends TypedInstallerHelperImpl> H createClusterHelper(Class<H> targetClass) {
        return (H) new FeatureInstallerClusterHelper();
    }

    @Override
    protected <H extends TypedInstallerHelperImpl> H createLocalHelper(Class<H> targetClass) {
        return (H) new FeatureInstallerLocalHelper();
    }

}
