package org.kralandce.krapi.core;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Method;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Node;
import org.kralandce.krapi.core.CommonConst;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


/**
 * A bunch of usefull thing
 * @author hello-gitty
 *
 */
public class Util {
	
    final static Logger logger = LoggerFactory.getLogger(Util.class);
    
	// private
	private Util() {
	}

	public static String getText(Node node) {

		if (node.childNodes().size() == 0) {
			return Jsoup.parse(node.toString()).text();
		}
		return getText(node.childNode(0));
	}

	public static String getFullText(Node node) {

		if (node.childNodes().size() == 0) {
			return Jsoup.parse(node.toString()).text();
		}
		
		String result = "";
		for (Node no : node.childNodes()) {
			result +=getFullText(no) + " ";
		}
		
		return result.trim();
	}
	
	public static float parseFloat(String ss) {
		float result = 0f;

		try {
			result = Float.parseFloat(ss);
		} catch (Exception e) {
			// Rien
		}

		return result;
	}

	public static double parseDouble(String ss) {
		double result = 0d;

		try {
			result = Double.parseDouble(ss);
		} catch (Exception e) {
			// Rien
		}

		return result;
	}

	public static int parseInt(String ss) {
		return parseInt(ss, 1);
	}

	public static int parseInt(String ss, int defaut) {
		int result = defaut;

		try {
			result = Integer.parseInt(ss);
		} catch (Exception e) {
			// Rien
		}

		return result;
	}

	public static String toJson(Object oo) {
		Gson gson = new Gson();
		return gson.toJson(oo);
	}

	public static String toPrettyJson(Object oo) {
		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		return gson.toJson(oo);
	}

	public static Document getDocument(String url) throws MalformedURLException, IOException {
		return Jsoup.parse(new URL(url).openStream(), CommonConst.ISO, url);
	}
	
	
    /**
     * Get an object connection with referer and cookie setted
     * @param url url to get
     * @param cookies cookies for the new connection
     * @param referer 
     * @return a connection 
     */
    public static Connection getConnection(String url, Map<String, String> cookies, String referer) {
        Connection result = null;
        result = Jsoup.connect(url).method(Method.GET).header("Host", CommonConst.HOST_KRALAND);
        if (referer != null) {
            result.referrer(referer);
        }
        if (cookies != null) {
            for (Map.Entry<String, String> entry : cookies.entrySet()) {
                result.cookie(entry.getKey(), entry.getValue());
            }
        }
        return result;
    }
	
    /**
     * Handle authentification if needed for cookie
     * @return
     * @throws IOException
     */
    public static Map<String, String> authentificationKi() throws IOException {
        Connection.Response res = null;
        if (AuthentificationParam.AUTHENFICATION.isValue()) {
            Connection conn = getConnection(CommonConst.URL_AUTH_KRALAND, null, null);
            conn.data("p1", AuthentificationParam.KI_SLAVE_LOGIN.getValue(), "p2", AuthentificationParam.KI_SLAVE_PASS.getValue(),
                    "Submit", "Ok").method(Method.POST).execute();
            res = conn.execute();
        } else {
            res = getConnection(CommonConst.URL_KRALAND_MAIN, null, null).execute();
        }
        Map<String, String> cookies = res.cookies();

        logger.debug("cookies: ", cookies);
        return cookies;
    }
}

@@ -39,111 +39,65 @@
 ort 
-java.io.IOException;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL;%0Aimport java.util.Map;%0A
+com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder;
 %0Aimp
@@ -139,21 +139,33 @@
 g.jsoup.
-Jsoup
+Connection.Method
 ;%0Aimport
@@ -175,33 +175,21 @@
 g.jsoup.
-Connection.Method
+Jsoup
 ;%0Aimport
@@ -248,54 +248,8 @@
 de;%0A
-import org.kralandce.krapi.core.CommonConst;%0A%0A
 impo
@@ -313,64 +313,70 @@
 ort 
-com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder
+java.io.IOException;%0Aimport java.net.URL;%0Aimport java.util.Map
 ;%0A%0A%0A
@@ -457,17 +457,16 @@
  Util %7B%0A
-%09
 %0A    fin
@@ -528,20 +528,16 @@
 class);%0A
-    
 %0A%09// pri
@@ -876,18 +876,16 @@
 ();%0A%09%09%7D%0A
-%09%09
 %0A%09%09Strin
@@ -976,18 +976,16 @@
  %22;%0A%09%09%7D%0A
-%09%09
 %0A%09%09retur
@@ -1004,17 +1004,16 @@
 m();%0A%09%7D%0A
-%09
 %0A%09public
@@ -1903,31 +1903,8 @@
 rows
- MalformedURLException,
  IOE
@@ -1992,16 +1992,10 @@
 %0A%09%7D%0A
+%0A
 %09
-%0A%09%0A    
 /**%0A
@@ -2139,20 +2139,17 @@
 nection%0A
-    
+%09
  * @para
@@ -2157,22 +2157,18 @@
  referer
- %0A    
+%0A%09
  * @retu
@@ -2186,14 +2186,10 @@
 tion
- %0A    
+%0A%09
  */%0A
@@ -2726,14 +2726,10 @@
   %7D%0A
+%0A
 %09
-%0A    
 /**%0A
@@ -3497,24 +3497,18 @@
 ies();%0A%0A
-        
+%09%09
 logger.d
@@ -3522,16 +3522,18 @@
 ookies: 
+%7B%7D
 %22, cooki
@@ -3537,24 +3537,18 @@
 okies);%0A
-        
+%09%09
 return c

Fix missing placeholder in log call.