package hu.nir.nmail.migration;

import hu.nir.nmail.migration.gui.swing.MigrationWindow;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author attila
 */
public class NmailMigrationTool {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length > 0) {
            String proces = args[0];
            if (proces.equals("swing")) {
                swing();
            } else {
                try {
                    console();
                } catch (IOException ex) {
                    Logger.getLogger(NmailMigrationTool.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        else {
            try {
                console();
            } catch (IOException ex) {
                Logger.getLogger(NmailMigrationTool.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    private static void swing() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MigrationWindow window = new MigrationWindow();
                window.pack();
                window.setVisible(true);
            }
        });
    }
    
    private static void console() throws IOException {
        String oldHost = null;
        String oldPortS = null;
        String oldDBname = null;
        String oldAccount = null;
        String oldPw = null;
        String newHost = null;
        String newPortS = null;
        String newDBname = null;
        String newAccount = null;
        String newPw = null;
        DatabaseManager oldDM = null;
        DatabaseManager newDM = null;
        
        while (true) {
            System.out.println("Adja meg az nmail1-hez tartozó adatbázis adatait:");
            System.out.print("Host (localhost): ");
            oldHost = read();
            System.out.print("Port (5432): ");
            oldPortS = read();
            int oldPort = 0;
            try {
                oldPort = Integer.valueOf(oldPortS);
            } catch (NumberFormatException ex) {
                System.out.println("A port csak számokat tartalmazhat!");
                continue;
            }
            System.out.print("Database name (mailds): ");
            oldDBname = read();
            System.out.print("Account: ");
            oldAccount = read();
            System.out.print("Password: ");
            oldPw = read();
            oldDM = new DatabaseManager(oldHost, oldPort, oldDBname, oldAccount, oldPw);
            if(oldDM.testConnection()) {
                break;
            } else {
                System.out.println("A megadott adatokkal nem lehetett kacsolódni az adatbázishoz!");
            }
        }
        
        while (true) {
            System.out.println("Adja meg az nmail2-höz tartozó adatbázis adatait:");
            System.out.print("Host (localhost): ");
            newHost = read();
            System.out.print("Port (5432): ");
            newPortS = read();
            int newPort = 0;
            try {
                newPort = Integer.valueOf(newPortS);
            } catch (NumberFormatException ex) {
                System.out.println("A port csak számokat tartalmazhat!");
                continue;
            }
            System.out.print("Database name (nmail): ");
            newDBname = read();
            System.out.print("Account: ");
            newAccount = read();
            System.out.print("Password: ");
            newPw = read();
            newDM = new DatabaseManager(newHost, newPort, newDBname, newAccount, newPw);
            if(newDM.testConnection()) {
                break;
            } else {
                System.out.println("A megadott adatokkal nem lehetett kacsolódni az adatbázishoz!");
            }
        }
        
        System.out.print("Adatok migrálása (i/n): ");
        String migr = read();
        if (migr != null && !migr.trim().equals("")) {
            migr = migr.toLowerCase();
            if (migr.equals("i") || migr.equals("igen")) {
                MigrationManager mm = new MigrationManager(oldDM, newDM);
                mm.migrate(null, null);
            }
        }
    }
    
    private static String read() throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        return br.readLine();
    }

}
