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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
***REMOVED***
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
***REMOVED***

import org.gcube.dataanalysis.dataminer.poolmanager.clients.configuration.Configuration;
import org.gcube.dataanalysis.dataminer.poolmanager.clients.configuration.DMPMClientConfiguratorManager;
***REMOVED***
***REMOVED***
***REMOVED***

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;

public class CheckMethod ***REMOVED***
	
	private Logger logger;

	public CheckMethod() 
	***REMOVED***
		this.logger = LoggerFactory.getLogger(CheckMethod.class);
***REMOVED***
	
	public boolean checkMethod(String machine, String token) throws Exception ***REMOVED***
		try ***REMOVED***
			this.logger.debug("Checking method for machine "+machine);
			this.logger.debug("By using tocken "+token);
			System.out.println("Machine: " + machine);
			String getCapabilitesRequest = new String();
			String getCapabilitesResponse = new String();
			System.out.println("   Token: " + token);
			String request = "http:***REMOVED***" + machine
					+ "/wps/WebProcessingService?Request=GetCapabilities&Service=WPS&gcube-token=" + token;
			String response = machine + "___" + token + ".xml";
			getCapabilitesRequest = request;
			getCapabilitesResponse = response;
			String baseDescriptionRequest = "http:***REMOVED***" + machine
					+ "/wps/WebProcessingService?Request=DescribeProcess&Service=WPS&Version=1.0.0" + "&gcube-token="
					+ token + "&Identifier=";
			URL requestURL = new URL(request);
			this.logger.debug("Request url "+requestURL.toString());
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(requestURL.openStream()));
			FileWriter fileWriter = new FileWriter(response);
			String line;
			boolean flag = true;
			this.logger.debug("Writing file");
			while (flag && (line = bufferedReader.readLine()) != null) ***REMOVED***
				this.logger.debug(line);
				fileWriter.write(line);
				fileWriter.write(System.lineSeparator());
				
				if (line.contains("ows:Identifier")) 
				***REMOVED***
					this.logger.debug("Identifier found");
					String operatorName = line.substring(line.indexOf(">") + 1);
					operatorName = operatorName.substring(0, operatorName.indexOf("<"));
					this.logger.debug("Operator "+operatorName);
					System.out.println("      " + operatorName);
					URL innerRequestURL = new URL(baseDescriptionRequest + operatorName);
					BufferedReader innerBufferedReader = new BufferedReader(
							new InputStreamReader(innerRequestURL.openStream()));
					String innerLine = innerBufferedReader.readLine();
					this.logger.debug("Inner line "+innerLine);
					boolean innerFlag = true;
					while (innerFlag && (innerLine = innerBufferedReader.readLine()) != null) 
					***REMOVED***
						if (innerLine.contains("ows:Abstract")) 
						***REMOVED***
							this.logger.debug("Abstract found");
							String operatorDescription = innerLine.substring(innerLine.indexOf(">") + 1);
							operatorDescription = operatorDescription.substring(0, operatorDescription.indexOf("<"));
							this.logger.debug("Operator descriptor "+operatorDescription);
							System.out.println("         " + operatorDescription);
							innerFlag = false;
					***REMOVED*** else if (innerLine.contains("ows:ExceptionText")) 
						***REMOVED***
							this.logger.debug("Exception found");
							System.out.println("         " + "error retrieving operator description");
							innerFlag = false;
							flag = false;
					***REMOVED*** else
						***REMOVED***
							innerLine = innerBufferedReader.readLine();
							this.logger.debug("Inner line completed "+innerLine);
					***REMOVED***
				***REMOVED***
			***REMOVED***
		***REMOVED***
			
			this.logger.debug("Operation successful");
			fileWriter.close();
			return true;

	***REMOVED*** catch (Exception e) ***REMOVED***
			e.getMessage();
			this.logger.error("Error "+e.getMessage(),e);
			return false;
	***REMOVED***
***REMOVED***
	
	
	
	public boolean algoExists(Algorithm algo/*, String env*/) throws Exception***REMOVED***

		this.logger.debug("Looking if algo "+algo.getName()+ " exists");
		Configuration stagingConfiguration = DMPMClientConfiguratorManager.getInstance().getStagingConfiguration(); 
		File file = new File(stagingConfiguration.getGhostAlgoDirectory()+"/"+algo.getName()+".jar");
		File file2 = new File(stagingConfiguration.getGhostAlgoDirectory()+"/"+algo.getName()+"_interface.jar");
		this.logger.debug("Looking for files "+file.getPath()+ " "+file.getPath());
		
		System.out.println("First file is located to: "+file.getPath());
		System.out.println("Second file is located to: "+file2.getPath());
		
		
		if ((this.doesExist(file.getPath()/*,env*/)) && (this.doesExist(file2.getPath()/*,env*/)))
		***REMOVED***
			this.logger.debug("Files found");
			this.copyFromDmToSVN(file/*,env*/);
			this.copyFromDmToSVN(file2/*,env*/);
			System.out.println("Files have been copied to SVN");


			return true;
	***REMOVED***
		else 
		***REMOVED***
			this.logger.debug("Files not found");
			System.out.println("Algorithm "+algo.getName()+".jar"+ " and "+algo.getName()+"_interface.jar files are not present at the expected path");
			return false;
	***REMOVED***
		
***REMOVED***
	
	
	public boolean algoExistsProd(Algorithm a/*, String env*/) throws Exception***REMOVED***
		Configuration productionConfiguration = DMPMClientConfiguratorManager.getInstance().getProductionConfiguration(); 
		File file = new File(productionConfiguration.getGhostAlgoDirectory()+"/"+a.getName()+".jar");
		File file2 = new File(productionConfiguration.getGhostAlgoDirectory()+"/"+a.getName()+"_interface.jar");

		
		System.out.println("First file is located to: "+file.getPath());
		System.out.println("Second file is located to: "+file2.getPath());
		
		
		if ((this.doesExistProd(file.getPath()/*,env*/)) && (this.doesExistProd(file2.getPath()/*,env*/)))***REMOVED***
			this.copyFromDmToSVNProd(file/*,env*/);
			this.copyFromDmToSVNProd(file2/*,env*/);


			return true;
	***REMOVED***
		else 
			System.out.println("Algorithm "+a.getName()+".jar"+ " and "+a.getName()+"_interface.jar files are not present at the expected path");
			return false;		
***REMOVED***
	
	
	public void deleteFiles(Algorithm a/*,String env*/) throws Exception***REMOVED***
		JSch jsch = new JSch();
		Session session = null;
		Channel channel = null;
		ChannelSftp c = null;
		Configuration stagingConfiguration = DMPMClientConfiguratorManager.getInstance().getStagingConfiguration(); 
		System.out.println("checking existing in env: " + stagingConfiguration.getHost());
		
		File file = new File(stagingConfiguration.getGhostAlgoDirectory()+"/"+a.getName()+".jar");
		File file2 = new File(stagingConfiguration.getGhostAlgoDirectory()+"/"+a.getName()+"_interface.jar");
		
		
		System.out.println("First file is located to: "+file.getPath());
		System.out.println("Second file is located to: "+file2.getPath());
		
		
		jsch.setKnownHosts("~/.ssh/known_hosts");
		String privateKey = "~/.ssh/id_rsa";

		jsch.addIdentity(privateKey);
		System.out.println("Private Key Added.");

		session = jsch.getSession("root", stagingConfiguration.getHost());
		System.out.println("session created.");

		java.util.Properties config = new java.util.Properties();
		config.put("StrictHostKeyChecking", "no");

		session.setConfig(config);
		session.connect();

		channel = session.openChannel("sftp");
		channel.connect();
		System.out.println("shell channel connected....");

		c = (ChannelSftp) channel;

		if(doesExist(file.getPath()/*,env*/)&&(doesExist(file2.getPath()/*,env*/)))***REMOVED***
			
			c.rm(file.getPath());
			c.rm(file2.getPath());
	    	System.out.println("Both the files have been deleted");
	***REMOVED***
		else System.out.println("Files not found");
	 	channel.disconnect();
		c.disconnect();
		session.disconnect();

***REMOVED***
	
	
	public void deleteFilesProd(Algorithm a/*,String env*/) throws Exception***REMOVED***
		JSch jsch = new JSch();
		Session session = null;
		Channel channel = null;
		ChannelSftp c = null;
		Configuration productionConfiguration = DMPMClientConfiguratorManager.getInstance().getProductionConfiguration(); 
		System.out.println("checking existing in env: " + productionConfiguration.getHost());
		
		File file = new File(productionConfiguration.getGhostAlgoDirectory()+"/"+a.getName()+".jar");
		File file2 = new File(productionConfiguration.getGhostAlgoDirectory()+"/"+a.getName()+"_interface.jar");
		
		
		System.out.println("First file is located to: "+file.getPath());
		System.out.println("Second file is located to: "+file2.getPath());
		
		
		jsch.setKnownHosts("~/.ssh/known_hosts");
		String privateKey = "~/.ssh/id_rsa";

		jsch.addIdentity(privateKey);
		System.out.println("Private Key Added.");

		session = jsch.getSession("root", productionConfiguration.getHost());
		System.out.println("session created.");

		java.util.Properties config = new java.util.Properties();
		config.put("StrictHostKeyChecking", "no");

		session.setConfig(config);
		session.connect();

		channel = session.openChannel("sftp");
		channel.connect();
		System.out.println("shell channel connected....");

		c = (ChannelSftp) channel;

		if(doesExistProd(file.getPath()/*,env*/)&&(doesExistProd(file2.getPath()/*,env*/)))***REMOVED***
			
			c.rm(file.getPath());
			c.rm(file2.getPath());
	    	System.out.println("Both the files have been deleted");
	***REMOVED***
		else System.out.println("Files not found");
	 	channel.disconnect();
		c.disconnect();
		session.disconnect();

***REMOVED***
	
	
	
	public boolean doesExist(String path/*, String env*/) throws Exception ***REMOVED***
		this.logger.debug("Looking if file "+path + " exists");
		JSch jsch = new JSch();
		Session session = null;
		Channel channel = null;
		ChannelSftp c = null;
		boolean success = false;
		Configuration stagingConfiguration = DMPMClientConfiguratorManager.getInstance().getStagingConfiguration(); 
		jsch.setKnownHosts("~/.ssh/known_hosts");
		String privateKey = "~/.ssh/id_rsa";

		jsch.addIdentity(privateKey);
		System.out.println("Private Key Added.");

		this.logger.debug("Staging configuration host "+stagingConfiguration.getHost());
		session = jsch.getSession("root",stagingConfiguration.getHost() );
		System.out.println("session created.");

		java.util.Properties config = new java.util.Properties();
		config.put("StrictHostKeyChecking", "no");

		session.setConfig(config);
		session.connect();

		channel = session.openChannel("sftp");
		channel.connect();
		System.out.println("shell channel connected....");

		c = (ChannelSftp) channel;

		SftpATTRS is = null;
		System.out.println(path);

		try ***REMOVED***			
			is = c.lstat(path);
			this.logger.debug("File found");
			success = true;
	***REMOVED*** catch (SftpException e) 
		***REMOVED***
			this.logger.error("File not found",e);
			
			if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) 
			***REMOVED***
				***REMOVED*** file doesn't exist
				success = false;
		***REMOVED***
			***REMOVED***success = true; ***REMOVED*** something else went wrong
	***REMOVED***
		channel.disconnect();
		c.disconnect();
		session.disconnect();
		this.logger.debug("Operation result "+success);
		return success;

***REMOVED***
	
	
	
	public boolean doesExistProd(String path/*, String env*/) throws Exception ***REMOVED***
		JSch jsch = new JSch();
		Session session = null;
		Channel channel = null;
		ChannelSftp c = null;
		boolean success = false;
		Configuration productionConfiguration = DMPMClientConfiguratorManager.getInstance().getProductionConfiguration(); 


		jsch.setKnownHosts("~/.ssh/known_hosts");
		String privateKey = "~/.ssh/id_rsa";

		jsch.addIdentity(privateKey);
		System.out.println("Private Key Added.");

		session = jsch.getSession("root", productionConfiguration.getHost());
		System.out.println("session created.");

		java.util.Properties config = new java.util.Properties();
		config.put("StrictHostKeyChecking", "no");

		session.setConfig(config);
		session.connect();

		channel = session.openChannel("sftp");
		channel.connect();
		System.out.println("shell channel connected....");

		c = (ChannelSftp) channel;

		SftpATTRS is = null;
		System.out.println(path);

		try ***REMOVED***
			is = c.lstat(path);
			success = true;
	***REMOVED*** catch (SftpException e) ***REMOVED***
			if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) ***REMOVED***
				***REMOVED*** file doesn't exist
				success = false;
		***REMOVED***
			***REMOVED***success = true; ***REMOVED*** something else went wrong
	***REMOVED***
		channel.disconnect();
		c.disconnect();
		session.disconnect();
		return success;

***REMOVED***
	
	
	public void copyFromDmToSVN(File a/*,String env*/) throws Exception ***REMOVED***
		JSch jsch = new JSch();
		Session session = null;
	    SVNUpdater svnUpdater = new SVNUpdater();
		Configuration stagingConfiguration = DMPMClientConfiguratorManager.getInstance().getStagingConfiguration(); 


		jsch.setKnownHosts("~/.ssh/known_hosts");
		String privateKey = "~/.ssh/id_rsa";

		jsch.addIdentity(privateKey);
		System.out.println("Private Key Added.");

		session = jsch.getSession("root", stagingConfiguration.getHost());
		System.out.println("session created.");

		java.util.Properties config = new java.util.Properties();
		config.put("StrictHostKeyChecking", "no");

		session.setConfig(config);
		session.connect();
		
		Channel channel = session.openChannel("sftp");
		channel.connect();

		ChannelSftp sftp = (ChannelSftp) channel;

		sftp.cd(stagingConfiguration.getGhostAlgoDirectory());
		
		System.out.println("REMOTE : "+stagingConfiguration.getGhostAlgoDirectory()+"/"+a.getName());
		System.out.println("LOCAL : /tmp/"+a.getName());
		
		sftp.get(stagingConfiguration.getGhostAlgoDirectory()+"/"+a.getName(),"/tmp/"+a.getName());

		channel.disconnect();
		session.disconnect();
	
	    File f = new File("/tmp/"+a.getName());
		svnUpdater.updateAlgorithmFiles(f);	
		f.delete();
***REMOVED***
 
 
	
	public void copyFromDmToSVNProd(File a/*,String env*/) throws Exception ***REMOVED***
		JSch jsch = new JSch();
		Session session = null;
		SVNUpdater svnUpdater = new SVNUpdater();
		Configuration productionConfiguration = DMPMClientConfiguratorManager.getInstance().getProductionConfiguration(); 

		jsch.setKnownHosts("~/.ssh/known_hosts");
		String privateKey = "~/.ssh/id_rsa";

		jsch.addIdentity(privateKey);
		System.out.println("Private Key Added.");

		session = jsch.getSession("root", productionConfiguration.getHost());
		System.out.println("session created.");

		java.util.Properties config = new java.util.Properties();
		config.put("StrictHostKeyChecking", "no");

		session.setConfig(config);
		session.connect();
		
		Channel channel = session.openChannel("sftp");
		channel.connect();

		ChannelSftp sftp = (ChannelSftp) channel;

		sftp.cd(productionConfiguration.getGhostAlgoDirectory());
		
		System.out.println("REMOTE : "+productionConfiguration.getGhostAlgoDirectory()+"/"+a.getName());
		System.out.println("LOCAL : /tmp/"+a.getName());
		
		sftp.get(productionConfiguration.getGhostAlgoDirectory()+"/"+a.getName(),"/tmp/"+a.getName());

		channel.disconnect();
		session.disconnect();
	
	    File f = new File("/tmp/"+a.getName());
		svnUpdater.updateAlgorithmFilesProd(f);	
		f.delete();
***REMOVED***
    
	public List<String> getFiles(String a)***REMOVED***

		String[] array = a.split(",");
		ArrayList<String> list = new ArrayList<>(Arrays.asList(array));
		List<String> ls = new LinkedList<String>();
		
        for (String s: list)***REMOVED***
        	ls.add(s.trim());
        ***REMOVED***
      
     	return ls;	
***REMOVED*** 
	
	
	public static void main(String[] args) throws Exception ***REMOVED***
***REMOVED***		ServiceConfiguration a = new ServiceConfiguration();
***REMOVED***		System.out.println(a.getStagingHost());
	
	CheckMethod a = new CheckMethod();
	
	***REMOVED***a.getFiles("/trunk/data-analysis/RConfiguration/RPackagesManagement/r_deb_pkgs.txt, /trunk/data-analysis/RConfiguration/RPackagesManagement/r_cran_pkgs.txt, /trunk/data-analysis/RConfiguration/RPackagesManagement/r_github_pkgs.txt");
	
***REMOVED***	File aa = new File("OCTAVEBLACKBOX.jar");
***REMOVED***	System.out.println(aa.getName());
***REMOVED***	System.out.println(aa.getPath());
	
	
	
	
	
	
	***REMOVED***a.copyFromDmToSVN(aa);
***REMOVED***	if (a.checkMethod("dataminer-ghost-d.dev.d4science.org", "***REMOVED***"))***REMOVED***
***REMOVED***		System.out.println("AAA");***REMOVED***
***REMOVED***		
***REMOVED***	if (a.doesExist("/home/gcube/wps_algorithms/algorithms/WINDOWS_BLACK_BOX_EXAMPLE.jar"))***REMOVED***
***REMOVED***		System.out.println("BBBB");
***REMOVED***	
***REMOVED******REMOVED***
***REMOVED***	if (a.doesExist("/home/gcube/wps_algorithms/algorithms/WINDOWS_BLACK_BOX_EXAMPLE_interface.jar"))***REMOVED***
***REMOVED***		System.out.println("CCCC");***REMOVED***
***REMOVED***	
***REMOVED***	File aa = new File("/home/gcube/wps_algorithms/algorithms/RBLACKBOX_interface.jar");
***REMOVED***	a.copyFromDmToSVN(aa, "Dev");
	
***REMOVED***
	
	
***REMOVED***System.out.println(a.checkMethod("dataminer-ghost-t.pre.d4science.org",
***REMOVED***		"2eceaf27-0e22-4dbe-8075-e09eff199bf9-98187548"));

***REMOVED***System.out.println(a.checkMethod("dataminer-proto-ghost.d4science.org",
	***REMOVED***	"***REMOVED***"));

System.out.println(a.checkMethod("dataminer-ghost-d.dev.d4science.org",
	"***REMOVED***"));


***REMOVED***Algorithm aa = new Algorithm();
***REMOVED***aa.setName("UDPIPE_WRAPPER");
***REMOVED***System.out.println(a.algoExists(aa));
***REMOVED******REMOVED***
***REMOVED***ServiceConfiguration bp = new ServiceConfiguration();
***REMOVED******REMOVED***
***REMOVED***SecurityTokenProvider.instance.set("***REMOVED***");
***REMOVED******REMOVED***
***REMOVED***if (a.checkMethod(bp.getStagingHost(), SecurityTokenProvider.instance.get())&&a.algoExists(aa)); ***REMOVED***
***REMOVED***System.out.println("ciao");
***REMOVED***
***REMOVED******REMOVED***

***REMOVED***
***REMOVED***Algorithm al = new Algorithm();
***REMOVED***	al.setName("UDPIPE_WRAPPER");
***REMOVED***	a.deleteFiles(al);
	
	
	
	
	
***REMOVED***
***REMOVED***
