package con.geo;

import geomesa.core.index.Constants;
import geomesa.core.index.IndexEntry;

import java.util.*;
import java.io.*;

import org.geotools.data.*;
import org.geotools.factory.Hints;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.WKTReader2;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import com.vividsolutions.jts.io.ParseException;

public class App
{
    public static void main(String[] args)
    {    	
        try
        {
            final boolean mock = true;
            DataStore ds = createDataStore(mock);
            
            // Create accumulo type
            SimpleFeatureType simpleFeatureType =
            		createFeatureType(ds);
                       
            // Create object of accumulo type
            SimpleFeature simpleFeature =
            		createFeature(simpleFeatureType.getTypeName(), ds);
            
            FeatureIterator it = queryTest(ds);
            
            System.out.println(it.toString());
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        catch (SchemaException e)
        {
			e.printStackTrace();
		}
        catch (ParseException e)
        {
			e.printStackTrace();
		}
    }

    public static DataStore createDataStore(boolean mock) throws IOException
    {
        Map<String, Serializable> params = new HashMap<String, Serializable>();

        params.put("instanceId", "inst");
        params.put("zookeepers", "localhost:2181");
        params.put("user", "connor");
        params.put("password", "pw");
        // params.put("auths", "Table.READ,Table.WRITE");
        params.put("tableName", "tbl");
        params.put("useMock", mock ? "true" : "false");

        return DataStoreFinder.getDataStore(params);
    }

    public static SimpleFeatureType createFeatureType(DataStore dataStore)
        throws IOException, SchemaException
    {
        String featureName = "MyPoint";

        // Create the feature-type, a.k.a. schema - creates the Accumulo table if nonexistent
        String featureSchema =
            "X:java.lang.Long," +
            "Y:java.lang.Long," +
            "DATA:java.lang.Long";
        	// TODO IndexEntryType doesn't exist - did it get renamed?
            //+ IndexEntryType.getTypeSpec(); // Built-in Geomesa attrs
        
        SimpleFeatureType featureType = DataUtilities.createType(featureName, featureSchema);
        
        // TODO FAILS HERE!!!
        dataStore.createSchema(featureType);
        
        return featureType;
    }

    public static SimpleFeature createFeature(String featureName, DataStore dataStore)
    	throws IOException, ParseException
    {
    	// Fetch the feature-type corresponding to this name.
    	SimpleFeatureType featureType = dataStore.getSchema(featureName);
    	
    	// Create the feature.
    	Object[] noValues = {};

    	SimpleFeature newFeature =
    		SimpleFeatureBuilder.build(featureType, noValues, "MyPoint");
    	
    	// Initialize
    	newFeature.setDefaultGeometry((new WKTReader2()).read("POINT(45.0 49.0)"));
    	newFeature.setAttribute("XMIN", 9);
    	newFeature.setAttribute("XMAX", 42);
    	newFeature.setAttribute("YMIN", 500);
    	newFeature.setAttribute("YMAX", 600);
    	
    	// Start-date can be NULL, but end-date must not be
    	newFeature.setAttribute(Constants.SF_PROPERTY_START_TIME, new Date());
    	newFeature.setAttribute(Constants.SF_PROPERTY_END_TIME, new Date());
    	
    	return newFeature;
    }

    public static void writeFeature(
    		String featureName,
    		SimpleFeature feature,
    		DataStore ds) throws Exception
    {
    	// Get a feature store.
    	FeatureSource featureSource = ds.getFeatureSource(featureName);
    	
    	if (!(featureSource instanceof FeatureSource))
    		throw new Exception("Could not retreive feature store");
    	
    	FeatureStore featureStore = (FeatureStore) featureSource;
    	
    	// preserve the ID that we created for this feature
    	// (set the hint to FALSE to have GeoTools generate IDs)
    	feature.getUserData().put(Hints.USE_PROVIDED_FID, java.lang.Boolean.FALSE);
    	
    	DefaultFeatureCollection features = new DefaultFeatureCollection();
    	features.add(feature);
    	
    	// Write this feature collection to the data store.
    	featureStore.addFeatures(features);
    }

    public static FeatureIterator queryTest(DataStore dataStore) throws IOException
    {
    	// There should be a previously created Feature Type called "MyPoint".
    	String featureName = "MyPoint";
    	
    	FeatureSource featureSource = dataStore.getFeatureSource(featureName);
    	
    	// Perform a 'get everything' query and return the iterator.
    	FeatureCollection featureCollection = featureSource.getFeatures();
    	
    	System.out.println(featureCollection.toString());
    	
    	return featureCollection.features();
    }
}
















