/**
 * 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.ArrayList;
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.contexts.Contexts;
import org.jboss.seam.core.Events;
import org.richfaces.realworld.domain.Album;
import org.richfaces.realworld.domain.Shelf;
import org.richfaces.realworld.domain.User;
import org.richfaces.realworld.service.FlushStrategy;
import org.richfaces.realworld.service.IShelfAction;

@Name("shelfManager")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
public class ShelfManager implements Serializable {

    private static final long serialVersionUID = 2631634926126857691L;

    private List<Shelf> shelfs;

    @In
    FileManager fileManager;

    @In
    IShelfAction shelfAction;

    private String oncomplete = ";";

    @In @Out
    Model model;

    @In
    User user;

    public void initEdit() {
        Shelf shelf = new Shelf();
        Contexts.getConversationContext().set("shelf", shelf);
    }

    public void addShelf(Shelf shelf) {
        shelfAction.addShelf(shelf);
        model.resetModel(model.getMainArea(), model.getSelectedUser(), shelf, null, null);
        setOncomplete();
        Events.instance().raiseEvent("clearTree");
    }

    public List<Shelf> getAvailableShelfs(User user) {
        return user.getShelfs();
    }

    public void editShelf(Shelf shelf) {
        shelfAction.editShelf(shelf);
        model.resetModel(NavigationEnum.SHELF_PREVIEW, model.getSelectedUser(), shelf, null, null);
    }

    public void deleteShelf(Shelf shelf) {
        //Update domain model
        List<String> directoriesToDelete = new ArrayList<String>();
        for (Album a : shelf.getAlbums()) {
            directoriesToDelete.add(user.getLogin() + "/" + a.getId());
        }
        shelfAction.deleteShelf(shelf);
        deleteDirectories(directoriesToDelete);
        model.resetModel(NavigationEnum.ALL_SHELFS, model.getSelectedUser(), null, null, null);
        Events.instance().raiseEvent("clearTree");
    }

    private void deleteDirectories(List<String> directoriesToDelete) {
        fileManager.deleteDirectories(directoriesToDelete);
    }

    public List<Shelf> getShelfs() {
        if (shelfs == null) {
            shelfs = shelfAction.getShelfs(user);
        }
        return shelfs;
    }

    @Observer("clearTree")
    public void clearShelfs() {
        shelfs = null;
    }

    private void setOncomplete() {
        oncomplete = "$('shelfModalPanel').component.hide()";
    }

    public String getOncomplete() {
        return oncomplete;
    }

    public void setOncomplete(String oncomplete) {
        this.oncomplete = oncomplete;
    }

    public Long getCountUnvisitedImages(Shelf shelf) {
        if (shelf.getCountUnvisitedImages() == 0) {
            shelf.setUnvisitedImages(shelfAction.getUnvisitedImages(shelf));
        }
        return shelf.getCountUnvisitedImages();
    }
}
