/**
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007  Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */
package org.richfaces.realworld.manager;

import java.io.Serializable;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.service.Constants;

@Name("slideshow")
@Scope(ScopeType.CONVERSATION)
public class SlideshowManager implements Serializable{

	private static final long serialVersionUID = 7801877176558409702L;
	private Integer slideshowIndex;
	private Image selectedImage;
	private List<Image> images;
	private boolean active;
	
	private int interval = Constants.INITIAL_DELAY;

	public int getInterval() {
		return interval;
	}

	public void setInterval(int interval) {
		this.interval = interval;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
	
	public void startSlideshow(List<Image> images){
		active = true;
		this.images = images;
		this.slideshowIndex = 0;
		this.selectedImage = this.images.get(this.slideshowIndex);
		this.selectedImage.getAlbum().visitImage(selectedImage);
		this.selectedImage.getAlbum().getShelf().visitImage(selectedImage);
	}
	
	public void stopSlideshow(){
		active = false;
		this.images = null;
		this.selectedImage = null;
		this.slideshowIndex = 0;
	}

	public Integer getSlideshowIndex() {
		return slideshowIndex;
	}

	public void setSlideshowIndex(Integer slideshowIndex) {
		this.slideshowIndex = slideshowIndex;
	}

	public Image getSelectedImage() {
		return selectedImage;
	}

	public void setSelectedImage(Image selectedImage) {
		this.selectedImage = selectedImage;
	}

	public List<Image> getImages() {
		return images;
	}

	public void setImages(List<Image> images) {
		this.images = images;
	}
	
	public void showNextImage(){
		if(slideshowIndex == images.size() - 1){
			slideshowIndex = -1;
		}
		slideshowIndex++;
		selectedImage = images.get(slideshowIndex);
	}
}