package extras;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Scanner;

import extras.urlparsers.FileParser;
import extras.urlparsers.FourChanParser;
import extras.urlparsers.SteamParser;
import extras.urlparsers.YoutubeParser;

public class URLTitles {
	
	
	public static String find(String s){
		if(s.contains("youtube.com/watch?") || s.contains("youtu.be/")) return YoutubeParser.find(s);
		if(s.contains("boards.4chan.org/") && s.contains("/thread/")) return FourChanParser.find(s);
		if(s.contains("steamcommunity.com") && (s.contains("/id/") || s.contains("/profiles/"))) return SteamParser.find(s);
		URL url;
		String title = "Title not found";
		String host = "";
		try {
			
			url = new URL(s);
			URLConnection urlc = url.openConnection();
			urlc.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
			urlc.addRequestProperty("User-Agent", "Mozilla");
			//urlc.addRequestProperty("Referer", "google.com");
			urlc.connect();
			
			
			
			
			Scanner scan = new Scanner(urlc.getInputStream());
			host = urlc.getURL().getHost();
			
			System.out.println(urlc.getContentType());
			if(!urlc.getContentType().startsWith("text/html")) {
				scan.close();
				return FileParser.find(urlc);
			}
			boolean titlefound = false;
			String titlecode = "";
			while(scan.hasNextLine()){
				String next = scan.nextLine();
				if(next.contains("<title") && !titlefound){
					titlefound = true;
					titlecode += next;
					continue;
					
				}
				if(next.contains("<TITLE") && !titlefound){
					titlefound = true;
					titlecode += next;
					continue;
					
				}
				if(titlefound){
					titlecode +=  next;
				}
				if(titlecode.length() > 200){
					titlecode += "...";
					break;
				}
				if(titlecode.contains("</title>") || titlecode.contains("</TITLE>")){
					break;
				}
				
				
			}
			if(title.length() > 100){
				title = title.substring(0, 95) + "...";
			}
			scan.close();
			if(titlecode.contains("<TITLE"))title = titlecode.split("<TITLE?>")[1];
			if(titlecode.contains("<title"))title = titlecode.split("<title?>")[1];
			if(title.contains("</TITLE>"))title = title.split("</TITLE")[0];
			if(title.contains("</title>"))title = title.split("</title")[0];
			//}
			
			if(title.equals("")) title = "Title not found";
			title = makeClean(title);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
			title = "Title not found";
		}
		title = String.format("[URL] %s (%s)",title.trim(), host);
		return title;
	}
	
	public static String makeClean(String s){
		s = s.replaceAll("&#039;","'");
		s = s.replaceAll("&#124;","|");
		return s;
	}
	
}

@@ -254,20 +254,20 @@
 parsers.
-File
+Chan
 Parser;%0A
@@ -292,23 +292,19 @@
 arsers.F
-ourChan
+ile
 Parser;%0A
@@ -542,32 +542,33 @@
 r.find(s);%0A%09%09if(
+(
 s.contains(%22boar
@@ -583,19 +583,61 @@
 .org/%22) 
+%7C%7C%0A%09%09%09%09s.contains(%22//8ch.net%22)%0A%09%09%09%09)%0A%09%09%09%09
 && 
+(
 s.contai
@@ -650,16 +650,40 @@
 hread/%22)
+ %7C%7C s.contains(%22/res/%22))
 ) return
@@ -687,12 +687,8 @@
 urn 
-Four
 Chan

fix problems occuring when title tag is in caps