// 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.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.lang.reflect.Type;

import com.google.gson.reflect.TypeToken;
import com.google.gson.Gson;

public class ClientMain {
    private static String interact(String method, String urlstr, String typ, String body) {
        try {
            HttpURLConnection con = (HttpURLConnection)(new URL(urlstr)).openConnection();
            con.setRequestMethod(method);
            con.addRequestProperty("accept",  typ);
            if(body != null) {
                con.addRequestProperty("content-type", typ);
                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 testGetOne(String urlstr, int v) {
        String response = interact("GET", urlstr + "/" + v, "application/json", null);
        Gson gson = new Gson();
        Data d = gson.fromJson(response, Data.class);
        System.out.println(d);
    }
    private static void testGetAll(String urlstr) {
        String response = interact("GET", urlstr, "application/json", null);
        Gson gson = new Gson();
        Type t = new TypeToken<TreeMap<String,ArrayList<Data>>>(){}.getType();
        Map<String,List<Data>> result = gson.fromJson(response, t);
        System.out.println(result.get("data"));
    }
    private static void testGetSome(String urlstr, int start, int finish) {
        String response = interact("GET", urlstr + "?start=" + start + "&finish=" + finish, "application/json", null);
        Gson gson = new Gson();
        Type t = new TypeToken<TreeMap<String,ArrayList<Data>>>(){}.getType();
        Map<String,List<Data>> result = gson.fromJson(response, t);
        System.out.println(result.get("data"));
    }
    private static void testAdd(String urlstr, Data d1) {
        Gson  gson = new Gson();
        String request = gson.toJson(d1);
        String response = interact("POST", urlstr + "/", "application/json", request);
        Data d2 = gson.fromJson(response, Data.class);
        System.out.println(d2);
    }
    private static void testUpdate(String urlstr, int v) {
        String response = interact("GET", urlstr + "/" + v, "application/json", null);
        Gson gson = new Gson();
        Data d = gson.fromJson(response, Data.class);
        d.setXv(d.getXv() + 0.01);
        d.setSv(d.getSv() + " - updated");
        String request = gson.toJson(d);
        interact("PUT", urlstr + "/" + v, "application/json", request);
    }
    private static void testRemove(String urlstr, int v) {
        interact("DELETE", urlstr + "/" + v, "application/json", null);
    }
    private static void test(String urlstr) {
        testGetOne(urlstr, 2);
        testGetAll(urlstr);
        testGetSome(urlstr, 2, 3);
        testAdd(urlstr, new Data(4, 4.4, "Text #4"));
        testGetAll(urlstr);
        testGetSome(urlstr, 2, 3);
        testUpdate(urlstr, 2);
        testGetAll(urlstr);
        testRemove(urlstr, 4);
        testGetAll(urlstr);
    }
    public static void main(String[] args) {
        test("http://localhost:8080/api/data");
        //test("http://localhost:8080/rest/api/data");
    }
}
