package com.airlock.iam.servicecontainer.app.application.configuration.task;

import com.airlock.iam.base.misc.concurrent.ThreadUtils;
import com.airlock.iam.elog.api.ELogger;
import com.airlock.iam.elog.api.ELoggerFactory;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.lifecycle.BeforePluginDeactivation;
import com.airlock.iam.servicecontainer.app.application.service.AbstractService;
import com.airlock.iam.servicecontainer.app.infrastructure.server.ManagableViaGUI;
import com.airlock.iam.servicecontainer.app.infrastructure.webapp.presenter.GenericServicePresenter;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.concurrent.TimeUnit;

@Plugin(description = "A demo service that writes a log line from time to time.<p>There are no configuration properties.</p>")
public class DemoServiceConfig extends AbstractService implements ManagableViaGUI {
   private static final ELogger LOG = ELoggerFactory.getLogger(DemoServiceConfig.class);

   @Override
   public String getName() {
      return "Demo Service";
   }

   @Override
   public String getThreadName() {
      return "DemoService";
   }

   @Override
   public void run() {
      this.setRunning(true);
      LOG.info("Service " + this.getName() + " is starting to run");
      int counter = 0;

      do {
         LOG.info("Service " + this.getName() + " is counting: " + counter++);
         ThreadUtils.sleepInterruptibly(3L, TimeUnit.SECONDS);
      } while (!this.isStopping());

      this.setPleaseStopToFalse();
      LOG.info("Service " + this.getName() + " has stopped.");
      this.setRunning(false);
   }

   @BeforePluginDeactivation
   public void handlePluginUnloading() {
      this.pleaseStop();
   }

   @Override
   public Class<GenericServicePresenter> getPresenterClass() {
      return GenericServicePresenter.class;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static DemoServiceConfig.DemoServiceConfigBuilder builder() {
      return new DemoServiceConfig.DemoServiceConfigBuilder();
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class DemoServiceConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      DemoServiceConfigBuilder() {
      }

      @SuppressFBWarnings(justification = "generated code")
      public DemoServiceConfig build() {
         return new DemoServiceConfig();
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "DemoServiceConfig.DemoServiceConfigBuilder()";
      }
   }
}
