package org.mitallast.queue.common.component;

import org.mitallast.queue.common.settings.Settings;

import java.io.IOException;

public abstract class AbstractLifecycleComponent extends AbstractComponent implements LifecycleComponent {

    private final Lifecycle lifecycle = new Lifecycle();

    protected AbstractLifecycleComponent(Settings settings) {
        super(settings);
    }

    protected AbstractLifecycleComponent(Settings settings, Class customClass) {
        super(settings, customClass);
    }

    protected AbstractLifecycleComponent(Settings settings, Class loggerClass, Class componentClass) {
        super(settings, loggerClass, componentClass);
    }

    protected AbstractLifecycleComponent(Settings settings, String prefixSettings) {
        super(settings, prefixSettings);
    }

    protected AbstractLifecycleComponent(Settings settings, String prefixSettings, Class customClass) {
        super(settings, prefixSettings, customClass);
    }

    protected AbstractLifecycleComponent(Settings settings, String prefixSettings, Class loggerClass, Class componentClass) {
        super(settings, prefixSettings, loggerClass, componentClass);
    }

    @Override
    public Lifecycle lifecycle() {
        return lifecycle;
    }

    @Override
    public void checkIsStarted() {
        if (!lifecycle.started()) {
            throw new IllegalStateException(logger.getName() + " is not started");
        }
    }

    @Override
    public void checkIsStopped() {
        if (!lifecycle.started()) {
            throw new IllegalStateException(logger.getName() + " is not started");
        }
    }

    @Override
    public void checkIsClosed() {
        if (!lifecycle.started()) {
            throw new IllegalStateException(logger.getName() + " is not started");
        }
    }

    @Override
    public void start() throws IOException {
        if (!lifecycle.canMoveToStarted()) {
            logger.warn("Can't move to started, " + lifecycle.state());
            return;
        }
        if (!lifecycle.moveToStarted()) {
            logger.warn("Don't moved to started, " + lifecycle.state());
        }
        doStart();
        logger.info("started");
    }

    protected abstract void doStart() throws IOException;

    @Override
    public void stop() throws IOException {
        logger.debug("stopping");
        if (!lifecycle.canMoveToStopped()) {
            logger.warn("Can't move to stopped, it's a " + lifecycle.state());
            return;
        }
        lifecycle.moveToStopped();
        doStop();
        logger.debug("stopped");
    }

    protected abstract void doStop() throws IOException;

    @Override
    public void close() throws IOException {
        if (lifecycle.started()) {
            stop();
        }
        if (!lifecycle.canMoveToClosed()) {
            logger.warn("Can't move to closed, it's a " + lifecycle.state());
            return;
        }
        lifecycle.moveToClosed();
        doClose();
        logger.info("closed");
    }

    protected abstract void doClose() throws IOException;
}


@@ -2133,32 +2133,65 @@
 e());%0A        %7D%0A
+        logger.info(%22starting%22);%0A
         doStart(
@@ -2189,24 +2189,24 @@
  doStart();%0A
-
         logg
@@ -2353,42 +2353,8 @@
 n %7B%0A
-        logger.debug(%22stopping%22);%0A
     
@@ -2534,24 +2534,57 @@
 oStopped();%0A
+        logger.info(%22stopping%22);%0A
         doSt
@@ -2955,32 +2955,32 @@
 turn;%0A        %7D%0A
-
         lifecycl
@@ -2993,24 +2993,56 @@
 ToClosed();%0A
+        logger.info(%22closing%22);%0A
         doCl

refactor logging in abstract lifecycle component