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

import com.airlock.iam.servicecontainer.app.infrastructure.webapp.presenter.OverviewPresenter;
import com.airlock.iam.servicecontainer.app.infrastructure.webapp.util.Css;
import com.airlock.iam.servicecontainer.app.infrastructure.webapp.util.ServiceContainerVaadinTranslator;
import com.airlock.iam.servicecontainer.app.infrastructure.webapp.util.TranslatedKeyValueTable;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

public class OverviewView extends CustomComponent {
   private final OverviewPresenter overviewPresenter;
   private TranslatedKeyValueTable<String, String> statisticsTable;

   public OverviewView(OverviewPresenter overviewPresenter) {
      this.overviewPresenter = overviewPresenter;
      this.init();
   }

   private void init() {
      VerticalLayout layout = new VerticalLayout();
      layout.addStyleName(Css.SC_GENERIC_SERVICE_VIEW.toString());
      layout.setSpacing(false);
      Label title = new Label(ServiceContainerVaadinTranslator.translate("overview.title"));
      title.addStyleName(Css.SC_H1.toString());
      layout.addComponent(title);
      VerticalLayout innerLayout = new VerticalLayout();
      innerLayout.setSpacing(false);
      innerLayout.setMargin(false);
      layout.addComponent(innerLayout);
      this.statisticsTable = TranslatedKeyValueTable.buildTable();
      innerLayout.addComponent(this.statisticsTable);
      this.initStatisticsTable();
      this.statisticsTable.setStyleName("sc-generic-table");
      this.setCompositionRoot(layout);
   }

   private void initStatisticsTable() {
      this.statisticsTable.addKeyValuePair("overview.service-container-uptime", this.overviewPresenter.getServerUptime());
      this.statisticsTable.addKeyValuePair("overview.loaded-services", this.overviewPresenter.getNumberOfLoadedServices());
      this.statisticsTable.addKeyValuePair("overview.running-services", this.overviewPresenter.getNumberOfRunningServices());
      this.statisticsTable.addKeyValuePair("overview.total-memory", this.overviewPresenter.getTotalMemory());
      this.statisticsTable.addKeyValuePair("overview.used-memory", this.overviewPresenter.getUsedMemory());
      this.statisticsTable.addKeyValuePair("overview.free-memory", this.overviewPresenter.getFreeMemory());
      this.statisticsTable.addKeyValuePair("overview.number-of-total-threads", this.overviewPresenter.getNumberOfThreads());
      this.statisticsTable.addKeyValuePair("overview.number-of-idle-threads", this.overviewPresenter.getNumberOfSleepingThreads());
      this.statisticsTable.addKeyValuePair("overview.number-of-busy-threads", this.overviewPresenter.getNumberOfWorkingThreads());
   }

   private void updateStatisticsTable() {
      this.statisticsTable.update("overview.service-container-uptime", this.overviewPresenter.getServerUptime());
      this.statisticsTable.update("overview.loaded-services", this.overviewPresenter.getNumberOfLoadedServices());
      this.statisticsTable.update("overview.running-services", this.overviewPresenter.getNumberOfRunningServices());
      this.statisticsTable.update("overview.total-memory", this.overviewPresenter.getTotalMemory());
      this.statisticsTable.update("overview.used-memory", this.overviewPresenter.getUsedMemory());
      this.statisticsTable.update("overview.free-memory", this.overviewPresenter.getFreeMemory());
      this.statisticsTable.update("overview.number-of-total-threads", this.overviewPresenter.getNumberOfThreads());
      this.statisticsTable.update("overview.number-of-idle-threads", this.overviewPresenter.getNumberOfSleepingThreads());
      this.statisticsTable.update("overview.number-of-busy-threads", this.overviewPresenter.getNumberOfWorkingThreads());
   }

   public void update(ServiceContainerUI serviceContainerUI) {
      if (serviceContainerUI.isAttached()) {
         serviceContainerUI.access(this::updateStatisticsTable);
      }
   }
}
