/**
 * 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.util.Date;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Events;
import org.richfaces.realworld.domain.Comment;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.MetaTag;
import org.richfaces.realworld.domain.User;
import org.richfaces.realworld.service.Constants;
import org.richfaces.realworld.service.FlushStrategy;
import org.richfaces.realworld.service.IImageAction;
import org.richfaces.realworld.util.DirectLinkHelper;

@Name("imageManager")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
public class ImageManager {

    @In
    IImageAction imageAction;
    
    @In
    FileManager fileManager;

    @In @Out
    Model model;

    @In
    private User user;

    public void deleteImage(Image image) {
        String imagePath = image.getPath();
        model.resetModel(NavigationEnum.ALBUM_PREVIEW, image.getAlbum().getOwner(), image.getAlbum().getShelf(), image.getAlbum(), null);
        imageAction.deleteImage(image);
        fileManager.deleteImage(imagePath);
        Events.instance().raiseEvent("imageDeleted");
    }

    public void editImage(Image image) {
        imageAction.editImage(image);
        Events.instance().raiseEvent(Constants.UPDATE_MAIN_AREA_EVENT, NavigationEnum.ALBUM_IMAGE_PREVIEW);
        Events.instance().raiseEvent("imageEdited");
    }

    @Observer(Constants.ADD_IMAGE_EVENT)
    public void addImage(Image image) {
        imageAction.addImage(image);
        model.resetModel(NavigationEnum.ALBUM_IMAGE_PREVIEW, model.getSelectedUser(), model.getSelectedShelf(), image.getAlbum(), image);
        Events.instance().raiseEvent("imageAdded");
    }

    public void addComment(Image image, String message) {
        if (null == user.getLogin()) {
            Events.instance().raiseEvent(Constants.ADD_ERROR_EVENT, new Exception("For vote on this picture you must login"));
            return;
        }if(message.trim().equals("")){
        	Events.instance().raiseEvent(Constants.ADD_ERROR_EVENT, new Exception("Comment must be not-null"));
            return;
        }
        Comment comment = new Comment();
        comment.setAuthor(user);
        comment.setImage(image);
        comment.setDate(new Date());
        comment.setMessage(message);
        imageAction.addComment(comment);
        Events.instance().raiseEvent(Constants.CLEAR_EDITOR_EVENT, "");
    }

    public void deleteComment(Comment comment) {
        imageAction.deleteComment(comment);
    }

    public List<MetaTag> popularTags() {
        return imageAction.getPopularTags();
    }
    
    public List<MetaTag> autoComplete(Object suggest) {
    	String temp = (String)suggest;
    	if(temp.trim().equals("")){
    		return null;
    	}
        return imageAction.getTagsLikeString((String)suggest);
    }
}
