package heufybot.modules;

import heufybot.utils.FileUtils;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Searcher
{
	private String rootLogPath;
	
	public Searcher(String rootLogPath)
	{
		this.rootLogPath = rootLogPath;
	}
	
	public String firstSeen(String source, String searchTerms)
	{
		File[] logsFolder = new File(rootLogPath).listFiles();
		Arrays.sort(logsFolder);

		for(File file : logsFolder)
		{
			int dateStart = file.getPath().lastIndexOf(File.separator) + 1;
			String date = "[" + file.getPath().substring(dateStart, dateStart + 10) + "] ";
			
			String[] lines = FileUtils.readFile(file.getPath()).split("\n");
			Pattern normalPattern = Pattern.compile(".*<(.?" + searchTerms + ")> .*", Pattern.CASE_INSENSITIVE);
			Pattern actionPattern = Pattern.compile(".*\\* (" + searchTerms + ") .*", Pattern.CASE_INSENSITIVE);
			
			for(String line : lines)
			{
				Matcher matcher = normalPattern.matcher(line);
				if(matcher.find())
				{
					return date + line;
				}
				else
				{
					matcher = actionPattern.matcher(line);
					if(matcher.find())
					{
						return date + line;
					}
				}
			}
		}
		return "No user matching \"" + searchTerms + "\" was found in the logs.";
	}
	
	public String lastSeen(String source, String searchTerms, boolean includeToday)
	{
		File[] logsFolder = new File(rootLogPath).listFiles();
		Arrays.sort(logsFolder);
		
		Date today = new Date();
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String todayString = formatter.format(today);
		
		for(int i = logsFolder.length - 1; i >= 1; i--)
		{
			File file = logsFolder[i];
			
			int dateStart = file.getPath().lastIndexOf(File.separator) + 1;
			String date = "[" + file.getPath().substring(dateStart, dateStart + 10) + "] ";
			
			if(!(includeToday && date.equals(todayString)))
			{
				String[] lines = FileUtils.readFile(file.getPath()).split("\n");
				Pattern normalPattern = Pattern.compile(".*<(.?" + searchTerms + ")> .*", Pattern.CASE_INSENSITIVE);
				Pattern actionPattern = Pattern.compile(".*\\* (" + searchTerms + ") .*", Pattern.CASE_INSENSITIVE);
				
				for(String line : lines)
				{
					Matcher matcher = normalPattern.matcher(line);
					if(matcher.find())
					{
						return date + line;
					}
					else
					{
						matcher = actionPattern.matcher(line);
						if(matcher.find())
						{
							return date + line;
						}
					}
				}
			}
		}
		return "No user matching \"" + searchTerms + "\" was found in the logs.";
	}
}

@@ -1717,17 +1717,17 @@
 1; i %3E= 
-1
+0
 ; i--)%0A%09
@@ -2261,24 +2261,74 @@
 %09%09%09%0A%09%09%09%09for(
+int j = lines.length - 1; j %3E= 0; j--)%0A%09%09%09%09%7B%0A%09%09%09%09%09
 String line 
@@ -2331,22 +2331,19 @@
 ine 
-:
+=
  lines
-)%0A%09%09%09%09%7B
+%5Bj%5D;
 %0A%09%09%09

Fix LastSeen and LastSaw for real