package org.mitallast.queue.common.component;

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

public abstract class AbstractLifecycleComponent extends AbstractComponent implements LifecycleComponent {

    protected 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.State lifecycleState() {
        return this.lifecycle.state();
    }

    @SuppressWarnings({"unchecked"})
    @Override
    public void start() throws QueueException {
        if (!lifecycle.canMoveToStarted()) {
            return;
        }
        doStart();
        lifecycle.moveToStarted();
    }

    protected abstract void doStart() throws QueueException;

    @SuppressWarnings({"unchecked"})
    @Override
    public void stop() throws QueueException {
        if (!lifecycle.canMoveToStopped()) {
            return;
        }
        lifecycle.moveToStopped();
        doStop();
    }

    protected abstract void doStop() throws QueueException;

    @Override
    public void close() throws QueueException {
        if (lifecycle.started()) {
            stop();
        }
        if (!lifecycle.canMoveToClosed()) {
            return;
        }
        lifecycle.moveToClosed();
        doClose();
    }

    protected abstract void doClose() throws QueueException;
}


@@ -1456,77 +1456,289 @@
     
-return;%0A        %7D%0A        doStart();%0A        lifecycle.moveToStarted(
+logger.warn(%22Can't move to started, %22 + lifecycleState());%0A            return;%0A        %7D%0A        if (!lifecycle.moveToStarted()) %7B%0A            logger.warn(%22Don't moved to started, %22 + lifecycleState());%0A        %7D%0A        doStart();%0A        logger.info(%22started %22 + lifecycleState()
 );%0A 
@@ -1940,32 +1940,110 @@
 eToStopped()) %7B%0A
+            logger.warn(%22Can't move to stopped, it's a %22 + lifecycleState());%0A
             retu
@@ -2105,24 +2105,56 @@
   doStop();%0A
+        logger.info(%22stopped%22);%0A
     %7D%0A%0A    p
@@ -2372,32 +2372,116 @@
 veToClosed()) %7B%0A
+            logger.warn(%22Can't move to closed, it's a %22 + lifecycleState().name());%0A
             retu
@@ -2543,24 +2543,55 @@
  doClose();%0A
+        logger.info(%22closed%22);%0A
     %7D%0A%0A    p

add logging