package com.airlock.iam.plugin.configeditor.ui.dialog;

import com.airlock.iam.plugin.framework.misc.config.ConfigHistoryItem;
import com.vaadin.server.Sizeable.Unit;
import com.vaadin.shared.ui.ContentMode;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.VerticalLayout;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;
import org.apache.commons.lang3.time.FastDateFormat;

public class ConfigMetaDataPanel extends Panel {
   private String author;
   private Date configDate;
   private TextArea configCommentField;
   private Label authorLabel;
   private Label configDateLabel;

   public ConfigMetaDataPanel(boolean readOnly, String author, Date configDate) {
      this(readOnly);
      Objects.requireNonNull(author, "Author null");
      Objects.requireNonNull(configDate, "Config date null");
      this.setAuthor(author);
      this.setConfigDate(configDate);
   }

   public ConfigMetaDataPanel(boolean readOnly) {
      this.setCaption("Configuration Comment");
      VerticalLayout contentToAdd = new VerticalLayout();
      contentToAdd.setSpacing(false);
      this.authorLabel = new Label();
      this.authorLabel.setContentMode(ContentMode.HTML);
      this.setAuthor(null);
      contentToAdd.addComponent(this.authorLabel);
      this.configDateLabel = new Label();
      this.configDateLabel.setContentMode(ContentMode.HTML);
      this.setConfigDate(null);
      contentToAdd.addComponent(this.configDateLabel);
      contentToAdd.addComponent(new Label("Comment:"));
      this.configCommentField = new TextArea();
      this.configCommentField.setWordWrap(true);
      this.configCommentField.setRows(8);
      this.configCommentField.setWidth(100.0F, Unit.PERCENTAGE);
      this.configCommentField.setValue("");
      this.configCommentField.setReadOnly(readOnly);
      contentToAdd.addComponent(this.configCommentField);
      this.setContent(contentToAdd);
   }

   public void focusOnCommentField() {
      this.configCommentField.focus();
      this.configCommentField.selectAll();
   }

   public String getConfigComment() {
      Object o = this.configCommentField.getValue();
      return o == null ? "" : o.toString();
   }

   public ConfigMetaDataPanel setConfigComment(Optional<String> configComment) {
      boolean readOnlyState = this.configCommentField.isReadOnly();
      this.configCommentField.setReadOnly(false);
      this.configCommentField.setValue(configComment.orElse(""));
      this.configCommentField.setReadOnly(readOnlyState);
      return this;
   }

   public void setConfigCommentFieldReadOnly(boolean value) {
      this.configCommentField.setReadOnly(value);
   }

   public ConfigHistoryItem getHistoryItem() {
      return new ConfigHistoryItem(this.author, this.configDate, this.getConfigComment());
   }

   public final ConfigMetaDataPanel setConfigDate(Date configDateArg) {
      this.configDate = configDateArg;
      this.configDateLabel
         .setValue(
            "<div>Date and Time: <b>"
               + (this.configDate == null ? "-" : FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss").format(this.configDate))
               + "</b></div>"
         );
      this.configDateLabel.setId("dateAndTime");
      return this;
   }

   public final ConfigMetaDataPanel setAuthor(String authorArg) {
      this.author = authorArg;
      this.authorLabel.setValue("<div>Author: <b>" + (this.author == null ? "-" : this.author) + "</b></div>");
      this.authorLabel.setId("author");
      return this;
   }

   public void clear() {
      this.setAuthor(null);
      this.setConfigComment(Optional.empty());
      this.setConfigDate(null);
   }
}
