package cn.citycraft.AuthMe.converter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map.Entry;

import org.bukkit.command.CommandSender;

import cn.citycraft.AuthMe.AuthMe;
import cn.citycraft.AuthMe.ConsoleLogger;
import cn.citycraft.AuthMe.cache.auth.PlayerAuth;
import cn.citycraft.AuthMe.datasource.DataSource;
import cn.citycraft.AuthMe.security.HashAlgorithm;
import cn.citycraft.AuthMe.security.PasswordSecurity;
import cn.citycraft.AuthMe.settings.Settings;

/**
 * @author Xephi59
 */
public class RakamakConverter implements Converter {

	public DataSource database;
	public AuthMe instance;
	public CommandSender sender;

	public RakamakConverter(AuthMe instance, CommandSender sender) {
		this.instance = instance;
		this.database = instance.database;
		this.sender = sender;
	}

	public RakamakConverter getInstance() {
		return this;
	}

	@Override
	public void run() {
		HashAlgorithm hash = Settings.getPasswordHash;
		boolean useIP = Settings.rakamakUseIp;
		String fileName = Settings.rakamakUsers;
		String ipFileName = Settings.rakamakUsersIp;
		File source = new File(Settings.APLUGIN_FOLDER, fileName);
		File ipfiles = new File(Settings.APLUGIN_FOLDER, ipFileName);
		HashMap<String, String> playerIP = new HashMap<>();
		HashMap<String, String> playerPSW = new HashMap<>();
		try {
			BufferedReader users;
			BufferedReader ipFile;
			ipFile = new BufferedReader(new FileReader(ipfiles));
			String line;
			if (useIP) {
				String tempLine;
				while ((tempLine = ipFile.readLine()) != null) {
					if (tempLine.contains("=")) {
						String[] args = tempLine.split("=");
						playerIP.put(args[0], args[1]);
					}
				}
			}
			ipFile.close();
			users = new BufferedReader(new FileReader(source));
			while ((line = users.readLine()) != null) {
				if (line.contains("=")) {
					String[] arguments = line.split("=");
					try {
						playerPSW.put(arguments[0], PasswordSecurity.getHash(hash, arguments[1], arguments[0]));
					} catch (NoSuchAlgorithmException e) {
						ConsoleLogger.showError(e.getMessage());
					}
				}
			}
			users.close();
			for (Entry<String, String> m : playerPSW.entrySet()) {
				String playerName = m.getKey();
				String psw = playerPSW.get(playerName);
				String ip;
				if (useIP) {
					ip = playerIP.get(playerName);
				} else {
					ip = "127.0.0.1";
				}
				PlayerAuth auth = new PlayerAuth(playerName, psw, ip, System.currentTimeMillis(), playerName);
				if (PasswordSecurity.userSalt.containsKey(playerName))
					auth.setSalt(PasswordSecurity.userSalt.get(playerName));
				database.saveAuth(auth);
			}
			ConsoleLogger.info("Rakamak database has been imported correctly");
			sender.sendMessage("Rakamak database has been imported correctly");
		} catch (IOException ex) {
			ConsoleLogger.showError(ex.getMessage());
			sender.sendMessage("Can't open the rakamak database file! Does it exist?");
		}
	}
}
