package de.ivu.elect.presentation.internet.table;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;

import de.ivu.elect.business.internet.boundary.InternetSeiteConversationSelect;
import de.ivu.elect.business.internet.boundary.InternetWidgetExtendedDAO;
import de.ivu.elect.business.internet.entity.AbstractInternetWidget;
import de.ivu.elect.business.internet.entity.InternetWidget;
import de.ivu.elect.business.internet.entity.InternetWidgetCustomData;
import de.ivu.elect.business.internet.entity.InternetWidgetFile;
import de.ivu.elect.jee.presentation.dynamictable.AbstractColumnBuilder;
import de.ivu.elect.jee.presentation.dynamictable.ColumnEntry;
import de.ivu.elect.jee.presentation.dynamictable.ColumnType;

@Named(InternetWidgetColumnBuilder.COMPONENT_NAME)
public class InternetWidgetColumnBuilder extends AbstractColumnBuilder<InternetWidget> {

  public static final String COMPONENT_NAME = "internetWidgetColumnBuilder";

  private static final long serialVersionUID = 1L;

  @Inject
  private transient InternetWidgetExtendedDAO internetWidgetExtendedDAO;

  @Inject
  private transient InternetSeiteConversationSelect internetSeiteConversationSelect;


  private AbstractInternetWidgetColumn<InternetWidget> internetWidgetIdColumn;
  private AbstractInternetWidgetColumn<InternetWidget> internetWidgetPositionColumn;
  private AbstractInternetWidgetColumn<InternetWidget> internetWidgetWidgetTypColumn;
  private AbstractInternetWidgetColumn<InternetWidget> internetWidgetZeileColumn;
  private AbstractInternetWidgetColumn<InternetWidget> internetWidgetStatusColumn;
  private AbstractInternetWidgetColumn<InternetWidget> internetWidgetBreiteColumn;
  private AbstractInternetWidgetColumn<InternetWidget> internetWidgetBeschreibungColumn;
  private AbstractInternetWidgetColumn<InternetWidget> internetWidgetSprungmarkeColumn;

  private AbstractInternetWidgetColumn<InternetWidget> dateien;

  public InternetWidgetColumnBuilder() {
    super();
  }

  @Override
  protected void initColumns() {
    internetWidgetIdColumn = new AbstractInternetWidgetColumn<>("widgetIdd", messages, "internet.widget.internet.id", ColumnType.STRING, AbstractInternetWidget::buildInternetId);
    internetWidgetWidgetTypColumn = new InternetWidgetTypColumn(AbstractInternetWidgetColumn.createInternetWidgetWidgetTypColumn(getMessages()), messages);
    internetWidgetStatusColumn = new InternetWidgetStatusColumn(AbstractInternetWidgetColumn.createInternetWidgetStatusColumn(getMessages()), messages);
    internetWidgetZeileColumn = AbstractInternetWidgetColumn.createInternetWidgetZeileColumn(getMessages());
    if (isSlideShow()){
      internetWidgetZeileColumn = new AbstractInternetWidgetColumn<>("InternetWidget-Zeile", messages, "internet.widget.zeile.slideshow.seite",
              ColumnType.LONG
              , InternetWidget::getZeile);
    }
    internetWidgetPositionColumn = AbstractInternetWidgetColumn.createInternetWidgetPositionColumn(getMessages());
    internetWidgetBreiteColumn = AbstractInternetWidgetColumn.createInternetWidgetBreiteColumn(getMessages());
    internetWidgetSprungmarkeColumn = AbstractInternetWidgetColumn
        .createInternetWidgetSprungmarkeColumn(getMessages());
    internetWidgetBeschreibungColumn = AbstractInternetWidgetColumn.createInternetWidgetBeschreibungColumn(getMessages());
    dateien = new AbstractInternetWidgetColumn<>("dateien", messages, InternetWidgetCustomData.MESSAGE_KEY_FILES, ColumnType.COLLECTION, iw ->
    {
      try {
        return internetWidgetExtendedDAO.attach(iw).getCustomData(InternetWidgetCustomData.class).getFiles().stream().sorted(
            Comparator.comparing(InternetWidgetFile::getPosition)).map(iwf -> iwf.getFilename() + getLabel(iwf)).collect(Collectors.toList());
      } catch (Exception e){
        // ignorieren
      }
      return Collections.emptyList();
    }
    );

  }

  private String getLabel(InternetWidgetFile iwf) {
    if (StringUtils.isBlank(iwf.getLabel())){
      return "";
    }
    return " (" + iwf.getLabel() + ")";
  }

  @Override
  protected List<ColumnEntry<? super InternetWidget>> getColumnEntriesSorted() {
    List<ColumnEntry<? super InternetWidget>> result = new ArrayList<>();
    result.add(internetWidgetSprungmarkeColumn);
    result.add(internetWidgetWidgetTypColumn);
    result.add(internetWidgetBeschreibungColumn);
    result.add(internetWidgetStatusColumn);
    result.add(internetWidgetZeileColumn);
    result.add(internetWidgetPositionColumn);
    result.add(internetWidgetBreiteColumn);
    result.add(dateien);
    result.add(internetWidgetIdColumn);
    return result;
  }

  private boolean isSlideShow(){
    return internetSeiteConversationSelect.isSelected() && internetSeiteConversationSelect.getEntity().getSeitenTyp().isSlideshow();
  }

}
