package it.unical.mat.embasp.specializations.SolverPlanningDomains;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


import org.json.simple.JSONObject;

import it.unical.mat.embasp.languages.pddl.PDDLInputProgram;

public class SolverPlanningDomainsUtility {
	
	
	static String getFileAsString(String s) throws Exception {
		String everything = "";
		BufferedReader br = new BufferedReader(new FileReader(s));
		try {
			StringBuilder sb = new StringBuilder();
			String line = br.readLine();

			while (line != null) {
				sb.append(line);
				sb.append(System.lineSeparator());
				line = br.readLine();
			}
			everything = sb.toString();
		} finally {
			br.close();
		}
		return everything;
	}

	private final String solverUrl ="http://solver.planning.domains/solve";
	
	public SolverPlanningDomainsUtility() {}

	public String postJsonToURL(String json) throws MalformedURLException, IOException, PDDLException {
	     
		String result="";
        URL myurl = new URL(solverUrl);
        HttpURLConnection con = (HttpURLConnection)myurl.openConnection();
        con.setDoOutput(true);
        con.setDoInput(true);
 
        con.setRequestProperty("Content-Type", "application/json");
//        con.setRequestProperty("Accept", "application/json,text/plain");
        con.setRequestProperty("Method", "POST");
        OutputStream os = con.getOutputStream();
        os.write(json.toString().getBytes("UTF-8"));
        os.flush();
        os.close();
 
        StringBuilder sb = new StringBuilder();  
        int HttpResult =con.getResponseCode();
        if(HttpResult ==HttpURLConnection.HTTP_OK){
	        BufferedReader br = new BufferedReader(new   InputStreamReader(con.getInputStream(),"utf-8"));  
	        String line = null;
	        while ((line = br.readLine()) != null) {  
	        	sb.append(line + "\n");  
	        }
	        br.close(); 
	        result=sb.toString();
        }else{
        	throw new PDDLException("HTTP connection error, response code : "+con.getResponseCode()+" response message : "+con.getResponseMessage());
        }  
        return result;
 
    }

	
	public JSONObject createJson(final PDDLInputProgram pddlInputProgram) throws PDDLException{
		String problem=pddlInputProgram.getPDDLProblemString();
		String domain=pddlInputProgram.getPDDLDomainString();
		if(problem==""){
			String problemFile=pddlInputProgram.getPDDLProblemFile();
			if(problemFile=="")
				throw new PDDLException("Problem file not specified");
			try {
				problem=getFileAsString(problemFile);
			} catch (Exception e) {
				throw new PDDLException("Problem file : " + problemFile+" not found.");
			}
		}
		if(domain==""){
			String domainFile=pddlInputProgram.getPDDLDomainFile();
			if(domainFile=="")
				throw new PDDLException("Domain file not specified");
			try {
				domain=getFileAsString(domainFile);
			} catch (Exception e) {
				throw new PDDLException("Domain file : " + domainFile+" not found.");
			}
		}
		JSONObject obj = new JSONObject();
		obj.put("problem", problem);
		obj.put("domain", domain);
		
		return obj;
	}
	

}

@@ -1062,43 +1062,8 @@
 ows 
-MalformedURLException, IOException,
  PDD
@@ -1102,16 +1102,24 @@
 ult=%22%22;%0A
+%09%09try%7B%0A%09
         
@@ -1150,16 +1150,17 @@
 erUrl);%0A
+%09
         
@@ -1222,24 +1222,25 @@
 nnection();%0A
+%09
         con.
@@ -1258,16 +1258,17 @@
 (true);%0A
+%09
         
@@ -1289,18 +1289,20 @@
 (true);%0A
+%09
  %0A
+%09
         
@@ -1361,16 +1361,17 @@
 json%22);%0A
+%09
 //      
@@ -1433,24 +1433,25 @@
 xt/plain%22);%0A
+%09
         con.
@@ -1488,16 +1488,17 @@
 POST%22);%0A
+%09
         
@@ -1534,24 +1534,25 @@
 utStream();%0A
+%09
         os.w
@@ -1588,24 +1588,25 @@
 (%22UTF-8%22));%0A
+%09
         os.f
@@ -1609,24 +1609,25 @@
 os.flush();%0A
+%09
         os.c
@@ -1634,18 +1634,20 @@
 lose();%0A
+%09
  %0A
+%09
         
@@ -1688,16 +1688,17 @@
 er();  %0A
+%09
         
@@ -1732,24 +1732,25 @@
 onseCode();%0A
+%09
         if(H
@@ -1789,16 +1789,17 @@
 TP_OK)%7B%0A
+%09
 %09       
@@ -1892,24 +1892,25 @@
 utf-8%22));  %0A
+%09
 %09        Str
@@ -1926,16 +1926,17 @@
 = null;%0A
+%09
 %09       
@@ -1979,16 +1979,17 @@
 ll) %7B  %0A
+%09
 %09       
@@ -2012,24 +2012,25 @@
  + %22%5Cn%22);  %0A
+%09
 %09        %7D%0A%09
@@ -2028,16 +2028,17 @@
       %7D%0A
+%09
 %09       
@@ -2051,16 +2051,17 @@
 ose(); %0A
+%09
 %09       
@@ -2083,16 +2083,17 @@
 ring();%0A
+%09
         
@@ -2099,16 +2099,17 @@
  %7Delse%7B%0A
+%09
         
@@ -2247,16 +2247,17 @@
 age());%0A
+%09
         
@@ -2257,18 +2257,93 @@
        %7D
-  
+%0A%09%09%7Dcatch (Exception e) %7B%0A%09%09%09throw new PDDLException(%22Conncetion Error%22);%0A%09%09%7D
 %0A       

Add try catch in postJsonToURL for connection error