package eu.dnetlib.authoring;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.BasicConfigurator;
import org.junit.Before;
import org.junit.Test;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import eu.dnetlib.efg1914.authoring.components.Archive;
import eu.dnetlib.efg1914.authoring.components.Configuration;
import eu.dnetlib.efg1914.authoring.components.Frame;
import eu.dnetlib.efg1914.authoring.components.Item;
import eu.dnetlib.efg1914.authoring.components.Theme;
import eu.dnetlib.efg1914.authoring.components.Topic;
import eu.dnetlib.efg1914.authoring.components.dao.ArchiveDAO;
import eu.dnetlib.efg1914.authoring.components.dao.ArchiveXmlDAO;
import eu.dnetlib.efg1914.authoring.components.dao.ConfigurationDAO;
import eu.dnetlib.efg1914.authoring.components.dao.ConfigurationXmlDAO;
import eu.dnetlib.efg1914.authoring.components.dao.FrameDAO;
import eu.dnetlib.efg1914.authoring.components.dao.FrameXmlDAO;
import eu.dnetlib.efg1914.authoring.components.dao.ItemDAO;
import eu.dnetlib.efg1914.authoring.components.dao.ItemXmlDAO;
import eu.dnetlib.efg1914.authoring.components.dao.ThemeDAO;
import eu.dnetlib.efg1914.authoring.components.dao.ThemeXmlDAO;
import eu.dnetlib.efg1914.authoring.components.dao.TopicDAO;
import eu.dnetlib.efg1914.authoring.components.dao.TopicXmlDAO;
import eu.dnetlib.efg1914.authoring.users.User;
import eu.dnetlib.efg1914.authoring.users.dao.IniFileReader;
import eu.dnetlib.efg1914.authoring.users.dao.UserDAO;
import eu.dnetlib.efg1914.authoring.users.dao.UserXmlDAO;
import eu.dnetlib.efg1914.commons.dao.DAOException;
import eu.dnetlib.efg1914.commons.store.SednaStore;
import eu.dnetlib.efg1914.commons.store.XMLStoreException;

public class Setup {

	SednaStore store;

	private ItemDAO itemDao;
	private ArchiveDAO archiveDao;

	private ThemeDAO themeDao;

	private TopicDAO topicDao;

	private FrameDAO frameDao;

	private UserDAO userDao;
	private ConfigurationDAO configurationDao;

	private String ARCHIVES_INI="/home/argiro/EFG1914/uoa-efg1914/efg1914-authoring/src/main/eu/dnetlib/efg1914/authoring/archives.ini";
	// private static String dbHost = "dl115.madgik.di.uoa.gr";

	private static String dbHost = "duffy.di.uoa.gr";
	private static String dbUser = "SYSTEM";
	private static String dbPassword = "MANAGER";
	// private static String dbName = "test_db";

//	private static String dbName = "test_users";

	private static String dbName = "small_db";
	private static String superUserpass = "superPass";
	private static String superUsername = "superUsername";

	private transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(Setup.class);

	public Setup() {
	};

//	@Before
	public void init() throws Exception {
		BasicConfigurator.configure();
		// sedna store
		store = new SednaStore();
		store.setDbHost(dbHost);
		store.setDbPassword(dbPassword);
		store.setDbUser(dbUser);
		store.setDbName(dbName);
		store.init();

		/* ITEM */
		itemDao = new ItemXmlDAO(Item.class);
		((ItemXmlDAO) itemDao).setStore(store);

		/* FRAME */
		frameDao = new FrameXmlDAO(Frame.class);
		((FrameXmlDAO) frameDao).setStore(store);

		/* TOPIC */

		topicDao = new TopicXmlDAO(Topic.class);
		((TopicXmlDAO) topicDao).setStore(store);

		((TopicXmlDAO) topicDao).setFrameDao(frameDao);

		/* THEME */
		themeDao = new ThemeXmlDAO(Theme.class);
		((ThemeXmlDAO) themeDao).setStore(store);

		/* archive */
		archiveDao = new ArchiveXmlDAO(Archive.class);
		((ArchiveXmlDAO) archiveDao).setStore(store);

		/* USER */

		userDao = new UserXmlDAO(User.class);
		((UserXmlDAO) userDao).setStore(store);
		
		/* Configuration */

		configurationDao = new ConfigurationXmlDAO(Configuration.class);
		((ConfigurationXmlDAO) configurationDao).setStore(store);


	}

//	@Test
	public void Setup() throws DAOException, XMLStoreException, IOException {
		createCollections();

	}

	public void createCollections() throws DAOException, XMLStoreException, IOException {

		if (createNewCollection("archive") || isEmpty("archive")) {
			createArchives();
		}
		 if (createNewCollection("user") || isEmpty("user")) {
		 createUsers();
		 }
		 createNewCollection("item");
		 createNewCollection("frame");
		 createNewCollection("topic");
		 createNewCollection("theme");
		 
		 if (createNewCollection("configuration")) {
			 createConfiguration();
		}

	}

	private boolean createNewCollection(String collectionName) throws XMLStoreException {

		try {
			store.createCollection(collectionName);
			return true;
		} catch (XMLStoreException e) {
			if (e.getCause().toString().contains("ERROR SE2002")) {
				return false;
			} else {
				throw new XMLStoreException(e);
			}
		}
	}

	private boolean isEmpty(String collectionName) throws DAOException {
		if (collectionName.equalsIgnoreCase("item")) {
			ArrayList<Item> i = itemDao.getAll();
			if (i == null || i.isEmpty()) {
				return true;
			}
		} else if (collectionName.equalsIgnoreCase("frame")) {
			ArrayList<Frame> i = frameDao.getAll();
			if (i == null || i.isEmpty()) {
				return true;
			}
		} else if (collectionName.equalsIgnoreCase("topic")) {
			ArrayList<Topic> i = topicDao.getAll();
			if (i == null || i.isEmpty()) {
				return true;
			}
		} else if (collectionName.equalsIgnoreCase("theme")) {
			ArrayList<Theme> i = themeDao.getAll();
			if (i == null || i.isEmpty()) {
				return true;
			}
		} else if (collectionName.equalsIgnoreCase("user")) {
			ArrayList<User> i = userDao.getAll();
			if (i == null || i.isEmpty()) {
				return true;
			}
		} else if (collectionName.equalsIgnoreCase("archive")) {
			ArrayList<Archive> i = archiveDao.getAll();
			if (i == null || i.isEmpty()) {
				return true;
			}
		}
		return false;

	}

	// @Test
	private void loadArchives() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, DAOException {
		IniFileReader archivesFile = new IniFileReader();
		archivesFile.load(ARCHIVES_INI, false);

		List<Object> a = archivesFile.generateObjectsFromIni(Archive.class.getName());
		for (Object object : a) {
			log.debug(((Archive) object).toString());
			archiveDao.save((Archive) object);
		}

	}

	// @Test
	private void createArchives() throws DAOException {

		Archive a = new Archive();
		a.setName("Deutsches Filminstitut – DIF e.V.");
		a.setUrl("http://deutsches-filminstitut.de/");
		a.setCity("v");
		archiveDao.save(a);

		a = new Archive();
		a.setName("Arhiva Nationala de Filme");
		a.setUrl("http://www.anf-cinemateca.ro/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Association des Cinémathèques Européennes");
		a.setUrl("http://www.ace-film.eu/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Athena Research and  Innovation Center in Information Communication & Knowledge Technologies");
		a.setUrl("http://www.athena-innovation.gr/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Centre national du cinéma et de l’image animée – Archives françaises du Film");
		a.setUrl("http://www.cnc.fr/web/fr;jsessionid=C35626E5BA06EFCE7D14BBF5A493E47C.liferay");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Cinecittá Luce S.p.A");
		a.setUrl("http://www.cinecitta.com/");

		archiveDao.save(a);

		a = new Archive();
		a.setName("Cinémathèque Royale de Belgique");
		a.setUrl("http://www.cinematek.be/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("CNR-Istituto di Scienza e Tecnologie dell´Informazione");
		a.setUrl("http://www.isti.cnr.it/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("CulturArts Generalita – Unidad de Audiovisual y Cinematografía");
		a.setUrl("http://ivac.gva.es/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Det Danske Filminstitut");
		a.setUrl("http://www.dfi.dk/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Deutsche Kinemathek – Museum für Film und Fernsehen");
		a.setUrl("http://www.deutsche-kinemathek.de/de");

		archiveDao.save(a);

		a = new Archive();
		a.setName("Estonian Film Archive");
		a.setUrl("http://www.filmi.arhiiv.ee/");

		archiveDao.save(a);

		a = new Archive();
		a.setName("EYE Stichting Film Instituut Nederland");
		a.setUrl("http://www.eyefilm.nl/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Filmarchiv Austria");
		a.setUrl("http://filmarchiv.at/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Fondazione Cineteca Italiana");
		a.setUrl("http://www.cinetecamilano.it/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Fondazione Cineteca di Bologna");
		a.setUrl("http://www.cinetecadibologna.it/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Fraunhofer Institute for Integrated Circuits IIS");
		a.setUrl("www.iis.fraunhofer.de");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Imperial War Museums");
		a.setUrl("http://www.iwm.org.uk/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Instituto de la Cinematografia y Artes Audiovisuales – Filmoteca Española");
		a.setUrl("http://www.mcu.es/cine/MC/FE/index.html");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Jugoslovenska Kinoteka");
		a.setUrl("http://www.kinoteka.org.rs/index.html");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Hungarian National Digital Archive MANDA");
		a.setUrl("http://mandarchiv.hu/");

		archiveDao.save(a);

		archiveDao.save(a);
		a = new Archive();
		a.setName("La Cineteca del Friuli");
		a.setUrl("http://www.cinetecadelfriuli.org/");

		archiveDao.save(a);

		archiveDao.save(a);
		a = new Archive();
		a.setName("Národní filmový archiv");
		a.setUrl("http://www.nfa.cz/");

		archiveDao.save(a);

		a = new Archive();
		a.setName("Nasjonalbiblioteket");
		a.setUrl("http://www.nb.no/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Österreichisches Filmmuseum");
		a.setUrl("http://www.filmmuseum.at/");

		archiveDao.save(a);
		a = new Archive();
		a.setName("Reelport GmbH ");
		a.setUrl("http://www.reelport.com/");

		archiveDao.save(a);

	}

	@Test
	private void getArchives() throws DAOException {
		ArrayList<Archive> Archives = this.archiveDao.getAll();
		for (Archive archive : Archives) {
			log.info("Name: " + archive.getName() + " URL: " + archive.getUrl() + " id  " + archive.getId());

		}
	}

	private void dropArchives() throws DAOException {
		ArrayList<Archive> Archives = this.archiveDao.getAll();
		for (Archive archive : Archives) {
			log.info("Name: " + archive.getName() + " URL: " + archive.getUrl() + " id  " + archive.getId());
			if ((!archive.getId().equals("8c296236-4390-47a0-b190-030e848ece2a"))
					&& (!archive.getId().equals("e6ea3747-8585-41d9-95dc-563426d65f34"))) {
				archiveDao.delete(archive.getId());
			}
		}
	}
//	 @Test
		private void createConfiguration() throws DAOException, XMLStoreException  {
//		 createNewCollection("configuration");
//			 Configuration configuration= new Configuration();
//			 configuration.setName("EFG1914");
//			configurationDao.save(configuration);
			System.out.println("aaaaaaaaaaaaa");
		}


	// @Test
	private void createUsers() throws DAOException, XMLStoreException {

		User user = new User();
		user.setUsername(superUsername);
		user.setPassword(superUserpass);
		user.setRole("Administrator");
		userDao.save(user);
	}

//	 @Test
	public void getUsers() throws DAOException, XMLStoreException {

		ArrayList<User> a = this.userDao.getAll();
		for (User u : a) {
			log.info("Name: " + u.toString());
		}
	}

//	@After
	public void cleanUp() {
		log.debug("cleaning up...");

	}
}
