package dynamic.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class ContentGrabber {

    public static String BASE_URL = "http://www3.imperial.ac.uk/computing";

    public static String content_div_tag_open = "<div id=\"doc_info\">";
    public static String content_div_tag_close = "</div>";

    public static String GetWebContentSimple(String target_url){

        StringBuilder sb = new StringBuilder();
        try {
            URL url = new URL(target_url);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String s;
            while ((s = in.readLine()) != null) {
                sb.append(s + "\n\r");
            }
            in.close();
        } catch (IOException e){
            e.getStackTrace();
            System.exit(0);
        }
        return new String(sb);

    }



    public static String GetWebContent(String target_url, int content_count) throws IOException {
        String webs = GetWebContentSimple(BASE_URL + target_url);
        return StripContentPanel(webs, content_count);
    }

    private static String StripContentPanel(String page, int content_count) {

        int start_pos = 0;

        while(content_count-- >= 0){

            start_pos = page.indexOf(content_div_tag_open,start_pos+1);
            if(start_pos < 0) return ""; // not found

        }

        int end_pos = GetClosingDiv(page,start_pos);

        String content = page.substring(start_pos, end_pos + content_div_tag_close.length()).trim();

        int spanind = content.indexOf("<span");
        if (spanind >= 0)
            content = StripSpan(content);

        return content;

    }


    private static int GetClosingDiv(String page, int start_pos) {

        int open_divs = 1;

        while(open_divs > 0){

            int next_close = page.indexOf("</div",start_pos + 1);
            int next_open = page.indexOf("<div",start_pos + 1);

            if(next_close < 0) return start_pos;
            else if (next_open < 0 || next_open > next_close) {
                start_pos = next_close;
                open_divs--;
            }
            else {
                open_divs++;
                start_pos = next_open;
            }

        }

        return start_pos;

    }

    private static String StripSpan(String content) {

        int spanind = content.indexOf("<span");
        int spantagclose = content.indexOf(">", spanind);
        int spanclose = content.indexOf("</SPAN>");

        return content.substring(spantagclose + 1, spanclose).trim();

    }


}
