/*-
 * ~~~~~~licensing~~~~~~
 * artifact-management
 * ==========
 * 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.installer.builder.typed.impl;

import org.osgi.framework.Constants;

import ru.entaxy.platform.base.support.CommonUtils;
import ru.entaxy.platform.core.artifact.installer.builder.InstallationResult;
import ru.entaxy.platform.core.artifact.installer.builder.impl.InstallationResultImpl;
import ru.entaxy.platform.core.artifact.installer.builder.typed.CommonBundleInstaller;

public class CommonBundleInstallerImpl extends TypedInstallerImpl
        implements CommonBundleInstaller, CommonBundleInstallerHelperConfig {

    protected CommonBundleInstallerHelper bundleInstallerHelper = null;

    protected boolean startBundle = false;

    protected boolean startBundleOnly = false;

    protected boolean stopBundle = false;

    protected boolean stopBundleOnly = false;

    // TODO get it from framework settings
    protected int startLevel = -1;

    protected boolean update = false;

    protected String symbolicNameToUpdate = null;

    protected String version = null;

    protected boolean updateOnly = false;

    protected boolean refresh = false;

    protected boolean rawUpdate = false;

    protected boolean installOnlyIfMissing = false;

    protected boolean forceInstall = false;

    @Override
    public void clearInstaller() {
        super.clearInstaller();

        startBundle = false;
        startBundleOnly = false;
        stopBundle = false;
        stopBundleOnly = false;
        startLevel = -1;
        update = false;
        symbolicNameToUpdate = null;
        version = null;
        updateOnly = false;
        refresh = false;
        rawUpdate = false;
        installOnlyIfMissing = false;
        forceInstall = false;
    }

    @Override
    public InstallationResult install() {
        InstallationResultImpl result = InstallationResultImpl.create();
        if (this.bundleInstallerHelper == null)
            result.failed("HELPER NOT FOUND");
        else
            this.bundleInstallerHelper.install(result);
        return result.property("artifact", this.artifact)
                .property("bundleId", getBundleId())
                .property("bundleLocation", getBundleLocation());
    }

    @Override
    public InstallationResult uninstall() {
        InstallationResultImpl result = InstallationResultImpl.create();
        if (this.bundleInstallerHelper == null)
            result.failed("HELPER NOT FOUND");
        else
            this.bundleInstallerHelper.uninstall(result);
        return result.property("artifact", this.artifact)
                .property("bundleId", getBundleId())
                .property("bundleLocation", getBundleLocation());
    }

    @Override
    public void setHelper(TypedInstallerHelperImpl helper) {
        super.setHelper(helper);
        if (this.helper instanceof CommonBundleInstallerHelper) {
            this.bundleInstallerHelper = (CommonBundleInstallerHelper) helper;
            this.bundleInstallerHelper.setConfig(this);
        }
    }

    protected String getArtifactLocation() {
        return this.artifact.getLocation();
    }

    @Override
    public CommonBundleInstaller start() {
        this.stop(false);
        this.stopBundleOnly = false;
        return this.start(true);
    }

    @Override
    public CommonBundleInstaller startOnly() {
        this.start();
        this.startBundleOnly = true;
        return this;
    }

    @Override
    public CommonBundleInstaller start(boolean value) {
        this.startBundle = value;
        return this;
    }

    @Override
    public CommonBundleInstaller stop() {
        this.start(false);
        this.startBundleOnly = false;
        return this.stop(true);
    }

    @Override
    public CommonBundleInstaller stopOnly() {
        this.stop();
        this.stopBundleOnly = true;
        return this;
    }

    @Override
    public CommonBundleInstaller stop(boolean value) {
        this.stopBundle = value;
        return this;
    }

    @Override
    public CommonBundleInstaller startLevel(int startLevel) {
        if (startLevel > 0)
            this.startLevel = startLevel;
        return this;
    }

    @Override
    public CommonBundleInstaller installOnlyIfMissing() {
        this.installOnlyIfMissing = true;
        return this;
    }

    @Override
    public CommonBundleInstaller forceInstall() {
        this.forceInstall = true;
        return this;
    }

    @Override
    public CommonBundleInstaller update() {
        return update(null, null);
    }

    @Override
    public CommonBundleInstaller update(String symbolicName) {
        return update(symbolicName, null);
    }

    @Override
    public CommonBundleInstaller update(String symbolicName, String version) {
        this.symbolicNameToUpdate = symbolicName;
        this.version = version;
        this.update = true;
        return this;
    }

    @Override
    public CommonBundleInstaller updateOnly() {
        this.updateOnly = true;
        return this;
    }

    @Override
    public CommonBundleInstaller refresh() {
        this.refresh = true;
        return this;
    }

    @Override
    public CommonBundleInstaller rawUpdate() {
        this.rawUpdate = true;
        this.update = true;
        return this;
    }


    /*
     * CommonBundleInstallerHelperConfig implementation
     */

    @Override
    public boolean isInstallOnlyIfMissing() {
        return installOnlyIfMissing;
    }

    @Override
    public boolean isForceInstall() {
        return forceInstall;
    }

    @Override
    public boolean isStartBundle() {
        return startBundle;
    }

    @Override
    public boolean isStartBundleOnly() {
        return startBundleOnly;
    }

    @Override
    public boolean isStopBundle() {
        return stopBundle;
    }

    @Override
    public boolean isStopBundleOnly() {
        return stopBundleOnly;
    }

    @Override
    public int getStartLevel() {
        return startLevel;
    }

    @Override
    public boolean isUpdate() {
        return update;
    }

    @Override
    public String getSymbolicNameToUpdate() {
        return symbolicNameToUpdate;
    }

    @Override
    public String getVersion() {
        return version;
    }

    @Override
    public boolean isUpdateOnly() {
        return updateOnly;
    }

    @Override
    public boolean isRefresh() {
        return refresh;
    }

    @Override
    public boolean isRawUpdate() {
        return rawUpdate;
    }

    @Override
    public String getBundleLocation() {
        if (CommonUtils.isValid(sourceLocation))
            return sourceLocation;
        if (artifact != null)
            return getArtifactLocation();
        // TODO throw exception due to misconfiguration
        return null;
    }

    @Override
    public String getBundleId() {
        String bundleId = "";
        if (CommonUtils.isValid(bundleName)) {
            bundleId += bundleName;
        } else if (CommonUtils.isValid(symbolicNameToUpdate)) {
            bundleId += symbolicNameToUpdate;
            if (CommonUtils.isValid(version))
                bundleId += version;
        } else {
            if (artifact != null) {
                Object tmp = artifact.getArtifact().getProperties().get(Constants.BUNDLE_SYMBOLICNAME);
                if (tmp != null)
                    if (CommonUtils.isValid(tmp.toString()))
                        bundleId += tmp.toString();
            }
        }
        if (!CommonUtils.isValid(bundleId) && (artifact != null)) {
            bundleId += artifact.getArtifact().getCoordinates().getGroupId()
                    + "." + artifact.getArtifact().getCoordinates().getArtifactId();
        }
        return bundleId;
    }
}
