package org.richfaces.realworld.util;

import java.io.Serializable;
import java.util.Date;

import javax.faces.event.ActionEvent;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.richfaces.realworld.domain.Album;
import org.richfaces.realworld.domain.Comment;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.Message;
import org.richfaces.realworld.domain.User;
import org.richfaces.realworld.tree.TreeActionManager;

@Name("modalPanelHelper")
@Scope(ScopeType.CONVERSATION)
public class ModalPanelHelper implements Serializable{
	
	private String caption;
	
	private String actionName;
	
	private boolean sendRequired;
	
	private boolean searchRequired = false;
	
	@In
	private User user; 
	
	@Out(required=false)
	private Message message;
	
	@In(create=true, required=true) @Out
	private Album album;
	
	@In(create=true, required=true) @Out
	private Image image;
	
	@In(create=true, required=true)
	private TreeActionManager treeActionManager;

	@In(create=true, required=true) @Out
	private Comment comment;

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public void initModalPanelData( String actionName, String caption, Album album){
		this.caption = caption;
		this.actionName = actionName;
		if(null != album){
			this.album = album;
		}else{
			this.album = new Album();
		}
	}
	
	public void initModalPanelData( String actionName, String caption, Image image){
		this.caption = caption;
		this.actionName = actionName;
		if(null != image){
			this.image = image;
		}else{
			this.image = new Image();
		}
	}
	
	public void initModalPanelData( String actionName, boolean sendRequired, Message message){
		this.actionName = actionName;
		this.sendRequired = sendRequired;
		this.message = new Message();
		this.message.setTheme("Re:" + message.getTheme());
		this.message.setAuthor(user);
		this.message.setOwner(message.getAuthor());
		this.message.setReaded(false);
		this.searchRequired = false;
	}
	
	public void initMessage( String actionName, boolean sendRequired, Message message, boolean userSearchRequired){
		this.actionName = actionName;
		this.sendRequired = sendRequired;
		this.searchRequired = userSearchRequired;
		this.message = new Message();
		this.message.setAuthor(user);
		this.message.setReaded(false);
	}
	
	public void initMessage(boolean sendRequired, Message message){
		this.sendRequired = sendRequired;
		this.message = message;
		this.message.setReaded(true);
		this.searchRequired = false;
	}
	
	public void initModalPanelData( String actionName, boolean sendRequired, User owner){
		this.actionName = actionName;
		this.sendRequired = sendRequired;
		this.message = new Message();
		this.message.setAuthor(user);
		this.message.setOwner(owner);
		this.message.setReaded(false);
		this.searchRequired = false;
	}
	
	public void initModalPanelData( String actionName, boolean sendRequired, User owner, String theme, String message){
		this.actionName = actionName;
		this.sendRequired = sendRequired;
		this.message = new Message();
		this.message.setAuthor(user);
		this.message.setOwner(owner);
		this.message.setReaded(false);
		this.message.setTheme(theme);
		this.message.setMessage(message);
		this.searchRequired = false;
	}
	
	public void initModalPanelData( String actionName, boolean sendRequired, User owner, String theme, String message, boolean friendshipRequest){
		this.actionName = actionName;
		this.sendRequired = sendRequired;
		this.message = new Message();
		this.message.setAuthor(user);
		this.message.setOwner(owner);
		this.message.setReaded(false);
		this.message.setTheme(theme);
		this.message.setMessage(message);
		this.message.setFriendshipRequest(friendshipRequest);
		this.searchRequired = false;
	}
	
	public void initModalPanelData( String actionName, String caption, Comment comment, Image image){
		this.caption = caption;
		this.actionName = actionName;
		if(null != comment){
			this.comment = comment;
		}else{
			this.comment = new Comment();
			this.comment.setAuthor(user);
			this.comment.setImage(image);
			this.comment.setDate(new Date());
		}
	}
	
	public void addAlbum(ActionEvent event){
		treeActionManager.addAlbum();
	}
	
	public void editAlbum(ActionEvent event){
		treeActionManager.editAlbum(this.album);
	}
	
	public void deleteAlbum(ActionEvent event){
		treeActionManager.deleteAlbum(this.album);
	}
	
	public void deleteImage(ActionEvent event){
		treeActionManager.deleteImage(this.image);
	}
	
	public void editComment(ActionEvent event){
		treeActionManager.editComment(this.comment);
	}
	
	public void addComment(ActionEvent event){
		treeActionManager.addComment(this.comment);
	}
	
	public void editImage(ActionEvent event){
		treeActionManager.editImage(this.image);
	}
	
	public void sendMessage(ActionEvent event){
		this.message.setDate(new Date());
		treeActionManager.sendMessage(this.message);
	}

	public boolean isSendRequired() {
		return sendRequired;
	}

	public void setSendRequired(boolean sendRequired) {
		this.sendRequired = sendRequired;
	}

	public boolean isSearchRequired() {
		return searchRequired;
	}

	public void setSearchRequired(boolean searchRequired) {
		this.searchRequired = searchRequired;
	}
}
