package org.kar.karusic.migration;

import java.util.List;

import org.kar.archidata.dataAccess.DBAccess;
import org.kar.archidata.migration.MigrationSqlStep;
import org.kar.archidata.model.Data;
import org.kar.archidata.model.User;
import org.kar.karusic.model.Album;
import org.kar.karusic.model.Artist;
import org.kar.karusic.model.Gender;
import org.kar.karusic.model.Playlist;
import org.kar.karusic.model.Track;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Initialization extends MigrationSqlStep {
	private static final Logger LOGGER = LoggerFactory.getLogger(Initialization.class);

	public static final int KARSO_INITIALISATION_ID = 1;
	public static final List<Class<?>> CLASSES_BASE = List.of(Album.class, Artist.class, Data.class, Gender.class, Playlist.class, Track.class, User.class);

	@Override
	public String getName() {
		return "Initialization";
	}

	public Initialization() {

	}

	@Override
	public void generateStep() throws Exception {
		for (final Class<?> elem : CLASSES_BASE) {
			addClass(elem);
		}

		addAction((final DBAccess da) -> {
			final List<Gender> data = List.of(//
					new Gender(1L, "Variété française"), //
					new Gender(2L, "Pop"), //
					new Gender(3L, "inconnue"), //
					new Gender(4L, "Disco"), //
					new Gender(5L, "Enfants"), //
					new Gender(6L, "Portugaise"), //
					new Gender(7L, "Apprentissage"), //
					new Gender(8L, "Blues"), //
					new Gender(9L, "Jazz"), //
					new Gender(10L, "Chanson Noël"), //
					new Gender(11L, "DubStep"), //
					new Gender(12L, "Rap français"), //
					new Gender(13L, "Classique"), //
					new Gender(14L, "Rock"), //
					new Gender(15L, "Electro"), //
					new Gender(16L, "Celtique"), //
					new Gender(17L, "Country"), //
					new Gender(18L, "Variété Québéquoise"), //
					new Gender(19L, "Médiéval"), //
					new Gender(20L, "Variété Italienne"), //
					new Gender(21L, "Comédie Musicale"), //
					new Gender(22L, "Vianney"), //
					new Gender(23L, "Bande Original"), //
					new Gender(24L, "Bande Originale"), //
					new Gender(25L, "Variété Belge"), //
					new Gender(26L, "Gospel"));
		});
		// set start increment element to permit to add after default elements
		addAction("""
				ALTER TABLE `album` AUTO_INCREMENT = 1000;
				""", "mysql");
		addAction("""
				ALTER TABLE `artist` AUTO_INCREMENT = 1000;
				""", "mysql");
		addAction("""
				ALTER TABLE `gender` AUTO_INCREMENT = 1000;
				""", "mysql");
		addAction("""
				ALTER TABLE `playlist` AUTO_INCREMENT = 1000;
				""", "mysql");
		addAction("""
				ALTER TABLE `track` AUTO_INCREMENT = 1000;
				""", "mysql");
		addAction("""
				ALTER TABLE `user` AUTO_INCREMENT = 1000;
				""", "mysql");
	}

	public static void dropAll(final DBAccess da) {
		for (final Class<?> element : CLASSES_BASE) {
			try {
				da.drop(element);
			} catch (final Exception ex) {
				LOGGER.error("Fail to drop table !!!!!!");
				ex.printStackTrace();
			}
		}
	}

	public static void cleanAll(final DBAccess da) {
		for (final Class<?> element : CLASSES_BASE) {
			try {
				da.cleanAll(element);
			} catch (final Exception ex) {
				LOGGER.error("Fail to clean table !!!!!!");
				ex.printStackTrace();
			}
		}
	}
}
