/**
 * 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.util;

import java.io.Serializable;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Conversation;
import org.jboss.seam.core.Events;
import org.richfaces.component.UIDatascroller;
import org.richfaces.realworld.domain.Album;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.service.Constants;

@Name("conversationState")
@Scope(ScopeType.CONVERSATION)
public class ConversationState implements Serializable{

	private static final long serialVersionUID = 5656562187249324512L;
	
	@In Conversation conversation;
	private Integer selectedImageIndex = 1;
	
	private Image selectedImage;

	private Album selectedAlbum;

	public Integer getSelectedImageIndex() {
		return selectedImageIndex;
	}
	
	public void setSelectedImageIndex(Integer selectedImageIndex) {
		this.selectedImageIndex = selectedImageIndex;
	}

	public Image getSelectedImage() {
		return selectedImage;
	}

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

	public Album getSelectedAlbum() {
		return selectedAlbum;
	}

	public void setSelectedAlbum(Album selectedAlbum) {
		this.selectedAlbum = selectedAlbum;
	}
	
	public void showImage(Image image){
		setSelectedAlbum(image.getAlbum());
		setSelectedImage(image);
		Integer index = getSelectedAlbum().getIndex(getSelectedImage());
		UIComponent component = FacesContext.getCurrentInstance().getViewRoot();
		UIDatascroller scroller = (UIDatascroller)component.findComponent(Constants.DATASCROLLER_ID);
		Map<String, Object> attributes = scroller.getDataTable().getAttributes();
		attributes.put(UIDatascroller.SCROLLER_STATE_ATTRIBUTE, index+1);
		setSelectedImageIndex(index+1);
	}
	
	public void showAlbum(Album album){
		setSelectedAlbum(album);
		setSelectedImage(null);
	}
	
	public void selectAlbum(Album a){
		setSelectedAlbum(a);
		setSelectedImage(null);
	}
	
	public boolean isSelectedImage(int index){
		return getSelectedAlbum().getIndex(getSelectedImage()) == index -1;
	}
	
	public void updateSelectedItems(Image image){
		setSelectedImage(image);
		Integer index = getSelectedAlbum().getIndex(getSelectedImage());
		UIComponent component = FacesContext.getCurrentInstance().getViewRoot();
		UIDatascroller scroller = (UIDatascroller)component.findComponent(Constants.DATASCROLLER_ID);
		Map<String, Object> attributes = scroller.getDataTable().getAttributes();
		attributes.put(UIDatascroller.SCROLLER_STATE_ATTRIBUTE, index+1);
		setSelectedImageIndex(index+1);
	}
	
	public void incrementSlideShowIndex(){
		Integer index = getSelectedAlbum().getIndex(getSelectedImage());
		System.out.println("Slideshow execute");
		if(index == -1){
			Events.instance().raiseEvent("stopSlideshow");
			System.out.println("Slideshow stop");
			return;
		}
		UIComponent component = FacesContext.getCurrentInstance().getViewRoot();
		UIDatascroller scroller = (UIDatascroller)component.findComponent(Constants.DATASCROLLER_ID);
		Map<String, Object> attributes = scroller.getDataTable().getAttributes();
		attributes.put(UIDatascroller.SCROLLER_STATE_ATTRIBUTE, index+1);
		if(index == getSelectedAlbum().getImages().size() - 1){
			index=-1;
		}
		setSelectedImageIndex(index+1);
		setSelectedImage(getSelectedAlbum().getImages().get(index + 1));
	}
}
