/**
 *#########################################################################
 *
 * A component of the Gatherer application, part of the Greenstone digital
 * library suite from the New Zealand Digital Library Project at the
 * University of Waikato, New Zealand.
 *
 * <BR><BR>
 *
 * Author: John Thompson, NZDL Project, University of Waikato
 *
 * <BR><BR>
 *
 * Copyright (C) 2005 New Zealand Digital Library Project
 *
 * <BR><BR>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * <BR><BR>
 *
 * This program 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 General Public License for more details.
 *
 * <BR><BR>
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *########################################################################
 */
package org.greenstone.gatherer.file;

import java.io.File;
import javax.swing.*;
import org.greenstone.gatherer.Dictionary;
import org.greenstone.gatherer.Gatherer;
import org.greenstone.gatherer.collection.CollectionTree;
import org.greenstone.gatherer.collection.CollectionTreeNode;
import org.greenstone.gatherer.gui.ExplodeMetadataDatabasePrompt;
import org.greenstone.gatherer.gui.GProgressBar;
import org.greenstone.gatherer.gui.NewFolderOrFilePrompt;
import org.greenstone.gatherer.gui.RenamePrompt;
import org.greenstone.gatherer.gui.tree.DragTree;
import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
import org.greenstone.gatherer.util.DragComponent;
import org.greenstone.gatherer.util.Utility;

/** Manages the moving of files within a separate thread.
 * @author John Thompson, NZDL Project, University of Waikato
 */
public class FileManager
{
    /** Not only the queue of files to be moved, but also the object that moves them. */
    static private FileQueue file_queue = null;

    public static int FILE_TYPE = 0;
    public static int FOLDER_TYPE = 1;
    protected static File startup_directory = null;


    /** Constructor.
     * @see org.greenstone.gatherer.file.FileQueue
     */
    public FileManager()
    {
	file_queue = new FileQueue();
	file_queue.start();
    }


    /** Determine what action should be carried out by the file queue, and add all of the necessary file jobs. */
    public void action(DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node)
    {
	// Check there is something to do
	if (source_nodes == null || source_nodes.length == 0) {
	    return;
	}

	// We need a unique ID for each file task
	long id = System.currentTimeMillis();

	// If source and target are the same we're moving
	if (source == target) {
	    // Start a new move FileTask and we're done
	    (new FileTask(id, source, source_nodes, target, target_node, FileJob.MOVE)).start();
	    return;
	}

	// If target isn't the RecycleBin, we're copying
	if (!(target instanceof RecycleBin)) {
	    // Start a new copy FileTask and we're done
	    (new FileTask(id, source, source_nodes, target, target_node, FileJob.COPY)).start();
	    return;
	}

	// We're deleting... but first make sure source isn't read-only
	boolean read_only_source = false;

	// The workspace tree is read-only...
	if (source.toString().equals("Workspace")) {
	    read_only_source = true;

	    // ...except for files from the "Downloaded Files" folder
	    String downloaded_files_folder_path = Gatherer.getGLIUserCacheDirectoryPath();
	    for (int i = 0; i < source_nodes.length; i++) {
		// Is this the "Downloaded Files" folder?
		if (source_nodes[i].getFile().getAbsolutePath().startsWith(downloaded_files_folder_path)) {
		    read_only_source = false;
		}
	    }
	}

	// The source is read-only, so tell the user and abort
	if (read_only_source) {
	    JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Read_Only"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
	    return;
	}

	// Start a new delete FileTask and we're done
	(new FileTask(id, source, source_nodes, target, target_node, FileJob.DELETE)).start();
    }


    /** Retrieves the file queue object. */
    public FileQueue getQueue()
    {
	return file_queue;
    }


    private class FileTask
	extends Thread
    {
	private long id;
	private DragComponent source;
	private FileNode[] source_nodes;
	private DragComponent target;
	private FileNode target_node;
	private byte type;


	public FileTask(long id, DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node, byte type)
	{
	    this.id = id;
	    this.source = source;
	    this.source_nodes = source_nodes;
	    this.target = target;
	    this.target_node = target_node;
	    this.type = type;
	}


	public void run()
	{
	    // Reset the progress bar and set it to indeterminate while calculating its size
	    GProgressBar progress_bar = file_queue.getProgressBar();
	    progress_bar.reset();
	    progress_bar.setIndeterminate(true);

	    // Calculate the progress bar size
	    boolean cancelled = file_queue.calculateSize(source_nodes);
	    if (!cancelled) {
		file_queue.addJob(id, source, source_nodes, target, target_node, type);
		if (Gatherer.isGsdlRemote) {
		    String collection_name = Gatherer.c_man.getCollection().getName();

		    // Perform the appropriate action based on the job type (RemoteGreenstoneServer will queue)
		    if (type == FileJob.COPY) {
			// Copies: upload all the files at once in one zip file
			File[] source_files = new File[source_nodes.length];
			for (int i = 0; i < source_nodes.length; i++) {
			    source_files[i] = source_nodes[i].getFile();
			}
			RemoteGreenstoneServer.uploadFilesIntoCollection(collection_name, source_files, target_node.getFile());
		    }
		    else if (type == FileJob.DELETE) {
			// Deletes: delete each top-level file/directory one at a time
			for (int i = 0; i < source_nodes.length; i++) {
			    RemoteGreenstoneServer.deleteCollectionFile(collection_name, source_nodes[i].getFile());
			}
		    }
		    else if (type == FileJob.MOVE) {
			// Moves: move each top-level file/directory one at a time
			for (int i = 0; i < source_nodes.length; i++) {
			    RemoteGreenstoneServer.moveCollectionFile(collection_name, source_nodes[i].getFile(), target_node.getFile());
			}
		    }
		}
	    }

	    progress_bar.setIndeterminate(false);
	    progress_bar.clear();
	}
    }


    public void explodeMetadataDatabase(File file)
    {
	// This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
	new ExplodeMetadataDatabasePromptTask(file).start();
    }


    private class ExplodeMetadataDatabasePromptTask
	extends Thread
    {
	private File metadata_database_file = null;

	public ExplodeMetadataDatabasePromptTask(File metadata_database_file)
	{
	    this.metadata_database_file = metadata_database_file;
	}

	public void run()
	{
	    ExplodeMetadataDatabasePrompt emp = new ExplodeMetadataDatabasePrompt(metadata_database_file);
	}
    }


    public void openFileInExternalApplication(File file)
    {
	// This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
	new OpenFileInExternalApplicationTask(file).start();
    }


    private class OpenFileInExternalApplicationTask
	extends Thread
    {
	private File file = null;

	public OpenFileInExternalApplicationTask(File file)
	{
	    this.file = file;
	}

	public void run()
	{
	    // If we're using a remote Greenstone server, we need to download the file before viewing it...
	    if (Gatherer.isGsdlRemote) {
		// ... but only if it is inside the collection and we haven't already downloaded it
		if (file.getAbsolutePath().startsWith(Gatherer.getCollectDirectoryPath()) && file.length() == 0) {
		    if (RemoteGreenstoneServer.downloadCollectionFile(Gatherer.c_man.getCollection().getName(), file).equals("")) {
			// Something has gone wrong downloading the file
			return;
		    }
		}
	    }

	    // View the file in an external application
	    Gatherer.spawnApplication(file);
	}
    }


    public void newDummyDoc(DragTree tree, CollectionTreeNode parent_node){
	newFolderOrDummyDoc(tree, parent_node, FILE_TYPE);
    }


    public void newFolder(DragTree tree, CollectionTreeNode parent_node) {
	newFolderOrDummyDoc(tree, parent_node, FOLDER_TYPE);
    }


    protected void newFolderOrDummyDoc(DragTree tree, CollectionTreeNode parent_node, int type) {
	(new NewFolderOrDummyDocumentTask(tree, parent_node, type)).start();
    }


    private class NewFolderOrDummyDocumentTask
	extends Thread
    {
	private DragTree tree = null;
	private CollectionTreeNode parent_node = null;
	private int type;

	public NewFolderOrDummyDocumentTask(DragTree tree, CollectionTreeNode parent_node, int type)
	{
	    this.tree = tree;
	    this.parent_node = parent_node;
	    this.type = type;
	}

	public void run()
	{
	    // Ask the user for the directories name.
	    String extension = "";
	    if (type == FILE_TYPE) {
		extension = ".nul";
	    }

	    NewFolderOrFilePrompt new_folder_prompt = new NewFolderOrFilePrompt(parent_node, type, extension);
	    String name = new_folder_prompt.display();
	    new_folder_prompt.dispose();
	    new_folder_prompt = null;

	    // And if the name is non-null...
	    if (name != null) {
		FileSystemModel model = (FileSystemModel) tree.getModel();
		File folder_file = new File(parent_node.getFile(), name);

		//... check if it already exists.
		if (folder_file.exists()) { 
		    if (type == FILE_TYPE) {
			JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Already_Exists_No_Create", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
		    }
		    else {
			JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Already_Exists", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
		    }
		}
		// Otherwise create it.
		else {
		    try {
			if (type == FILE_TYPE) {
			    folder_file.createNewFile();
			    if (Gatherer.isGsdlRemote) {
				RemoteGreenstoneServer.uploadCollectionFile(Gatherer.c_man.getCollection().getName(), folder_file);
			    }
			}
			else {
			    folder_file.mkdirs();
			    if (Gatherer.isGsdlRemote) {
				RemoteGreenstoneServer.newCollectionDirectory(Gatherer.c_man.getCollection().getName(), folder_file);
			    }
			}

			// Update the parent node to show the new folder
			parent_node.refresh();

			// Refresh workspace tree (collection tree is done automatically)
			Gatherer.g_man.refreshWorkspaceTree(DragTree.COLLECTION_CONTENTS_CHANGED);
		    }
		    catch (Exception exception) {
			if (type == FILE_TYPE) {
			    JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
			}
			else {
			    JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
			}
		    }
		}
		
		folder_file = null;
		model = null;
	    }
	    name = null;
	}
    }


    public void renameCollectionFile(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
    {
	// This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
	new RenameTask(collection_tree, collection_tree_node).start();
    }


    private class RenameTask
	extends Thread
    {
	private CollectionTree collection_tree = null;
	private CollectionTreeNode collection_tree_node = null;

	public RenameTask(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
	{
	    this.collection_tree = collection_tree;
	    this.collection_tree_node = collection_tree_node;
	}

	public void run()
	{
	    RenamePrompt rename_prompt = new RenamePrompt(collection_tree_node);
	    String new_collection_file_name = rename_prompt.display();
	    rename_prompt.dispose();
	    rename_prompt = null;

	    if (new_collection_file_name != null) {
		File collection_file = collection_tree_node.getFile();
		File new_collection_file = new File(collection_file.getParentFile(), new_collection_file_name);
		CollectionTreeNode new_collection_tree_node = new CollectionTreeNode(new_collection_file);
		file_queue.addJob(System.currentTimeMillis(), collection_tree, new FileNode[] { collection_tree_node }, collection_tree, new_collection_tree_node, FileJob.RENAME);
	    }
	}
    }

    public void replaceCollectionFile(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
    {
	// This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
	new ReplaceTask(collection_tree, collection_tree_node).start();
    }


    private class ReplaceTask
	extends Thread
    {
	private CollectionTree collection_tree = null;
	private CollectionTreeNode collection_tree_node = null;

	public ReplaceTask(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
	{
	    this.collection_tree = collection_tree;
	    this.collection_tree_node = collection_tree_node;
	}

	public void run()
	{

	    JFileChooser file_chooser = new JFileChooser(startup_directory);
	    file_chooser.setDialogTitle(Dictionary.get("ReplacePrompt.Title"));
	    File new_file = null;
	    int return_val = file_chooser.showOpenDialog(null);
	    if(return_val == JFileChooser.APPROVE_OPTION) {
		new_file = file_chooser.getSelectedFile();
	    }
	    
	    if (new_file == null) {
		return;
	    }
	    
	    // save the search path for next time
	    startup_directory = new_file.getParentFile();
	    // make up a node for the file to bring in
	    WorkspaceTreeNode source_node = new WorkspaceTreeNode(new_file);

	    File target_directory = collection_tree_node.getFile().getParentFile();
	    CollectionTreeNode new_collection_tree_node = new CollectionTreeNode(new File(target_directory, new_file.getName()));
	    // copy the new file in - but don't bring metadata
	    file_queue.addJob(System.currentTimeMillis(), Gatherer.g_man.gather_pane.workspace_tree, new FileNode[] { source_node }, collection_tree, (FileNode)collection_tree_node.getParent(),  FileJob.COPY_FILE_ONLY);
	    // do a replace of old file with new file
	    file_queue.addJob(System.currentTimeMillis(), collection_tree, new FileNode[] { collection_tree_node }, collection_tree, new_collection_tree_node, FileJob.REPLACE);

	}
    }
    
}




