/*-
 * ~~~~~~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 java.util.function.Function;

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

import ru.entaxy.platform.base.support.karaf.shell.ShellTableExt;
import ru.entaxy.platform.config.runtime.ObjectConfig.ConfigFieldHint;
import ru.entaxy.platform.integration.applications.EntaxyApplication;
import ru.entaxy.platform.integration.applications.EntaxyApplicationRevision;
import ru.entaxy.platform.integration.applications.EntaxyApplicationVersion;
import ru.entaxy.platform.integration.applications.shell.ApplicationShell;
import ru.entaxy.platform.config.runtime.ObjectConfigEditor;
import ru.entaxy.platform.config.runtime.ObjectConfigEditor.ConfigProperty;

@Service
@Command(name = "app-revision-config-status", scope = ApplicationShell.COMMAND_SCOPE)
public class ConfigStatusCommand extends ManagedApplicationConfigCommand {

    @Option(name = "-s", aliases = {"--show-system-properties"})
    boolean showSystemProperties = false;

    ConfigFieldHint currentHint = null;

    @Override
    protected void doExecute(EntaxyApplication application, EntaxyApplicationVersion version,
            EntaxyApplicationRevision revision, ObjectConfigEditor editor) {

        Function<String, String> isSystemColorProvider = (str -> {
            if (currentHint == null)
                return ApplicationShell.COLOR_RESET;
            if (currentHint.isInternal())
                return ApplicationShell.COLOR_GRAY;
            return ApplicationShell.COLOR_RESET;
        });

        Function<ShellTableExt.CellData, String> isSystemColorProviderExt = (cellData -> {

            if (cellData.dataObject == null)
                return ApplicationShell.COLOR_RESET;

            if (!(cellData.dataObject instanceof ConfigProperty))
                return ApplicationShell.COLOR_RESET;

            ConfigProperty configProperty = (ConfigProperty) cellData.dataObject;

            if (configProperty.hint == null)
                return ApplicationShell.COLOR_RESET;
            if (configProperty.hint.isInternal())
                return ApplicationShell.COLOR_GRAY;
            return ApplicationShell.COLOR_RESET;
        });

        Function<ShellTableExt.CellData, String> isNewValueProviderExt = (cellData -> {

            if (cellData.dataObject == null)
                return ApplicationShell.COLOR_RESET;

            if (!(cellData.dataObject instanceof ConfigProperty))
                return ApplicationShell.COLOR_RESET;

            ConfigProperty configProperty = (ConfigProperty) cellData.dataObject;

            if (configProperty.currentValue == null)
                return ApplicationShell.COLOR_INCLUDED;
            return ApplicationShell.COLOR_RESET;
        });

        Function<ShellTableExt.CellData, String> isAbsentValueProviderExt = (cellData -> {

            if (cellData.dataObject == null)
                return ApplicationShell.COLOR_RESET;

            if (!(cellData.dataObject instanceof ConfigProperty))
                return ApplicationShell.COLOR_RESET;

            ConfigProperty configProperty = (ConfigProperty) cellData.dataObject;

            if (configProperty.configValue == null)
                return ApplicationShell.COLOR_REQUIRED;
            return ApplicationShell.COLOR_RESET;
        });

        Function<ShellTableExt.CellData, String> isModifiedColorProvider = (cellData -> {
            if (cellData.dataObject == null)
                return ApplicationShell.COLOR_RESET;
            return ApplicationShell.COLOR_IGNORED;
        });

        ShellTableExt table = new ShellTableExt();
        table.column("Name").colorProviderExt(isSystemColorProviderExt);
        table.column("Display Name").colorProvider(ApplicationShell.normalColorProvider);
        table.column("Is system");
        table.column("Type");
        table.column("Imported Value").colorProviderExt(isNewValueProviderExt);
        table.column("Modified Value").colorProviderExt(isModifiedColorProvider);
        table.column("Local Value").colorProviderExt(isAbsentValueProviderExt);

        for (String name : editor.getPropertiesNames()) {
            ConfigProperty property = editor.getAsConfigProperty(name);
            currentHint = property.hint;
            if (property.hint.isInternal() && !showSystemProperties)
                continue;
            table.addRow(property).addContent(
                    name,
                    property.hint.getDisplayName(),
                    property.hint.isInternal(),
                    property.hint.getType(),
                    property.configValue == null ? "" : property.configValue.toString(),
                    property.modifiedValue == null ? "" : property.modifiedValue.toString(),
                    property.currentValue == null ? "" : property.currentValue.toString());

        }

        // output data
        System.out.println("PID:\t" + editor.getPid());
        System.out.println("Modified:\t" + editor.isDirty());
        System.out.println("Temp file:\t" + editor.getConfigFile().getAbsolutePath());
        System.out.println("\n\tPROPERTIES:");
        table.print(System.out);

    }

}
