/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2004-2006, Geotools Project Managment Committee (PMC)
 *    
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    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.
 */
package org.geotools.data.wms.gce;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.geotools.data.ows.WMSCapabilities;
import org.geotools.parameter.DefaultParameterDescriptorGroup;
import org.geotools.parameter.ParameterGroup;
import org.opengis.coverage.grid.Format;
import org.opengis.parameter.GeneralParameterDescriptor;
import org.opengis.parameter.ParameterValueGroup;


/**
 * GridCoverageExchangeFormat describing Capabilities of a Web Map Server.
 * 
 * <p>
 * Unlike several file system Formats this represents the abilities of a
 * dynamic Server. In addition file formats are often called by client code
 * with the specification of a single URL for processing. WMSFormat will need
 * a series of Layers.
 * </p>
 *
 * @author Richard Gould, Refractions Researach
 * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wms/src/main/java/org/geotools/data/wms/gce/WMSFormat.java $
 */
public class WMSFormat implements Format {
    /** Parsed Capabilities Document */
    private WMSCapabilities capabilities;

    /**
     * WMSFormat creation.
     *
     * @param capabilities Parsed Capabilties document from a Web Map Server
     */
    public WMSFormat(WMSCapabilities capabilities) {
        this.capabilities = capabilities;
    }

    /**
     * Determines if the input can be processed or not.
     * 
     * <p>
     * Currently it accepts WebMapServers, WMT_MS_Capabilities, and URLs and
     * Strings that point to the WMS's getCapabilities address.
     * </p>
     * 
     * <p>
     * Feedback: this seams a bit crazy?
     * </p>
     *
     * @param input DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean accepts(Object input) {
        if (input instanceof String) {
            try {
                URL url = new URL((String) input);

                return true;
            } catch (MalformedURLException e) {
                return false;
            }
        }

        if (input instanceof URL) {
            return true;
        }

        if (input instanceof WMSCapabilities) {
            return true;
        }

        return false;
    }

    /**
     * Retrive parameter metadata describing parameters required by this Web
     * Map Server.
     * 
     * <p>
     * This information should be specific enough to allow the creation of a
     * user interface.
     * </p>
     *
     * @return DOCUMENT ME!
     */
    public ParameterValueGroup getReadParameters() {
        GeneralParameterDescriptor[] params = new GeneralParameterDescriptor[16];

        WMSParameterMaker maker = new WMSParameterMaker(capabilities);
        params[0] = maker.createVersionReadParam();
        params[1] = maker.createRequestReadParam();
        params[2] = maker.createFormatReadParam();
        params[3] = maker.createHeightReadParam();
        params[4] = maker.createWidthReadParam();
        params[5] = maker.createSRSReadParam();
        params[6] = maker.createLayersReadParam();
        params[7] = maker.createBBoxMinXReadParam();
        params[8] = maker.createBBoxMinYReadParam();
        params[9] = maker.createBBoxMaxXReadParam();
        params[10] = maker.createBBoxMaxYReadParam();
        params[11] = maker.createTransparentReadParam();
        params[12] = maker.createBGColorReadParam();
        params[13] = maker.createExceptionsReadParam();
        params[14] = maker.createElevationReadParam();
        params[15] = maker.createTimeReadParam();

        Map properties = new HashMap();
        properties.put("name", capabilities.getService().getName());
        properties.put("remarks", capabilities.getService().get_abstract());
        return new ParameterGroup(new DefaultParameterDescriptorGroup(properties, params));
    }

	/* (non-Javadoc)
	 * @see org.opengis.coverage.grid.Format#getName()
	 */
	public String getName() {
		return "Web Map Server";
	}

	/* (non-Javadoc)
	 * @see org.opengis.coverage.grid.Format#getDescription()
	 */
	public String getDescription() {
		return "Web Map Server";
	}

	/* (non-Javadoc)
	 * @see org.opengis.coverage.grid.Format#getVendor()
	 */
	public String getVendor() {
		return "Open GIS Consortium";
	}

	/* (non-Javadoc)
	 * @see org.opengis.coverage.grid.Format#getDocURL()
	 */
	public String getDocURL() {
		return "http://www.opengis.org";
	}

	/* (non-Javadoc)
	 * @see org.opengis.coverage.grid.Format#getVersion()
	 */
	public String getVersion() {
		return capabilities.getVersion();
	}

	/* (non-Javadoc)
	 * @see org.opengis.coverage.grid.Format#getWriteParameters()
	 */
	public ParameterValueGroup getWriteParameters() {
		return null;
	}
}
