package microfolie.db;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import db.DatabaseManager;

public class MicrofolieDatabase extends DatabaseManager {
	
	private static MicrofolieDatabase instance;
	private final static Map<String, String> create = new LinkedHashMap<>();
	private final static Map<String, String> drop = new LinkedHashMap<>();
	private final static List<String> mock = new ArrayList<>();
	static {
		iniCreate();
		iniDrop();
		iniMock();
	}

	private static void iniCreate() {
		create.put("Espace", "CREATE TABLE Espace ("
				+ "ID BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,"
				+ "LIBELLE VARCHAR(250) NOT NULL)");
		create.put("Ville", "CREATE TABLE Ville ("
				+ "ID BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,"
				+ "LIBELLE VARCHAR(250) NOT NULL,"
				+ "CODEPOSTAL VARCHAR(5))");
		create.put("Niveau", "CREATE TABLE Niveau ("
				+ "ID BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,"
				+ "LIBELLE VARCHAR(250) NOT NULL)");
		create.put("Ecole", "CREATE TABLE Ecole ("
				+ "ID BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,"
				+ "LIBELLE VARCHAR(250) NOT NULL,"
				+ "VILLE BIGINT,"
				+ "NIVEAU BIGINT,"
				+ "CONSTRAINT fk_ecole_ville FOREIGN KEY (VILLE) REFERENCES Ville(ID),"
				+ "CONSTRAINT fk_ecole_niveau FOREIGN KEY (NIVEAU) REFERENCES Niveau(ID))");
		create.put("Usager", "CREATE TABLE Usager ("
				+ "ID BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,"
				+ "NOM VARCHAR(250) NOT NULL,"
				+ "PRENOM VARCHAR(250) NOT NULL,"
				+ "DATEDENAISSANCE DATE NOT NULL,"
				+ "EMAIL VARCHAR(250) NOT NULL,"
				+ "TELEPHONE VARCHAR(12) NOT NULL,"
				+ "CONTACTURGENCE VARCHAR(250) NOT NULL,"
				+ "CODE VARCHAR(4) NOT NULL,"
				+ "SCOLAIRE BOOLEAN NOT NULL,"
				+ "DESCRIPTION VARCHAR(250) NOT NULL,"
				+ "VILLE BIGINT,"
				+ "ECOLE BIGINT,"
				+ "CONSTRAINT fk_usager_ville FOREIGN KEY (VILLE) REFERENCES Ville(ID),"
				+ "CONSTRAINT fk_usager_ecole FOREIGN KEY (ECOLE) REFERENCES Ecole(ID))");
		create.put("Frequentation", "CREATE TABLE Frequentation ("
				+ "ID BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,"
				+ "ESPACE BIGINT,"
				+ "USAGER BIGINT,"
				+ "DATE DATE NOT NULL,"
				+ "CONSTRAINT fk_frequentation_espace FOREIGN KEY (ESPACE) REFERENCES Espace(ID),"
				+ "CONSTRAINT fk_frequentation_usager FOREIGN KEY (USAGER) REFERENCES Usager(ID))");
	}
	
	private static void iniDrop() {
		drop.put("Frequentation", "DROP TABLE Frequentation");
		drop.put("Usager", "DROP TABLE Usager");
		drop.put("Ecole", "DROP TABLE Ecole");
		drop.put("Niveau", "DROP TABLE Niveau");
		drop.put("Ville", "DROP TABLE Ville");
		drop.put("Espace", "DROP TABLE Espace");
	}
	
	private static void iniMock() {
		mock.add("Insert into Espace(LIBELLE) Values('Jeux')");
		mock.add("Insert into Espace(LIBELLE) Values('Video')");
		mock.add("Insert into Ville(LIBELLE, CODEPOSTAL) Values('Troyes', '10000')");
		mock.add("Insert into Ville(LIBELLE, CODEPOSTAL) Values('Reims', '51000')");
		mock.add("Insert into Ville(LIBELLE, CODEPOSTAL) Values('Lens', '62300')");
		mock.add("Insert into Niveau(LIBELLE) Values('A')");
		mock.add("Insert into Niveau(LIBELLE) Values('B')");
		mock.add("Insert into Ecole(LIBELLE, VILLE, NIVEAU) Values('Camille claudel', 1, 2)");
		mock.add("Insert into Ecole(LIBELLE, VILLE, NIVEAU) Values('UTT', 1, 1)");
		mock.add("Insert into Ecole(LIBELLE, VILLE, NIVEAU) Values('Moulin de la housse', 2, 2)");
		mock.add("Insert into Ecole(LIBELLE, VILLE, NIVEAU) Values('Jean Perrin', 3, 1)");
		mock.add("Insert into Ecole(LIBELLE, NIVEAU) Values('Nowhere', 1)");
		mock.add("Insert into Usager(NOM, PRENOM, DATEDENAISSANCE, EMAIL, TELEPHONE, CONTACTURGENCE, CODE, SCOLAIRE, DESCRIPTION, VILLE, ECOLE) Values('Brandao', 'Arthur', '1996-10-21', 'ab@email.fr', '0605040302', 'Nop', 'LCOM', TRUE, 'Desc', 1, 4)");
	}

	@Override
	public Map<String, String> create() {
		return create;
	}

	@Override
	public Map<String, String> drop() {
		return drop;
	}

	@Override
	public List<String> content() {
		return mock;
	}
	
	public static MicrofolieDatabase getInstance() {
		if (instance == null) {
			instance = new MicrofolieDatabase();
		}
		return instance;
	}
	
}
