package rdms;


import java.util.Date;
import java.util.GregorianCalendar;

//import com.jcraft.jsch.jce.Random;

// A very simple program to test the database
public class TestProgram {

	
	public static void main(String [] args) {
		int option = 7;
		// 6 for mysql
		// 7 for griddb
		// 12 for summary
		int option1 = 1;
		int runOption =2;
		//server.connectToServerTestMySQL();
		System.out.println("------");
		System.out.println();;
		
		Ingest ingestSQL = new MySQLIngest();
		Ingest ingestGDB = new GridDBIngest();
		Extract extractSQL = new MySQLExtract();
		Extract extractGDB = new GridDBExtract();

		//ServerConnection testConn = new ServerConnection(); // for testing
		
		Meter meterTest = new Meter();
		MeterRead meterReadTest = new MeterRead();
		Date testDate = new Date();
		if(option1 == 1) {
			meterTest.address = "someAddress";
			meterTest.city = "someCity";
			meterTest.contact_name = "John Smith";
			meterTest.description = "someDescription";
			meterTest.email = "something@some.com";
			meterTest.id = 15;
			meterTest.latitude = (float) 1.03;
			meterTest.longitude = (float) 2.52;
			meterTest.phone_number = "1 (949) 613 - 5829";
			meterTest.state_province = "CA";
			meterTest.last_reading = testDate;
			meterReadTest.all_time_usage = 0;
			meterReadTest.usage_since_read = 0;
			meterReadTest.usage_this_hour = 0;
			meterReadTest.usage_this_day = 0;
			meterReadTest.usage_this_month = 0;
			meterReadTest.usage_this_week = 0;
			meterReadTest.usage_this_year = 0;
			meterReadTest.error_code_1 = 12030;
			meterReadTest.error_code_2 = 215214;
			meterReadTest.timestamp = testDate;
			GregorianCalendar cal = new GregorianCalendar();
			cal.set(2018, 6, 17, 00, 20, 25);
			meterTest.last_reading = cal.getTime();
			//
			//extractSQL.connectToServer();
			//extractSQL.printData();
			//extractSQL.closeServer();
		}
		/*
		if((option == 1 || option == 3) && testConn.canConnectMySQL()) {
			System.out.println("MySQL");
			//
			ingestSQL.connectToServer();
			//
			ingestSQL.setMeter(meterTest);
			//
			if(runOption == 1)
				ingestSQL.runOnce();
			else if (runOption == 2) {
				ingestSQL.run();
			}
			ingestSQL.closeServer();
			extractSQL.connectToServer();
			extractSQL.printData();
			extractSQL.closeServer();
		} else if(option == 1 || option == 3){
			System.out.println("Could not connect to server.");
		}
		*/
		if((option == 2 || option == 3)) {
			System.out.println("GridDB");
			//
			ingestGDB.connectToServer();
			//
			ingestGDB.setMeter(meterTest);
			ingestGDB.setMeterRead(meterReadTest);
			//
			if(runOption == 1)
				ingestGDB.runOnce();
			else if (runOption == 2) {
				ingestGDB.run();
			}
			ingestGDB.closeServer();
			//
			extractGDB.connectToServer();
			extractGDB.printData();
			extractGDB.closeServer();
		} else if (option == 4) {
			ThreadManager thr = new ThreadManager();
			thr.setMeter(meterTest);
			thr.setMeterRead(meterReadTest);
			thr.setGridOrMySQL(1);
			thr.setSecondsWait(1);
			thr.setStopNum(1);
			//
			java.util.Random random = new java.util.Random();
			//
			long startNum = random.nextInt(100) + 1;
			long interv = random.nextInt(10) + 1;
			int numThreads = 10;
			//
			thr.genereateIDs(startNum, interv, numThreads);
			thr.startThreads(numThreads);
			//thr.getRunThread().closeServerI();
		} else if (option == 5) {
			/*
			ThreadManager thr = new ThreadManager();
			thr.setMeter(meterTest);
			thr.setMeterRead(meterReadTest);
			thr.setGridOrMySQL(1); // 1 for mysql, 2 for griddb
			thr.setSecondsWait(1);
			thr.setStopNum(1);
			//
			java.util.Random random = new java.util.Random();
			//
			long startNum = random.nextInt(100) + 1;
			long interv = random.nextInt(10) + 1;
			startNum = 1;
			interv = 1;
			int numThreads = 10;
			//
			thr.genereateIDs(startNum, interv, numThreads);
			thr.startThreads(numThreads);
			*/
			//
			//extractSQL.run2();
			
			//
			/*
			extractSQL.connectToServer();
			extractSQL.printSummary();
			extractSQL.testFunction();
			extractSQL.closeServer();
			*/
			//extractGDB.run2();
			extractGDB.connectToServer();
			//extractGDB.clearDatabase();
			//extractGDB.printData();
			extractGDB.printSummary();
			extractGDB.testFunction();
			extractGDB.closeServer();
		}
		else if(option == 6) {
			
			//
			ThreadManager thr = new ThreadManager();
			thr.setMeter(meterTest);
			thr.setMeterRead(meterReadTest);
			thr.setGridOrMySQL(1); // 1 for mysql, 2 for griddb
			thr.setOption(1);
			thr.setMinutes(60);
			thr.setStopNum(1);
			
			int num = 100;
			
			thr.genereateIDs(0, 1, num);
			
			thr.startThreads(num);
			
			boolean b1 = thr.checkThreads();
			//System.out.println("B1: " + b1);
			while(b1 == false) {
				System.out.print("");
				b1 = thr.checkThreads();
				if(b1 == true) {
					System.out.println("Threads finished");
					System.out.println("Start time: " + thr.getStartTime());
					System.out.println("End time: " + thr.getEndTime());
					float timeTaken = (float) (((thr.getEndTime().getTime() - thr.getStartTime().getTime()) * 1.0) / 1000.0);
					System.out.println("Time duration (in seconds): " + timeTaken);
				} 
			}
			if(thr.checkThreads()) {
				System.out.println("Start time: " + thr.getStartTime());
				System.out.println("End time: " + thr.getEndTime());
				float timeTaken = (float) (((thr.getEndTime().getTime() - thr.getStartTime().getTime()) * 1.0) / 1000.0);
				System.out.println("Time duration (in seconds): " + timeTaken);
			}
		}
		else if(option == 7) {
			//
			extractGDB.connectToServer();
			extractGDB.clearDatabase();
			extractGDB.closeServer();
			//
			ThreadManager thr = new ThreadManager();
			thr.setMeter(meterTest);
			thr.setMeterRead(meterReadTest);
			thr.setGridOrMySQL(2); // 1 for mysql, 2 for griddb
			thr.setOption(1);
			thr.setMinutes(60);
			thr.setStopNum(1);
			
			int num = 2;
			
			thr.genereateIDs(0, 1, num);
			
			thr.startThreads(num);
			
			boolean b1 = thr.checkThreads();
			while(b1 == false) {
				System.out.print("");
				b1 = thr.checkThreads();
				if(b1 == true) {
					System.out.println("Threads finished");
					System.out.println("Start time: " + thr.getStartTime());
					System.out.println("End time: " + thr.getEndTime());
					float timeTaken = (float) (((thr.getEndTime().getTime() - thr.getStartTime().getTime()) * 1.0) / 1000.0);
					System.out.println("Time duration (in seconds): " + timeTaken);
				} 
			}
			if(thr.checkThreads()) {
				System.out.println("Start time: " + thr.getStartTime());
				System.out.println("End time: " + thr.getEndTime());
				float timeTaken = (float) (((thr.getEndTime().getTime() - thr.getStartTime().getTime()) * 1.0) / 1000.0);
				System.out.println("Time duration (in seconds): " + timeTaken);
			}
		} else if(option == 8) {
			extractGDB.connectToServer();
			extractGDB.clearDatabase();
			extractGDB.printData();
			extractGDB.closeServer();
		} else if(option == 9) {
			extractGDB.connectToServer();
			extractGDB.printData();
			extractGDB.closeServer();
		} else if(option == 10) {
			extractGDB.run();
		} else if(option == 11) {
			extractSQL.run();
		} else if(option == 12) {
			System.out.println("GridDB");
			extractGDB.run();
			System.out.println("MySQL");
			extractSQL.run();
		}
		System.out.println();
		System.out.println("-----");
	}
}