package org.gcube.dataanalysis.dataminer.poolmanager.clients;

import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
***REMOVED***
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.StringWriter;
***REMOVED***
***REMOVED***
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
***REMOVED***
import java.util.Map;
import java.util.Scanner;
***REMOVED***
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;

***REMOVED***
import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.Resources;
import org.gcube.common.resources.gcore.ServiceEndpoint;
***REMOVED***
***REMOVED***
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Cluster;
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Dependency;
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Domain;
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Host;
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.comparator.HostComparator;
***REMOVED***
import org.gcube.informationsystem.publisher.AdvancedScopedPublisher;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.gcube.informationsystem.publisher.ScopedPublisher;
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
***REMOVED***
***REMOVED***
import org.tmatesoft.svn.core.SVNDepth;
***REMOVED***
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import au.com.bytecode.opencsv.CSVReader;

public class ISClient ***REMOVED***
	private static final Logger LOGGER = LoggerFactory.getLogger(ISClient.class);


	public Host getDataminer(String hostname) ***REMOVED***
		Host h = new Host();
		boolean remote2 = true;

		if (!remote2) ***REMOVED***
			h.setName("dataminer1-devnext.d4science.org");
			return h;
	***REMOVED*** else ***REMOVED***
			***REMOVED***SimpleQuery query = queryFor(ServiceEndpoint.class);
			***REMOVED***query.addCondition("$resource/Profile/RunTime/HostedOn/text() eq '" + hostname + "'");
			***REMOVED***DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
			***REMOVED***List<ServiceEndpoint> resources = client.submit(query);
			***REMOVED***ServiceEndpoint a = resources.get(0);
			***REMOVED***h.setName(a.profile().runtime().hostedOn());
			h.setName(hostname);
	***REMOVED***
		return h;
***REMOVED***
	

	***REMOVED*** return the HProxy hostname in the VRE
	public static String getHProxy() ***REMOVED***
		Host h = new Host();
		SimpleQuery query = queryFor(ServiceEndpoint.class);
		query.addCondition("$resource/Profile/Name/text() eq 'DataMiner'");
		DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
		List<ServiceEndpoint> resources = client.submit(query);
		h.setName(resources.get(0).profile().runtime().hostedOn());
		return h.getName();
***REMOVED***


	public Collection<Host> listDataminersInVRE() ***REMOVED***

		boolean remote = false;

		if (!remote) ***REMOVED***
			Collection<Host> out = new Vector<>();
			Host h = new Host();
			***REMOVED*** h.setName("bb-dataminer.res.eng.it");
			***REMOVED*** h.setName("vm101.ui.savba.sk");
			h.setName("dataminer1-devnext.d4science.org");
			out.add(h);
			return out;
	***REMOVED*** else ***REMOVED***

			SimpleQuery query = queryFor(ServiceEndpoint.class);

			***REMOVED*** old version
			***REMOVED*** query.addCondition("$resource/Profile/Category/text() eq
			***REMOVED*** 'DataAnalysis'")
			***REMOVED*** .addCondition("$resource/Profile/Name/text() eq 'DataMiner'");

			query.addCondition("$resource/Profile/Platform/Name/text() eq 'DataMiner'");

			DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);

			List<ServiceEndpoint> resources = client.submit(query);

			Collection<Host> out = new Vector<>();
			for (ServiceEndpoint r : resources) ***REMOVED***
				Host h = new Host();
				h.setName(r.profile().runtime().hostedOn());
				out.add(h);
		***REMOVED***
			return out;
	***REMOVED***
***REMOVED***

	
	public void updateAlg(Algorithm algo) ***REMOVED***

		ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();

		SimpleQuery query = queryFor(GenericResource.class);
		query.addCondition("$resource/Profile/Name/text() eq '" + algo.getName() + "'").setResult("$resource");
		DiscoveryClient<GenericResource> client = clientFor(GenericResource.class);
		List<GenericResource> ds = client.submit(query);
		if (ds.isEmpty()) ***REMOVED***
			return;
	***REMOVED***
		GenericResource a = ds.get(0);
		a.profile().newBody(this.getAlgoBody(algo));
		try ***REMOVED***
			scopedPublisher.update(a);
	***REMOVED*** catch (RegistryNotFoundException e) ***REMOVED***
			e.printStackTrace();
	***REMOVED***
***REMOVED***
	
	
	private String getAlgoBody(Algorithm algo) ***REMOVED***
		return "<category>" + algo.getCategory() + "</category>" + "\n" + "<clazz>" + algo.getClazz() + "</clazz>"
				+ "\n" + "<algorithmType>" + algo.getAlgorithmType() + "</algorithmType>" + "\n" + "<skipJava>"
				+ algo.getSkipJava() + "</skipJava>" + "\n" + "<packageURL>" + algo.getPackageURL() + "</packageURL>"
				+ "\n" + "<dependencies>" + algo.getDependencies() + "</dependencies>";
***REMOVED***

	
	
***REMOVED***	public void addAlgToIs(Algorithm algo) ***REMOVED***
***REMOVED***		GenericResource a = new GenericResource();
***REMOVED***		a.newProfile().name(algo.getName()).type("StatisticalManagerAlgorithm").description(algo.getDescription());
***REMOVED***		a.profile().newBody(this.getAlgoBody(algo));
***REMOVED***		try ***REMOVED***
***REMOVED***			publishScopedResource(a, Arrays.asList(new String[] ***REMOVED*** ScopeProvider.instance.get() ***REMOVED***));
***REMOVED***	***REMOVED*** catch (Exception e) ***REMOVED***
***REMOVED***			e.printStackTrace();
***REMOVED***	***REMOVED***
***REMOVED******REMOVED***
	
	
	
	public void addAlgToIs(Algorithm algo, String token) ***REMOVED***
		GenericResource a = new GenericResource();
		a.newProfile().name(algo.getName()).type("StatisticalManagerAlgorithm").description(algo.getDescription());
		a.profile().newBody(this.getAlgoBody(algo));
		try ***REMOVED***
			SecurityTokenProvider.instance.set(token);
			publishScopedResource(a, Arrays.asList(new String[] ***REMOVED*** SecurityTokenProvider.instance.get() ***REMOVED***));
	***REMOVED*** catch (Exception e) ***REMOVED***
			e.printStackTrace();
	***REMOVED***
***REMOVED***
	
	public void unPublishScopedResource(GenericResource resource) throws RegistryNotFoundException, Exception ***REMOVED***
		ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
		AdvancedScopedPublisher advancedScopedPublisher = new AdvancedScopedPublisher(scopedPublisher);
		String id = resource.id();
		LOGGER.debug("Trying to remove ***REMOVED******REMOVED*** with ID ***REMOVED******REMOVED*** from ***REMOVED******REMOVED***", resource.getClass().getSimpleName(), id,
				ScopeProvider.instance.get());
		***REMOVED*** scopedPublisher.remove(resource, scopes);
		advancedScopedPublisher.forceRemove(resource);
		LOGGER.debug("***REMOVED******REMOVED*** with ID ***REMOVED******REMOVED*** removed successfully", resource.getClass().getSimpleName(), id);
***REMOVED***

	public void publishScopedResource(GenericResource a, List<String> scopes)
			throws RegistryNotFoundException, Exception ***REMOVED***
		StringWriter stringWriter = new StringWriter();
		Resources.marshal(a, stringWriter);

		ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
		try ***REMOVED***
			System.out.println(scopes);
			System.out.println(stringWriter);
			scopedPublisher.create(a, scopes);
	***REMOVED*** catch (RegistryNotFoundException e) ***REMOVED***
			System.out.println(e);
			throw e;
	***REMOVED***
***REMOVED***
	

***REMOVED***	public Set<Algorithm> getAlgoFromIs() ***REMOVED***
***REMOVED******REMOVED***
***REMOVED***
***REMOVED***		Set<Algorithm> out = new HashSet<Algorithm>();
***REMOVED***		SimpleQuery query = queryFor(GenericResource.class);
***REMOVED***		query.addCondition("$resource/Profile/SecondaryType/text() eq 'StatisticalManagerAlgorithm'")
***REMOVED***				.setResult("$resource");
***REMOVED***		DiscoveryClient<GenericResource> client = clientFor(GenericResource.class);
***REMOVED***		List<GenericResource> ds = client.submit(query);
***REMOVED***		for (GenericResource a : ds) ***REMOVED***
***REMOVED***			out.add(this.convertAlgo(a));
***REMOVED***	***REMOVED***
***REMOVED***		return out;
***REMOVED******REMOVED***

	
***REMOVED***	private Algorithm convertAlgo(GenericResource a) ***REMOVED***
***REMOVED***		Algorithm out = new Algorithm();
***REMOVED***
***REMOVED***		***REMOVED*** out.setId(a.profile().body().getElementsByTagName("id").item(0).getTextContent());
***REMOVED***		out.setAlgorithmType(a.profile().body().getElementsByTagName("algorithmType").item(0).getTextContent());
***REMOVED***		out.setCategory(a.profile().body().getElementsByTagName("category").item(0).getTextContent());
***REMOVED***		out.setClazz(a.profile().body().getElementsByTagName("clazz").item(0).getTextContent());
***REMOVED***		out.setName(a.profile().name());
***REMOVED***		out.setPackageURL(a.profile().body().getElementsByTagName("packageURL").item(0).getTextContent());
***REMOVED***		out.setSkipJava(a.profile().body().getElementsByTagName("skipJava").item(0).getTextContent());
***REMOVED***		out.setDescription(a.profile().description());
***REMOVED***
***REMOVED***		Set<org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Dependency> deps = new HashSet<org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Dependency>();
***REMOVED***		for (int i = 0; i < a.profile().body().getElementsByTagName("dependencies").getLength(); i++) ***REMOVED***
***REMOVED***			org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Dependency d1 = new org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Dependency();
***REMOVED***			d1.setName(a.profile().body().getElementsByTagName("dependencies").item(i).getTextContent());
***REMOVED***			deps.add(d1);
***REMOVED***	***REMOVED***
***REMOVED***		out.setDependencies(deps);
***REMOVED***		return out;
***REMOVED******REMOVED***
	

	
	public static void main(String[] args) throws IOException, SVNException ***REMOVED***
		ISClient a = new ISClient();
		ScopeProvider.instance.set("/gcube/devNext/NextNext");
		***REMOVED*** System.out.println(a.getHProxy());
		***REMOVED*** System.out.println(a.MapCluster());
		***REMOVED*** System.out.println(a.listDataMinersByCluster());
		***REMOVED*** System.out.println(a.listDataMinersByCluster());

		***REMOVED*** List<Dependency> list = new LinkedList<Dependency>();
		***REMOVED*** Dependency aa = new Dependency();
		***REMOVED*** aa.setName("testnunzio");
		***REMOVED*** aa.setType("cran:");
		***REMOVED*** list.add(aa);

		***REMOVED*** a.checkSVNdep();
		***REMOVED***System.out.println(a.getDataminer("dataminer1-d-d4s.d4science.org").getDomain());
		***REMOVED*** System.out.println(a.listDataminersInVRE());
***REMOVED***
***REMOVED***
