/*-
 * ~~~~~~licensing~~~~~~
 * cellar-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.runtime.cellar.sequence.process;

import org.apache.karaf.cellar.core.event.Event;
import org.apache.karaf.cellar.features.ClusterFeaturesEvent;
import org.apache.karaf.cellar.features.ClusterRepositoryEvent;
import org.apache.karaf.features.FeatureEvent;
import org.apache.karaf.features.FeaturesListener;
import org.apache.karaf.features.RepositoryEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.entaxy.platform.runtime.cellar.helper.Services;

public class ChangeListenerFactory {

    private static final Logger LOG = LoggerFactory.getLogger(ChangeListenerFactory.class);

    public static ChangeListener create(Event originEvent) {

        if (originEvent instanceof ClusterRepositoryEvent) {
            return new RepositoryListener((ClusterRepositoryEvent) originEvent);
        }

        if (originEvent instanceof ClusterFeaturesEvent) {
            return new FeatureListener((ClusterFeaturesEvent) originEvent);
        }

        return null;
    }

    protected static abstract class AbstractChangeListener<T extends Event> implements ChangeListener {

        protected CallBack callBack;

        protected T originEvent;

        public AbstractChangeListener(T event) {
            super();
            this.originEvent = event;
        }

        @Override
        public void setCallback(CallBack callBack) {
            this.callBack = callBack;
        }

    }

    protected static class RepositoryListener extends AbstractChangeListener<ClusterRepositoryEvent>
            implements FeaturesListener {

        public RepositoryListener(ClusterRepositoryEvent event) {
            super(event);
        }

        @Override
        public void register() {
            Services.INSTANCE.featuresService.registerListener(this);

        }

        @Override
        public void unregister() {
            Services.INSTANCE.featuresService.unregisterListener(this);
        }

        @Override
        public void featureEvent(FeatureEvent event) {
            // ignore
        }

        @Override
        public void repositoryEvent(RepositoryEvent event) {
            if (event.getRepository().getURI().toString().equals(originEvent.getId())) {
                if (originEvent.getType().equals(event.getType())) {
                    LOG.debug("REPOSITORY EVENT for [{}]", originEvent.getId());
                    callBack.success();
                }
            }
        }

    }
    protected static class FeatureListener extends AbstractChangeListener<ClusterFeaturesEvent>
            implements FeaturesListener {

        public FeatureListener(ClusterFeaturesEvent event) {
            super(event);
        }

        @Override
        public void register() {
            Services.INSTANCE.featuresService.registerListener(this);

        }

        @Override
        public void unregister() {
            Services.INSTANCE.featuresService.unregisterListener(this);
        }

        @Override
        public void featureEvent(FeatureEvent event) {
            if (event.getFeature().getId().equals(originEvent.getId())) {
                if (originEvent.getType().equals(event.getType())) {
                    LOG.debug("FEATURE EVENT for [{}]", originEvent.getId());
                    callBack.success();
                }
            }
        }

        @Override
        public void repositoryEvent(RepositoryEvent event) {
            // ignore
        }

    }

}
