package no.uio.ifi.trackfind.frontend;

import com.vaadin.data.HasValue;
import com.vaadin.server.ThemeResource;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.shared.ui.ValueChangeMode;
import com.vaadin.ui.*;
import lombok.extern.slf4j.Slf4j;
import no.uio.ifi.trackfind.backend.configuration.TrackFindProperties;
import no.uio.ifi.trackfind.backend.data.TreeNode;
import no.uio.ifi.trackfind.backend.pojo.TfHub;
import no.uio.ifi.trackfind.backend.services.SchemaService;
import no.uio.ifi.trackfind.backend.services.TrackFindService;
import no.uio.ifi.trackfind.frontend.components.TrackFindTree;
import no.uio.ifi.trackfind.frontend.filters.TreeFilter;
import no.uio.ifi.trackfind.frontend.providers.TrackFindDataProvider;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Main Vaadin UI of the application.
 * Capable of displaying metadata for repositories, constructing and executing search queries along with exporting the results.
 * Uses custom theme (VAADIN/themes/trackfind/trackfind.scss).
 * Uses custom WidgetSet (TrackFindWidgetSet.gwt.xml).
 *
 * @author Dmytro Titov
 */
@Slf4j
public abstract class AbstractUI extends UI {

    protected SchemaService schemaService;
    protected TrackFindProperties properties;
    protected TrackFindService trackFindService;
    protected TrackFindDataProvider trackFindDataProvider;

    protected TabSheet tabSheet;

    @SuppressWarnings("unchecked")
    public TrackFindTree<TreeNode> getCurrentTree() {
        return (TrackFindTree<TreeNode>) tabSheet.getSelectedTab();
    }

    protected TfHub getCurrentHub() {
        return getCurrentTree().getHub();
    }

    protected VerticalLayout buildOuterLayout(HorizontalLayout headerLayout, HorizontalLayout mainLayout, HorizontalLayout footerLayout) {
        VerticalLayout outerLayout = new VerticalLayout(headerLayout, mainLayout, footerLayout);
        outerLayout.setSizeFull();
        outerLayout.setExpandRatio(headerLayout, 0.10f);
        outerLayout.setExpandRatio(mainLayout, 0.8f);
        outerLayout.setExpandRatio(footerLayout, 0.10f);
        outerLayout.setMargin(false);
        return outerLayout;
    }

    protected HorizontalLayout buildFooterLayout() {
        ThemeResource resource1 = new ThemeResource("images/nels.png");
        Image nels = new Image(null, resource1);
        ThemeResource resource2 = new ThemeResource("images/elixir.png");
        Image elixir = new Image(null, resource2);
        ThemeResource resource3 = new ThemeResource("images/uio.png");
        Image uio = new Image(null, resource3);
        HorizontalLayout footerLayout = new HorizontalLayout(nels, elixir, uio);
        footerLayout.setHeight("100%");
        footerLayout.setComponentAlignment(nels, Alignment.MIDDLE_LEFT);
        footerLayout.setComponentAlignment(elixir, Alignment.MIDDLE_LEFT);
        footerLayout.setComponentAlignment(uio, Alignment.MIDDLE_LEFT);
        footerLayout.setMargin(new MarginInfo(false, false, false, true));
        return footerLayout;
    }

    protected HorizontalLayout buildHeaderLayout() {
        ThemeResource resource = new ThemeResource("images/logo.png");
        Image logo = new Image(null, resource);
        HorizontalLayout headerLayout = new HorizontalLayout(logo);
        headerLayout.setSizeFull();
        headerLayout.setComponentAlignment(logo, Alignment.BOTTOM_CENTER);
        return headerLayout;
    }

    @SuppressWarnings("unchecked")
    protected TextField createFilter(boolean attributes) {
        TextField attributesFilterTextField = new TextField(attributes ? "Filter attributes" : "Filter values", (HasValue.ValueChangeListener<String>) event -> {
            TrackFindTree<TreeNode> currentTree = getCurrentTree();
            TreeFilter filter = (TreeFilter) ((TreeGrid<TreeNode>) currentTree.getCompositionRoot()).getFilter();
            if (attributes) {
                filter.setAttributesFilter(event.getValue());
            } else {
                filter.setValuesFilter(event.getValue());
            }
            currentTree.getDataProvider().refreshAll();
        });
        attributesFilterTextField.setValueChangeMode(ValueChangeMode.EAGER);
        attributesFilterTextField.setWidth(100, Unit.PERCENTAGE);
        return attributesFilterTextField;
    }

    @Autowired
    public void setSchemaService(SchemaService schemaService) {
        this.schemaService = schemaService;
    }

    @Autowired
    public void setProperties(TrackFindProperties properties) {
        this.properties = properties;
    }

    @Autowired
    public void setTrackFindService(TrackFindService trackFindService) {
        this.trackFindService = trackFindService;
    }

    @Autowired
    public void setTrackFindDataProvider(TrackFindDataProvider trackFindDataProvider) {
        this.trackFindDataProvider = trackFindDataProvider;
    }

}

@@ -66,16 +66,59 @@
 sValue;%0A
+import com.vaadin.server.ExternalResource;%0A
 import c
@@ -2272,34 +2272,107 @@
     
-ThemeResource resource1 = 
+Link nels = new Link(null, new ExternalResource(%22https://nels.bioinfo.no/%22));%0A        nels.setIcon(
 new 
@@ -2399,24 +2399,25 @@
 s/nels.png%22)
+)
 ;%0A        Im
@@ -2418,83 +2418,117 @@
     
-Image nels
+Link elixir
  = new 
-Image
+Link
 (null, 
-resource1);%0A        ThemeResource resource2 = 
+new ExternalResource(%22https://www.elixir-norway.org/%22));%0A        elixir.setIcon(
 new 
@@ -2557,24 +2557,25 @@
 elixir.png%22)
+)
 ;%0A        Im
@@ -2576,167 +2576,170 @@
     
-Image elixir
+Link uio
  = new 
-Image
+Link
 (null, 
-resource2);%0A        ThemeResource resource3 = new ThemeResource(%22images/uio.png%22);%0A        Image uio = new Image(null, resource3
+new ExternalResource(%22https://www.mn.uio.no/ifi/english/research/groups/bmi/%22));%0A        uio.setIcon(new ThemeResource(%22images/uio.png%22)
 );%0A 

Make branding badges be links