package com.vaadin.demo.coverflow;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.URL;
import java.nio.CharBuffer;

import com.vaadin.data.Property;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.ExpandLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Layout.AlignmentHandler;

public class CoverflowApplication extends com.vaadin.Application {

  Coverflow covers = new Coverflow();

  public void init() {

    setMainWindow(new Window("Coverflow", createMainLayout()));

    setTheme("black");

    addSlidesToCoverflow();
  }

  private ExpandLayout createMainLayout() {

    // Initialize coverflow component
    covers.setHeight(150);
    covers.setWidth(100, Sizeable.UNITS_PERCENTAGE);
    covers.setBackgroundColor(000100100100);

    // Initialize visible slide viewer
    final Embedded visibleSlide = new Embedded();
    visibleSlide.setHeight(100, Sizeable.UNITS_PERCENTAGE);

    // Listen to coverflow changes as change slides when needed
    covers.addListener(new Property.ValueChangeListener() {
      public void valueChange(Property.ValueChangeEvent event) {
        visibleSlide.setSource((Resourcecovers.getValue());
      }
    });

    // Show sources button
    Button showSrc = new Button("src"new Button.ClickListener() {
      public void buttonClick(ClickEvent event) {
        Window srcWindow = new Window("Source code");
        srcWindow.setWidth(700);
        srcWindow.setHeight(500);
        Label l = new Label(getSourceCodeForThisClass(),
            Label.CONTENT_XHTML);
        srcWindow.addComponent(l);
        getMainWindow().addWindow(srcWindow);
      }
    });

    // Initialize main layout
    ExpandLayout layout = new ExpandLayout(
        ExpandLayout.ORIENTATION_VERTICAL);
    layout.addComponent(showSrc);
    layout.setComponentAlignment(showSrc, AlignmentHandler.ALIGNMENT_RIGHT,
        AlignmentHandler.ALIGNMENT_TOP);
    layout.addComponent(visibleSlide);
    layout.setComponentAlignment(visibleSlide,
        AlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER,
        AlignmentHandler.ALIGNMENT_TOP);
    layout.addComponent(covers);
    layout.setComponentAlignment(covers,
        AlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER,
        AlignmentHandler.ALIGNMENT_TOP);
    layout.expand(visibleSlide);
    layout.setSizeFull();

    return layout;
  }

  private String getSourceCodeForThisClass() {
    String code = "Could not find source-file";
    try {
      InputStream is = this.getClass().getResource(
          "CoverflowApplication.html").openStream();
      BufferedReader r = new BufferedReader(new InputStreamReader(is));
      StringBuffer buf = new StringBuffer();
      String line;
      while ((line = r.readLine()) != null) {
        buf.append(line);
      }
      code = buf.toString();
    catch (IOException ignored) {
    }
    return code;
  }

  private void addSlidesToCoverflow() {
    for (int i = 1; i <= 22; i++) {
      String head = "../../../IMAGES/";
      String tail = "slideshow-example.0" ((i < 10"0" ""+ i
          ".jpg";
      ThemeResource slide = new ThemeResource(head + tail);
      covers.addItem(slide);
      covers.setItemIcon(slide,
          new ThemeResource(head + "thumbs/" + tail));
    }
  }
}