package eu.dnetlib.authoring;

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

import org.apache.log4j.BasicConfigurator;
import org.junit.Before;
import org.junit.Test;

import eu.dnetlib.efg1914.authoring.components.Archive;
import eu.dnetlib.efg1914.authoring.components.Frame;
import eu.dnetlib.efg1914.authoring.components.Item;
import eu.dnetlib.efg1914.authoring.components.MD5;
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.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.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 UsersTest {

	private static SednaStore store;

	static	private ItemDAO itemDao;
	static	private ArchiveDAO archiveDao;

	static	private ThemeDAO themeDao;
	static	private TopicDAO topicDao;

	static private FrameDAO frameDao;

	static	private UserDAO userDao;

//	 private static String dbHost = "dl115.madgik.di.uoa.gr";
	private static String dbHost = "duffy.di.uoa.gr";
	
//	private static String dbHost = "82.98.75.58"; //Barry
	
	private static String dbUser = "SYSTEM";
	private static String dbPassword = "eridanus";
	 private static String dbName = "test_db";
//	 private static String dbName = "small_db";

//	private static String dbName = "test_users";
 

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

	 

	@Before
	public void init() throws  XMLStoreException, DAOException
		{
		 
	 
 		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,true);
		((ItemXmlDAO) itemDao).setStore(store);

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

		/* TOPIC */

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

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

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

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

		/* USER */

		userDao = new UserXmlDAO(User.class,true);
		((UserXmlDAO) userDao).setStore(store);

	}
	@Test
  	public void  getUsers() throws DAOException, XMLStoreException, IOException {
		ArrayList<User> a = this.userDao.getAll();
		for (User u : a) {
			log.info("Name: " + u.getUsername()+" "+u.getId()+" pass: "+u.getPassword()+"  md5 "+MD5.encrypt("pass"));

		}
		
	}
	@Test
  	public void  updateUsersWithEncryptedPassword() throws DAOException, XMLStoreException, IOException {
		ArrayList<User> a = this.userDao.getAll();
		for (User u : a) {
			log.info("Name: " + u.toString()+MD5.encrypt(u.getPassword()));
			log.info("pass " + u.getPassword()+"  new password "+MD5.encrypt(u.getPassword()));
			//TODO uncomment  to encrypt saved passwords 
			//!! only once
// 			this.userDao.update_password(u.getId(), MD5.encrypt(u.getPassword()) );
 			u.setPassword(MD5.encrypt(u.getPassword()) );
 			this.userDao.update(u);

 
		}
	}
	@Test
  	public void  testUsers() throws DAOException, XMLStoreException, IOException {
		
 		 
 		ArrayList<User> a = this.userDao.getAll();
		for (User u : a) {
			log.info("Name::"+u.getUsername()+" id:"+u.getId() +"   "+a.size());
		 u.toString();
	 
		}
 
	}
	 
 
}
