package de.tkprog.UniLoRPaM;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public abstract class AbstractMessage {
	
	public static final ArrayList<Class<? extends AbstractMessage>> MessageClasses = new ArrayList<Class<? extends AbstractMessage>>();

	/**
	 * This method is called by the file which is opened or created newly. Use it to create your tables in your database.
	 */
	public abstract void initialise(UniLoFile f) throws Exception;
	/**
	 * This method is used to create a new entry. The base-class-method doesn't need to use the given id.
	 */
	public abstract int newEntry(UniLoFile f, int id) throws Exception;
	
	protected static void setDouble(UniLoFile f, int id, String table, String field, double _double) throws SQLException {
		PreparedStatement ps = f.getSql().getConnection().prepareStatement("UPDATE `"+table+"` SET `"+field+"` = ? WHERE `id` = ?;");
		ps.setDouble(1, _double);
		ps.setInt(2, id);
		ps.executeUpdate();
	}

	protected static double getDouble(UniLoFile f, int id, String table, String field) throws SQLException {
		ResultSet rs = get(f,id,table,field);
		if(rs == null || !rs.next()){
			return -1;
		}
		return rs.getDouble(field);
	}

	protected static void setInt(UniLoFile f, int id, String table, String field, int _int) throws SQLException {
		PreparedStatement ps = f.getSql().getConnection().prepareStatement("UPDATE `"+table+"` SET `"+field+"` = ? WHERE `id` = ?;");
		ps.setInt(1, _int);
		ps.setInt(2, id);
		ps.executeUpdate();
	}

	protected static int getInt(UniLoFile f, int id, String table, String field) throws SQLException {
		ResultSet rs = get(f,id,table,field);
		if(rs == null || !rs.next()){
			return -1;
		}
		return rs.getInt(field);
	}

	protected static void setLong(UniLoFile f, int id, String table, String field, long date) throws SQLException {
		PreparedStatement ps = f.getSql().getConnection().prepareStatement("UPDATE `"+table+"` SET `"+field+"` = ? WHERE `id` = ?;");
		ps.setLong(1, date);
		ps.setInt(2, id);
		ps.executeUpdate();
	}

	protected static long getLong(UniLoFile f, int id, String table, String field) throws SQLException {
		ResultSet rs = get(f,id,table,field);
		if(rs == null || !rs.next()){
			return -1;
		}
		return rs.getLong(field);
	}

	protected static void setString(UniLoFile f, int id, String table, String field, String precode) throws SQLException {
		PreparedStatement ps = f.getSql().getConnection().prepareStatement("UPDATE `"+table+"` SET `"+field+"` = ? WHERE `id` = ?;");
		ps.setString(1, precode);
		ps.setInt(2, id);
		ps.executeUpdate();
	}
	
	protected static String getString(UniLoFile f, int id, String table, String field) throws SQLException{
		ResultSet rs = get(f,id,table,field);
		if(rs == null || !rs.next()){
			return null;
		}
		return rs.getString(field);
	}
	
	protected static ResultSet get(UniLoFile f, int id, String table, String field) throws SQLException{
		PreparedStatement ps = f.getSql().getConnection().prepareStatement("SELECT `"+field+"` FROM `"+table+"` WHERE `id` = ?;");
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		return rs;
	}
}
