import eu.dnetlib.domain.enabling.SecurityProfile;
import eu.dnetlib.domain.functionality.UserProfile;
import eu.dnetlib.utils.md5.MD5;
import gr.uoa.di.driver.xml.SecurityProfileXmlConverter;
import gr.uoa.di.driver.xml.UserProfileXmlConverter;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBException;

public class CleanupSecurityProfiles {
	final static File rootDir = new File("/home/antleb/projects/dnet1.1/uoa-is/cnr-test-profiles/src/main/eu/dnetlib/test/profiles/");
	final static File userDir = new File(rootDir, "UserDSResources/UserDSResourceType");
	final static File secProfDir = new File(rootDir, "SecurityProfileDSResources/SecurityProfileDSResourceType");
	
	public static void cleanupUserProfiles()
			throws JAXBException, IOException {
		UserProfileXmlConverter conv = new UserProfileXmlConverter();
		File[] xmlsFiles = userDir.listFiles(new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(".xml");
			}
		});
		List<String> users = new ArrayList<String>();

		for (File xmlFile : xmlsFiles) {
			String xml = readXml(xmlFile);
			UserProfile user = conv.XmlToObject(xml);
			
			if (users.contains(user.getEmail())) {
				System.out.println("user with email '" + user.getEmail() + "' already exists. Removing profile");
				xmlFile.delete();
			} else {
				users.add(user.getEmail());
			}
		}
	}

	public static void cleanupSecProfiles() throws JAXBException,
			NoSuchAlgorithmException, IOException {
		SecurityProfileXmlConverter conv = new SecurityProfileXmlConverter();
		File[] xmlsFiles = secProfDir.listFiles(new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(".xml");
			}
		});

		for (File xmlFile : xmlsFiles) {
			String xml = readXml(xmlFile);
			SecurityProfile secProfile = conv.XmlToObject(xml);
			
			if (secProfile.getDriverResourceId() == null
					|| secProfile.getDriverResourceId().trim().equals("")) {
				System.out.println("Security profile is orphan: " + secProfile.getResourceId());
				xmlFile.delete();
			} else if (!new File(userDir, secProfile.getDriverResourceId().split("_")[0] + ".xml").exists()) {
				System.out.println("Security profile points to non existing user: " + secProfile.getResourceId());
				xmlFile.delete();
								
			} else {
				System.out.println("---");
				if (secProfile.getPassword() != null) {
					String oldPass = secProfile.getPassword();
					String newPass = MD5.encrypt2Hex(oldPass);

					System.out.println(oldPass + " -> " + newPass);

					secProfile.setPassword(newPass);

					xml = conv.ObjectToXml(secProfile);

					saveXml(xml, xmlFile);
				}
			} 
		}
	}

	private static void saveXml(String xml, File xmlFile) throws IOException {
		FileWriter fw = new FileWriter(xmlFile);

		fw.append(xml);

		fw.close();
	}

	private static String readXml(File xmlFile) throws IOException {
		FileReader fr = new FileReader(xmlFile);
		StringBuilder sb = new StringBuilder();
		char buffer[] = new char[1024];
		while (fr.read(buffer) != -1) {
			sb.append(buffer);

			buffer = new char[1024];
		}

		fr.close();

		return sb.toString().trim();
	}

	public static void main(String[] args) throws Exception {
		cleanupUserProfiles();
		cleanupSecProfiles();
	}
}
