/*-
 * ~~~~~~licensing~~~~~~
 * application-shell
 * ==========
 * 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.integration.applications.shell.application;

import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Completion;
import org.apache.karaf.shell.api.action.Option;
import org.apache.karaf.shell.api.action.lifecycle.Service;

import ru.entaxy.platform.integration.applications.ApplicationStorage;
import ru.entaxy.platform.integration.applications.EntaxyApplication;
import ru.entaxy.platform.integration.applications.EntaxyApplicationVersion;
import ru.entaxy.platform.integration.applications.shell.ApplicationManagerCommandSupport;
import ru.entaxy.platform.integration.applications.shell.ApplicationShell;
import ru.entaxy.platform.integration.applications.shell.completers.ApplicationCompleter;

@Service
@Command(name = "app-manage", scope = ApplicationShell.COMMAND_SCOPE)
public class ApplicationManageCommand extends ApplicationManagerCommandSupport {

    @Argument
    @Completion(value = ApplicationCompleter.class)
    String application;

    @Option(name = "-f", aliases = {"--force-cancel"})
    boolean forceCancel = false;

    @Override
    public Object execute() throws Exception {
        if (session.get(ApplicationShell.APPLICATION_KEY) != null) {
            if (forceCancel) {
                try {
                    EntaxyApplicationVersion version =
                            (EntaxyApplicationVersion) session.get(ApplicationShell.APPLICATION_VERSION_KEY);
                } catch (Exception ignore) {

                }
                try {
                    EntaxyApplication application =
                            (EntaxyApplication) session.get(ApplicationShell.APPLICATION_KEY);
                } catch (Exception ignore) {

                }
                session.put(ApplicationShell.APPLICATION_KEY, null);
                session.put(ApplicationShell.APPLICATION_VERSION_KEY, null);
            } else {
                // print error
                System.err.println("Other application is being managed, please commit or cancel");
                return null;
            }
        }

        String[] data = application.split("@");

        if (data.length < 2) {
            // print error
            System.err.println("Incorrect application specified");
            return null;
        }

        String storageName = data[1];
        ApplicationStorage storage = applicationManager.getStorage(storageName);
        if (storage == null) {
            // print error
            System.err.println("Incorrect storage specified: [" + storageName + "]");
            return null;
        }

        if (!storage.isEnabled()) {
            // print error
            System.err.println("Storage is disabled: [" + storageName + "]");
            return null;
        }

        String appName = data[0];
        EntaxyApplication app = storage.getApplication(appName);
        if (app == null) {
            // print error
            System.err.println(String.format("Application [%s] not found in storage [%s]", appName, storageName));
            return null;
        }

        session.put(ApplicationShell.APPLICATION_KEY, app);
        session.put(ApplicationShell.APPLICATION_VERSION_KEY, null);
        session.put(ApplicationShell.APPLICATION_REVISION_KEY, null);

        return null;
    }

}
