// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;

import com.github.arteam.simplejsonrpc.client.JsonRpcClient;
import com.github.arteam.simplejsonrpc.client.Transport;

import common.Data;

public class TestClient {
	public static class MyTransport2 implements Transport {
		private String urlstr;
		public MyTransport2(String urlstr) {
			this.urlstr = urlstr;
		}
		@Override
		public String pass(String body) throws IOException {
	        HttpClient client = HttpClientBuilder.create().build();
	        HttpPost request = new HttpPost(urlstr); 
	        request.setEntity(new StringEntity(body, ContentType.APPLICATION_JSON));
	        HttpResponse response = client.execute(request);
	        StringBuilder sb = new StringBuilder();
	        BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
	        String line;
	        while((line = br.readLine()) != null) {
	            sb.append(line);
	        }
	        br.close();
	        return sb.toString();
		}
	}
	public static class MyTransport implements Transport {
		private String urlstr;
		public MyTransport(String urlstr) {
			this.urlstr = urlstr;
		}
		@Override
		public String pass(String body) throws IOException {
	        try {
				HttpURLConnection con = (HttpURLConnection)(new URL(urlstr)).openConnection();
	            con.setRequestMethod("POST");
	            con.addRequestProperty("accept",  "application/json-rpc");
                con.addRequestProperty("content-type", "application/json-rpc");
                con.setDoOutput(true);
                con.getOutputStream().write(body.getBytes());
	            StringBuilder sb = new StringBuilder();
	            con.connect();
	            if(con.getResponseCode() / 100 == 2) {
	                InputStream is = con.getInputStream();
	                byte[] b = new byte[1000];
	                int n;
	                while((n = is.read(b)) >= 0) {
	                    sb.append(new String(b, 0, n));
	                }
	                is.close();
	            } else {
	                System.out.printf("Error: %d %s\n", con.getResponseCode(), con.getResponseMessage());
	            }
	            con.disconnect();
	            return sb.toString();
	        } catch (MalformedURLException e) {
	            e.printStackTrace();
	            return null;
	        } catch (IOException e) {
	            e.printStackTrace();
	            return null;
	        }
		}
	}
	private static void test(String urlstr) {
		System.out.println(urlstr);
		JsonRpcClient client = new JsonRpcClient(new MyTransport(urlstr));
		int v = client.createRequest().method("getInt").id(1).returnAs(int.class).execute();
		System.out.println(v);
		String s = client.createRequest().method("getString").id(2).returnAs(String.class).execute();
		System.out.println(s);
		Data d = client.createRequest().method("getData").id(3).returnAs(Data.class).execute();
		System.out.println(d);
		@SuppressWarnings("unchecked")
		List<Integer> lstv = client.createRequest().method("getListOfInts").id(4).returnAs(List.class).execute();
		System.out.println(lstv);
		@SuppressWarnings("unchecked")
		List<String> lsts = client.createRequest().method("getListOfStrings").id(5).returnAs(List.class).execute();
		System.out.println(lsts);
		@SuppressWarnings("unchecked")
		List<Data> lstd = client.createRequest().method("getListOfData").id(6).returnAs(List.class).execute();
		System.out.println(lstd);
		int v1 = 123;
		int v2 = 456;
		int v3 = client.createRequest().method("add").params(v1, v2).id(7).returnAs(int.class).execute();
		System.out.println(v3);
		String s1 = "ABC";
		String s2 = "DEF";
		String s3 = client.createRequest().method("concat").params(s1, s2).id(8).returnAs(String.class).execute();
		System.out.println(s3);
		Data d1 = new Data(123, "ABC");
		Data d2 = client.createRequest().method("modify").params(d1).id(9).returnAs(Data.class).execute();
		System.out.println(d2);
	}
	public static void main(String[] args) {
		test("http://localhost:8001/test");
		test("http://localhost:8002/test");
		test("http://localhost:8003/test");
		test("http://localhost:8004/test");
		test("http://localhost:8005/test");
		test("http://localhost:8006/server.php");
		test("http://localhost:8007/server.php");
	}
}
