/*
 * Copyright (c) 2002-2004 David Keiichi Watanabe
 * davew@xlife.org
 *
 * Modified by (c) 2004-2008 heavy_baby
 * heavy_baby@users.sourceforge.jp
 *
 * 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.
 * 
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package jp.sourceforge.cabos;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.limewire.io.IpPort;

import com.limegroup.gnutella.ActivityCallback;
import com.limegroup.gnutella.Downloader;
import com.limegroup.gnutella.FileDesc;
import com.limegroup.gnutella.FileManagerEvent;
import com.limegroup.gnutella.GUID;
import com.limegroup.gnutella.RemoteFileDesc;
import com.limegroup.gnutella.Uploader;
import com.limegroup.gnutella.Uploader.UploadStatus;
import com.limegroup.gnutella.connection.ConnectionLifecycleEvent;
import com.limegroup.gnutella.connection.RoutedConnection;
import com.limegroup.gnutella.search.HostData;

public class AqEventHandler implements ActivityCallback {
    /* Instances */
    
    private static final Log LOG = LogFactory.getLog(AqEventHandler.class);
    
    protected static final Map hosts = Collections.synchronizedMap(new HashMap());
    
    protected static final Map queries = Collections.synchronizedMap(new HashMap());
    
    protected static final Map indices = Collections.synchronizedMap(new HashMap());
    
    protected static final Map responses = Collections.synchronizedMap(new HashMap());
    
    
    /* Network */
    
	public void handleAddressStateChanged() {
    }
    
	public void handleConnectionLifecycleEvent(ConnectionLifecycleEvent evt) {
		RoutedConnection c = evt.getConnection();
		if (evt.isConnectionInitializedEvent())
			AqEvent.signalEvent(AqEvent.kLWEventConnectionInitialized, c);
		else if (evt.isConnectionClosedEvent())
			AqEvent.signalEvent(AqEvent.kLWEventConnectionClosed, c);        
    }
    
    public void installationCorrupted() {
    }
    
    /* Searching */
    
	public void handleQueryResult(RemoteFileDesc rfd, HostData data,
			Set<? extends IpPort> locs) {
        AqEvent.signalEvent(AqEvent.kLWEventQueryResult, new AqResponse(rfd, data, locs));
    }
    
    public void handleQueryString(String query) {
    }
    
    /* Upload */
    
    public void addUpload(Uploader u) {
        if (u.getState() == UploadStatus.CONNECTING ||
            u.getState() == UploadStatus.UPLOADING ||
            u.getState() == UploadStatus.QUEUED)
            AqEvent.signalEvent(AqEvent.kLWEventAddUpload, u);
    }
    
    public void removeUpload(Uploader u) {
        AqEvent.signalEvent(AqEvent.kLWEventUpdateUploadStats, u);
        AqEvent.signalEvent(AqEvent.kLWEventRemoveUpload, u);
    }
    
    /* Browsing */
    
    public void browseHostFailed(GUID guid) {
        AqEvent.signalEvent(AqEvent.kLWEventBrowseHostFailed, guid);
    }
    
    /* Sharing */
    
    public void fileManagerLoading() {
    }
    
    public void fileManagerLoaded() {
    }
    
    public boolean warnAboutSharingSensitiveDirectory(final File dir) {
        return false;
    }
    
    public void handleFileEvent(FileManagerEvent evt) {
        if (evt.isAddEvent()) {
            FileDesc[] files = evt.getFileDescs();
            for (int i = 0; i < files.length; i++)
                AqEvent.signalEvent(AqEvent.kLWEventAddSharedFile, files[i]);
        }
    }
    
    public void handleSharedFileUpdate(File file) {
    }
    
    public void setAnnotateEnabled(boolean enabled) {
    }
    
    /* Notifications */
    
    public void uploadsComplete() {
    }
    
    public void restoreApplication() {
    }
    
    public boolean isQueryAlive(GUID guid) {
        return queries.containsKey(guid);
    }
    
    public void componentLoading(String component) {
    }
    
    public void acceptedIncomingChanged(boolean status) {
    }
    
    /* DownloadCallback */
    
    public void addDownload(Downloader d) {
        AqEvent.signalEvent(AqEvent.kLWEventAddDownload, d);
    }
    
    public void removeDownload(Downloader d) {
        AqEvent.signalEvent(AqEvent.kLWEventUpdateDownloadStats, d);
        AqEvent.signalEvent(AqEvent.kLWEventRemoveDownload, d);
    }
    
    public void downloadsComplete() {
    }
    
    public void showDownloads() {
    }
    
    public void promptAboutCorruptDownload(Downloader dloader) {
        dloader.discardCorruptDownload(false);
    }
    
    public String getHostValue(String key) {
        return null;
    }

}