package org.gcube.application.geoportal;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.UUID;

import org.gcube.application.geoportal.model.content.GeoServerContent;
import org.gcube.application.geoportal.model.fault.SDIInteractionException;
import org.gcube.application.geoportal.model.gis.SDILayerDescriptor;
import org.gcube.application.geoportal.storage.SDIManager;
import org.gcube.application.geoportal.utils.Files;
import org.gcube.application.geoportal.utils.Workspace;
import org.gcube.common.storagehub.client.dsl.FileContainer;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.gcube.spatial.data.gis.GISInterface;
import org.geotoolkit.storage.DataStoreException;

import it.geosolutions.geoserver.rest.GeoServerRESTReader;
import it.geosolutions.geoserver.rest.decoder.RESTFeatureType;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;

public class TestSDI {

	public static void main(String[] args) throws Exception {
		TokenSetter.set("/gcube/devNext/NextNext");
		
//		String workspaceName="gna_conc_"+UUID.randomUUID().toString();
//
//		FileContainer file=Workspace.getClient().getWSRoot().uploadFile(
//				new FileInputStream(Files.getFileFromResources("concessioni/pos.shp")), "posizionamento", "test layer");
//		
//		SDIManager sdiManager=new SDIManager();
		
//		sdiManager.createWorkspace(workspaceName);
//		sdiManager.configureCentroidLayer("centroids_concessioni", "gna:test", "gna_test_postgis");
//		sdiManager.publishShapeFile(workspaceName, file.getPublicLink());
		
		
//		
//		GeoServerContent content=sdiManager.pushLayerFile(Files.getFileFromResources("concessioni/pos.shp"), "pos.shp", 123, 1);
//		System.out.println("content is "+content);
//		sdiManager.publishShapeFileDirectory(workspaceName, content.getGeoserverPath());

		
		GISInterface gis=GISInterface.get();
		String workspace="gna";
		String toSetLayerName="centroids_concessioni";
		String geoserverHostName=new URL(gis.getCurrentGeoServer().getUrl()).getHost();
		
		GeoServerRESTReader reader=gis.getCurrentGeoServer().getReader();
		RESTLayer l=reader.getLayer(workspace, toSetLayerName);
		RESTFeatureType f= reader.getFeatureType(l);
		
		System.out.println(String.format("http://%1$s/geoserver/%2$s/wms?"
				+"service=WMS&version=1.1.0&request=GetMap&layers=%2$s:%3$s&"
				+ "styles=&bbox=%4$f,%5$f,%6$f,%7$f&srs=%8$s&format=application/openlayers&width=%9$d&height=%10$d",
						geoserverHostName,
						workspace,
						toSetLayerName,
						f.getMinX(),
						f.getMinY(),
						f.getMaxX(),
						f.getMaxY(),
						"EPSG:4326",
						400,
						400)
		);
		
	}

}
