package szakdolgozat.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import szakdolgozat.shared.User;

public class DB {

	private static ArrayList<User> loggedInUsers = new ArrayList<User>();

	public void populateDatabase() throws Exception {
		Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
		Connection con = DriverManager
				.getConnection("jdbc:derby:users;create=true");
		con.setAutoCommit(false);
		Statement s = con.createStatement();

		s.execute("DROP TABLE userstats");
		s.execute("DROP TABLE users");

		s
				.execute("CREATE TABLE users("
						+ "id int generated always as identity constraint users_pk primary key, "
						+ "username varchar(100) not null constraint users_uq unique, "
						+ "password varchar(32))");

		s
				.execute("CREATE TABLE userstats("
						+ "playerid int generated always as identity constraint id_fk references users constraint stats_pk primary key, "
						+ "logincount int default 0, "
						+ "gamesplayed int default 0, "
						+ "gameswon int default 0," + "logintime date)");
		//        
		// All the other create table statements from above would go here...
		//

		/*Statement insertUserStatement = con.createStatement();
		Statement insertStatsStatement = con.createStatement();
		insertUserStatement
				.execute("INSERT into users(id,username,password) values (DEFAULT,\'Anna\',\'a\')");
		insertStatsStatement
				.execute("INSERT into userStats(playerID,loginCount,gamesPlayed,gamesWon) values (DEFAULT,10,10,7)");
		insertUserStatement
				.execute("INSERT into users(id,username,password) values (DEFAULT,\'Béla\',\'a\')");
		insertStatsStatement
				.execute("INSERT into userStats(playerID,loginCount,gamesPlayed,gamesWon) values (DEFAULT,34,50,40)");
		insertUserStatement
				.execute("INSERT into users(id,username,password) values (DEFAULT,\'Csaba\',\'a\')");
		insertStatsStatement
				.execute("INSERT into userStats(playerID,loginCount,gamesPlayed,gamesWon) values (DEFAULT,1,3,0)");
		insertUserStatement
				.execute("INSERT into users(id,username,password) values (DEFAULT,\'Dénes\',\'a\')");
		insertStatsStatement
				.execute("INSERT into userStats(playerID,loginCount,gamesPlayed,gamesWon) values (DEFAULT,66,70,34)");
		insertUserStatement
				.execute("INSERT into users(id,username,password) values (DEFAULT,\'Eszter\',\'a\')");
		insertStatsStatement
				.execute("INSERT into userStats(playerID,loginCount,gamesPlayed,gamesWon) values (DEFAULT,22,22,17)");
		*/con.commit();
		con.close();
		try {
			DriverManager.getConnection("jdbc:derby:;shutdown=true");
		} catch (SQLException ignore) {
		}
	}

	public static ArrayList<User> getLoggedInUsers() {
		return loggedInUsers;
	}

	public static void setLoggedInUsers(ArrayList<User> loggedInUsers) {
		DB.loggedInUsers = loggedInUsers;
	}

	public static void registerUserLogin(User user) {
		DB.loggedInUsers.add(user);
	}

	public static void registerUserLogout(User user) {
		DB.loggedInUsers.remove(user);
	}

	public static void main(String[] args) throws Exception {
		(new DB()).populateDatabase();
	}
}
