package com.airlock.iam.servicecontainer.app.infrastructure.webapp.view;

import com.airlock.iam.servicecontainer.app.infrastructure.webapp.presenter.GenericServicePresenter;
import com.airlock.iam.servicecontainer.app.infrastructure.webapp.presenter.ServicePresenter;
import com.airlock.iam.servicecontainer.app.infrastructure.webapp.util.Css;
import com.airlock.iam.servicecontainer.app.infrastructure.webapp.util.GuiResources;
import com.airlock.iam.servicecontainer.app.infrastructure.webapp.util.ServiceContainerVaadinTranslator;
import com.airlock.iam.servicecontainer.app.infrastructure.webapp.util.TranslatedKeyValueTable;
import com.vaadin.ui.AbstractOrderedLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

public class GenericServiceView extends CustomComponent {
   private final ServicePresenter<?, ?> servicePresenter;
   private Button startStopButton;
   private TranslatedKeyValueTable<String, String> generalInformationTable;
   private Label statusLayoutLabel;

   public GenericServiceView(ServicePresenter<?, ?> newServicePresenter) {
      this.servicePresenter = newServicePresenter;
   }

   public GenericServiceView(GenericServicePresenter newServicePresenter) {
      this.servicePresenter = newServicePresenter;
   }

   public void init() {
      AbstractOrderedLayout layout = this.getLayout();
      this.setCompositionRoot(layout);
   }

   protected AbstractOrderedLayout getLayout() {
      VerticalLayout layout = new VerticalLayout();
      layout.setSpacing(false);
      layout.addStyleName(Css.SC_GENERIC_SERVICE_VIEW.toString());
      Label text = new Label(this.servicePresenter.getName());
      text.addStyleName(Css.SC_H1.toString());
      layout.addComponent(text);
      VerticalLayout innerLayout = new VerticalLayout();
      innerLayout.setSpacing(false);
      innerLayout.setMargin(false);
      layout.addComponent(innerLayout);
      this.generalInformationTable = TranslatedKeyValueTable.buildTable();
      innerLayout.addComponent(this.generalInformationTable);
      this.fillGeneralInformation();
      this.generalInformationTable.setStyleName("sc-generic-table");
      innerLayout.addComponent(new HorizontalRuler("sc-hr-with-margin"));
      HorizontalLayout statusLayout = new HorizontalLayout();
      statusLayout.setSpacing(false);
      statusLayout.addStyleName(Css.SC_GENERIC_STATUS.toString());
      innerLayout.addComponent(statusLayout);
      this.startStopButton = new Button("", event -> {
         this.servicePresenter.startStop();
         this.startStopButton.setEnabled(false);
      });
      statusLayout.addComponent(this.startStopButton);
      this.startStopButton.addStyleName("borderless");
      this.statusLayoutLabel = new Label("test");
      statusLayout.addComponent(this.statusLayoutLabel);
      statusLayout.setComponentAlignment(this.statusLayoutLabel, Alignment.MIDDLE_LEFT);
      this.updateStatus();
      return layout;
   }

   private void updateStatus() {
      if (this.servicePresenter.isRunning() && !this.servicePresenter.isStopping()) {
         this.startStopButton.setIcon(GuiResources.ICON_SERVICE_STOP);
         this.statusLayoutLabel.setValue(ServiceContainerVaadinTranslator.translate("genericserviceview.current-status-running"));
         this.startStopButton.setEnabled(true);
         this.startStopButton.setDescription(ServiceContainerVaadinTranslator.translate("taskschedulerservice.stop-task"));
      } else if (this.servicePresenter.isStopping()) {
         this.startStopButton.setIcon(GuiResources.ICON_SERVICE_STOPPING);
         this.statusLayoutLabel.setValue(ServiceContainerVaadinTranslator.translate("genericserviceview.current-status-stopping"));
         this.startStopButton.setEnabled(false);
      } else {
         this.startStopButton.setIcon(GuiResources.ICON_SERVICE_START);
         this.statusLayoutLabel.setValue(ServiceContainerVaadinTranslator.translate("genericserviceview.current-status-not-running"));
         this.startStopButton.setEnabled(true);
         this.startStopButton.setDescription(ServiceContainerVaadinTranslator.translate("taskschedulerservice.start-task"));
      }
   }

   private void fillGeneralInformation() {
      this.generalInformationTable.addKeyValuePair("genericserviceview.name", this.servicePresenter.getName());
      this.generalInformationTable.addKeyValuePair("genericserviceview.description", this.servicePresenter.getDescription());
      this.generalInformationTable.addKeyValuePair("genericserviceview.threadname", this.servicePresenter.getThreadName());
   }

   private void updateGeneralInformation() {
      this.generalInformationTable.update("genericserviceview.name", this.servicePresenter.getName());
      this.generalInformationTable.update("genericserviceview.description", this.servicePresenter.getDescription());
      this.generalInformationTable.update("genericserviceview.threadname", this.servicePresenter.getThreadName());
   }

   public void update(ServiceContainerUI serviceContainerUI) {
      if (serviceContainerUI.isAttached()) {
         serviceContainerUI.access(() -> {
            this.updateGeneralInformation();
            this.updateStatus();
         });
      }
   }
}
