package de.tkprog.MiSeLoR;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.tkprog.log.Logger;

public class MiSeLoR {
	private Database currentDatabase;
	public static MiSeLoR THIS;
	public static Logger log;
	public static void main(String[] args){
		(new File("log/")).mkdir();
		log = new Logger("log/MiSeLoR_"+System.currentTimeMillis()+".log");
		log.setLogToCLI(true);
		log.setLogToFile(true);
		log.logAll(true);
		new MiSeLoR();
	}
	
	public MiSeLoR(){
		try {
			THIS = this;
			currentDatabase = new Database("MiSeLoR_current.db");
			Message.initialise(cD());
			SimpleMessage.initialise(cD());
			ChatMessage.initialise(cD());
			LoginMessage.initialise(cD());
			LeftGameMessage.initialise(cD());
			ServerOverloadMessage.initialise(cD());
			DeathMessage.initialise(cD());
			EarnedAchievementMessage.initialise(cD());
			JoinMessage.initialise(cD());
			LostConnectionMessage.initialise(cD());
			MovedWronglyMessage.initialise(cD());
			PlayerOnlineMessage.initialise(cD());
			SavingWorldDataMessage.initialise(cD());
			ServerChatMessage.initialise(cD());
			UUIDofPlayerIsMessage.initialise(cD());
			cmd();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void cmd() {
		boolean running = true;
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		System.out.println();
		System.out.println();
		do{
			try {
				System.out.print("> ");
				String input = bf.readLine();
				if(input != null){
					if(Pattern.matches("help|\\?|\\\\\\?|\\\\help", input)){
						System.out.println("Commands:\r\n"+
									"\thelp|?|\\?|\\help  -  Shows this help\r\n"+
									"\tparse <dd> <mm> <yyyy> <file name> - parses the given file for the given day // file name can use Regex\r\n"+
									"\tparses <file name> - parses the given file (\"yyyy-mm-dd-x.log\") for the given day // file name can use Regex\r\n"+
									"\tuptime <player name> - shows the uptime for the player\r\n"+
									"\tall - shows a summary\r\n"+
									"\tgetAllPlayer - shows all saved player\r\n"+
									"\tleaderboard - Shows some leaderboard\r\n"+
									"\texit - exits the program");
					}
					else if(Pattern.matches("parse\\s\\d{2}\\s\\d{2}\\s\\d{4}\\s(\\S*)", input)){
						Pattern p = Pattern.compile("parse\\s(\\d{2})\\s(\\d{2})\\s(\\d{4})\\s(\\S*)");
						Matcher mm = p.matcher(input);
						mm.find();
						File[] f = getFiles(mm.group(4));
						for(File ff : f){
							Commands.parse(Integer.parseInt(mm.group(1)), Integer.parseInt(mm.group(2)), Integer.parseInt(mm.group(3)), ff.getAbsolutePath());
						}
					}
					else if(Pattern.matches("parses\\s(\\S*)", input)){
						Pattern p = Pattern.compile("parses\\s(\\S*)");
						Matcher mm = p.matcher(input);
						mm.find();
						String filename = mm.group(1);
						File[] f = getFiles(filename);
						for(File ff : f){
							p = Pattern.compile("(\\d{4})\\-(\\d{2})\\-(\\d{2})\\-\\d*\\.log");
							mm = p.matcher(ff.getName());
							mm.find();
							Commands.parse(Integer.parseInt(mm.group(3)), Integer.parseInt(mm.group(2)), Integer.parseInt(mm.group(1)), ff.getAbsolutePath());
						}
					}
					else if(Pattern.matches("uptime\\s(\\S*)", input)){
						Pattern p = Pattern.compile("uptime\\s(\\S*)");
						Matcher mm = p.matcher(input);
						mm.find();
						Commands.uptime(mm.group(1));
					}
					else if(Pattern.matches("all", input)){
						Commands.all();
					}
					else if(Pattern.matches("exit", input)){
						Commands.exit();
					}
					else if(Pattern.matches("getAllPlayer", input)){
						String[] s = Commands.getAllPlayers();
						for(String ss : s){
							System.out.println(ss);
						}
					}
					else if(Pattern.matches("leaderboard", input)){
						Commands.showLeaderboard(cD());
					}
					else{
						System.out.println("The command \""+input+"\" wasn't recognized. Type in \"help\".");
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} while(running);
	}

	private File[] getFiles(String fileregex) {
		File f = new File(".");
		if(!f.isDirectory()){
			f = f.getParentFile();
		}
		if(!f.isDirectory()){
			System.err.println("Sollte ned passieren...");
		}
		File[] ff = f.listFiles();
		ArrayList<File> o = new ArrayList<File>();
		for(File fff : ff){
			if(Pattern.matches(fileregex, fff.getName())){
				o.add(fff);
			}
			else{
				System.out.println("\""+fileregex+"\" does not match \""+fff.getName()+"\"");
			}
		}
		File[] ffff = new File[o.size()];
		int i = 0;
		for(File fff : o){
			ffff[i] = fff;
			i++;
		}
		return ffff;
	}

	public Database cD() {
		return getCurrentDatabase();
	}

	public Database getCurrentDatabase() {
		return currentDatabase;
	}
	public void setCurrentDatabase(Database currentDatabase) {
		this.currentDatabase = currentDatabase;
	}
}
