/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.wildfly.mod_cluster.undertow;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import org.jboss.as.server.suspend.ServerActivity;
import org.jboss.as.server.suspend.ServerActivityCallback;
import org.jboss.as.server.suspend.SuspendController;
import org.jboss.logging.Logger;
import org.jboss.modcluster.container.Connector;
import org.jboss.modcluster.container.ContainerEventHandler;
import org.jboss.modcluster.container.Context;
import org.jboss.modcluster.container.Engine;
import org.jboss.modcluster.container.Server;
import org.jboss.msc.Service;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StopContext;
import org.wildfly.clustering.context.DefaultThreadFactory;
import org.wildfly.extension.undertow.Host;
import org.wildfly.extension.undertow.UndertowEventListener;
import org.wildfly.extension.undertow.UndertowService;
import org.wildfly.security.manager.WildFlySecurityManager;

import io.undertow.servlet.api.Deployment;

/**
 * Builds a service exposing an Undertow subsystem adapter to mod_cluster's {@link ContainerEventHandler}.
 *
 * @author Paul Ferraro
 * @author Radoslav Husar
 */
public class UndertowEventHandlerAdapterService implements UndertowEventListener, Service, Runnable, ServerActivity {
    // No logger interface for this module and no reason to create one for this class only
    private static final Logger log = Logger.getLogger("org.jboss.mod_cluster.undertow");
    private static final ThreadFactory THREAD_FACTORY = new DefaultThreadFactory(UndertowEventHandlerAdapterService.class, WildFlySecurityManager.getClassLoaderPrivileged(UndertowEventHandlerAdapterService.class));

    private final UndertowEventHandlerAdapterConfiguration configuration;
    private final Set<Context> contexts = new HashSet<>();
    private volatile ScheduledExecutorService executor;
    private volatile Server server;
    private volatile Connector connector;
    private volatile String serverName;

    public UndertowEventHandlerAdapterService(UndertowEventHandlerAdapterConfiguration configuration) {
        this.configuration = configuration;
    }

    @Override
    public void start(StartContext context) {
        UndertowService service = this.configuration.getUndertowService();
        ContainerEventHandler eventHandler = this.configuration.getContainerEventHandler();
        this.connector = new UndertowConnector(this.configuration.getListener());
        this.serverName = this.configuration.getServer().getName();
        this.server = new UndertowServer(this.serverName, service, this.connector);

        // Register ourselves as a listener to the container events
        service.registerListener(this);

        // Initialize mod_cluster and start it now
        eventHandler.init(this.server);
        eventHandler.start(this.server);
        for (Engine engine : this.server.getEngines()) {
            for (org.jboss.modcluster.container.Host host : engine.getHosts()) {
                host.getContexts().forEach(contexts::add);
            }
        }

        // Start the periodic STATUS thread
        this.executor = Executors.newScheduledThreadPool(1, THREAD_FACTORY);
        this.executor.scheduleWithFixedDelay(this, 0, this.configuration.getStatusInterval().toMillis(), TimeUnit.MILLISECONDS);
        this.configuration.getSuspendController().registerActivity(this);
    }

    @Override
    public void stop(StopContext context) {
        this.configuration.getSuspendController().unRegisterActivity(this);
        this.configuration.getUndertowService().unregisterListener(this);

        this.executor.shutdownNow();
        try {
            this.executor.awaitTermination(this.configuration.getStatusInterval().toMillis(), TimeUnit.MILLISECONDS);
        } catch (InterruptedException ignore) {
            // Move on.
        }

        this.configuration.getContainerEventHandler().stop(this.server);
    }

    private Context createContext(Deployment deployment, Host host) {
        return new UndertowContext(deployment, new UndertowHost(host, new UndertowEngine(serverName, host.getServer().getValue(), this.configuration.getUndertowService(), this.connector)));
    }

    private Context createContext(String contextPath, Host host) {
        return new LocationContext(contextPath, new UndertowHost(host, new UndertowEngine(serverName, host.getServer().getValue(), this.configuration.getUndertowService(), this.connector)));
    }

    private synchronized void onStart(Context context) {
        ContainerEventHandler handler = this.configuration.getContainerEventHandler();

        SuspendController.State state = this.configuration.getSuspendController().getState();

        if (state == SuspendController.State.RUNNING) {
            // Normal operation - trigger ENABLE-APP
            handler.start(context);
        } else {
            // Suspended mode - trigger STOP-APP without request nor session draining;
            // n.b. contexts will be started by UndertowEventHandlerAdapterService#resume()
            handler.add(context);
        }

        this.contexts.add(context);
    }

    private synchronized void onStop(Context context) {
        ContainerEventHandler handler = this.configuration.getContainerEventHandler();

        // Trigger STOP-APP with possible session draining
        handler.stop(context);

        // Trigger REMOVE-APP
        handler.remove(context);

        this.contexts.remove(context);
    }

    @Override
    public void onDeploymentStart(Deployment deployment, Host host) {
        if (this.filter(host)) {
            this.onStart(this.createContext(deployment, host));
        }
    }

    @Override
    public void onDeploymentStop(Deployment deployment, Host host) {
        if (this.filter(host)) {
            this.onStop(this.createContext(deployment, host));
        }
    }

    @Override
    public void onDeploymentStart(String contextPath, Host host) {
        if (this.filter(host)) {
            this.onStart(this.createContext(contextPath, host));
        }
    }

    @Override
    public void onDeploymentStop(String contextPath, Host host) {
        if (this.filter(host)) {
            this.onStop(this.createContext(contextPath, host));
        }
    }

    public boolean filter(Host host) {
        return host.getServer().getName().equals(serverName);
    }

    @Override
    public void run() {
        try {
            for (Engine engine : this.server.getEngines()) {
                this.configuration.getContainerEventHandler().status(engine);
            }
        } catch (Throwable e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public synchronized void preSuspend(ServerActivityCallback listener) {
        try {
            for (Context context : this.contexts) {
                this.configuration.getContainerEventHandler().stop(context);
            }
        } finally {
            listener.done();
        }
    }

    @Override
    public void suspended(ServerActivityCallback listener) {
        listener.done();
    }

    @Override
    public void resume() {
        for (Context context : this.contexts) {
            this.configuration.getContainerEventHandler().start(context);
        }
    }
}
