/*
 * aTunes 2.1.0-SNAPSHOT
 * Copyright (C) 2006-2011 Alex Aranda, Sylvain Gaudard and contributors
 *
 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
 *
 * http://www.atunes.org
 * http://sourceforge.net/projects/atunes
 *
 * 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.
 *
 * 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.
 */

package net.sourceforge.atunes.kernel.modules.process;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.swing.SwingUtilities;

import net.sourceforge.atunes.Context;
import net.sourceforge.atunes.model.IProcessListener;
import net.sourceforge.atunes.model.IProgressDialog;
import net.sourceforge.atunes.model.IState;
import net.sourceforge.atunes.utils.I18nUtils;
import net.sourceforge.atunes.utils.Logger;

/**
 * A Process represents a task to be done in background. While task is being
 * executed it updates a progress dialog. Also represents a common and easy way
 * to implement new background processes extending this class
 * 
 * Note: it is not implemented using a SwingWorker since with that progress
 * dialog is not updated synchronized
 * 
 * @author fleax
 * 
 */
public abstract class AbstractProcess {

    private final class ProcessRunnable implements Runnable {
		@Override
		public void run() {
		    // Show progress dialog
		    showProgressDialog();

		    // Run process code
		    boolean ok = runProcess();

		    // Process finished: hide progress dialog
		    hideProgressDialog();

		    // If process has been canceled then execute cancel code
		    if (canceled) {
		        runCancel();
		    }

		    // Notify all listeners
		    if (listeners != null && !listeners.isEmpty()) {
		        for (IProcessListener listener : listeners) {
		            if (canceled) {
		                listener.processCanceled();
		            } else {
		                listener.processFinished(ok);
		            }
		        }
		    }
		}
	}

    /**
     * List of listeners notified when Process ends or is canceled
     */
    private volatile List<IProcessListener> listeners;

    /**
     * Flag indicating if process has been canceled
     */
    private volatile boolean canceled = false;

    /**
     * Size of this process. This can be for example the total number of files
     * to copy, to delete, ...
     */
    private volatile long processSize;

    /**
     * The dialog used to show the progress of this process
     */
    private IProgressDialog progressDialog;

    /**
     * The Swing component owner of this process. Needed to set owner of
     * progress dialog. Can be null
     */
    private Window owner;
    
    /**
     * State of app
     */
    private IState state;

    public AbstractProcess(IState state) {
		this.state = state;
	}
    
    protected IState getState() {
		return state;
	}

	/**
     * Adds a listener to this process
     * 
     * @param listener
     */
    public final void addProcessListener(IProcessListener listener) {
        if (listeners == null) {
            listeners = new CopyOnWriteArrayList<IProcessListener>();
        }
        listeners.add(listener);
    }

    /**
     * Removes a listener of this process
     * 
     * @param listener
     */
    public final void removeProcessListener(IProcessListener listener) {
        if (listeners != null) {
            listeners.remove(listener);
        }
    }

    /**
     * Returns a message to be shown in progress dialog. Processes can override
     * this method to show custom information
     * 
     * @return
     */
    protected String getProgressDialogInformation() {
        return I18nUtils.getString("PLEASE_WAIT");
    }

    /**
     * Used to cancel this process
     */
    protected final void cancelProcess() {
        canceled = true;
    }

    /**
     * Executes this process
     */
    public final void execute() {
        // Add a debug log entry
        addDebugLog(this.getClass().getName());

        // Get size of this process
        this.processSize = getProcessSize();

        // Create new thread to run process
        Thread t = new Thread(new ProcessRunnable());

        // Run...
        t.start();
    }

    /**
     * Adds a information log
     * 
     * @param o
     */
    protected final void addInfoLog(Object o) {
        Logger.info(o);
    }

    /**
     * Adds a debug log
     * 
     * @param o
     */
    protected final void addDebugLog(Object... o) {
        Logger.debug(o);
    }

    /**
     * Adds an error log
     * 
     * @param o
     */
    protected final void addErrorLog(Object o) {
        Logger.error(o);
    }

    /**
     * Returns progress dialog used in this process
     * 
     * @return
     */
    protected IProgressDialog getProgressDialog() {
        if (progressDialog == null) {
            progressDialog = (IProgressDialog) Context.getBean("progressDialog");
            progressDialog.setTitle(getProgressDialogTitle());
            progressDialog.setInfoText(getProgressDialogInformation());
            progressDialog.setCurrentProgress(0);
            progressDialog.setProgressBarValue(0);
            progressDialog.addCancelButtonActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // When cancel disable cancel button
                    cancelProcess();
                    progressDialog.disableCancelButton();
                }
            });
        }
        return progressDialog;
    }

    /**
     * Shows progress dialog
     */
    final void showProgressDialog() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    // Set process size in dialog
                    getProgressDialog().setTotalProgress(processSize);

                    // Show dialog
                    getProgressDialog().showDialog();
                }
            });
        } catch (Exception e) {
            Logger.error(e);
        }
    }

    /**
     * Hide progress dialog
     */
    final void hideProgressDialog() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    getProgressDialog().hideDialog();
                }
            });
        } catch (Exception e) {
            Logger.error(e);
        }
    }

    /**
     * Sets the current progress of this process. This method should be called
     * every time process wants to increase progress indicator
     * 
     * @param currentProgress
     */
    protected final void setCurrentProgress(final long currentProgress) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    getProgressDialog().setCurrentProgress(currentProgress);
                    getProgressDialog().setProgressBarValue((int) ((currentProgress * 100.0) / processSize));
                }
            });
        } catch (Exception e) {
            Logger.error(e);
        }
    }

    /**
     * Component owner of this process
     * 
     * @param owner
     *            the owner to set
     */
    protected void setOwner(Window owner) {
        this.owner = owner;
    }

    /**
     * Returns a string for the title of progress dialog
     * 
     * @return
     */
    protected abstract String getProgressDialogTitle();

    /**
     * Code of the process
     * 
     * @return <code>true</code> if process is executed succesfully,
     *         <code>false</code>otherwise
     */
    protected abstract boolean runProcess();

    /**
     * Code to be executed after process cancelation
     */
    protected abstract void runCancel();

    /**
     * Returns process size
     * 
     * @return
     */
    protected abstract long getProcessSize();

    /**
     * @return the canceled
     */
    protected boolean isCanceled() {
        return canceled;
    }

    /**
     * @return the owner
     */
    protected Window getOwner() {
        return owner;
    }
}
