package cz.vutbr.feec.session.gnp;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import cz.vutbr.feec.iodata.ReadConfig;

public class MainGNP extends Thread {

	public MainGNP(String jmeno) {
		super(jmeno);
	}
	
	public void run() {
		float hLat=49.19106f, hLng=16.611419f;	//Brno
//		float lat=50.120346f, lng=17.382648f;	//Vrbno pod Pradedem		
		//int numOfLandmarks = 3;
		//int dispersion = 5;
		//int maxRtt = 200;
		float[] hostPosition = null;
		int[][] testConfig = null;
		int numOfTestParameters = 4;
		
		try {
			//umisteni v Brne
			if(InetAddress.getLocalHost().getHostAddress().equalsIgnoreCase("147.229.221.79")) {
				hLat = 49.19106f;
				hLng = 16.611419f;
			}
			//umisteni nactene z konfiguracniho souboru podle hostName
			else {
				try {
					hostPosition = ReadConfig.readHostPosition(InetAddress.getLocalHost().getHostName());
				} catch (IOException e) {
					e.printStackTrace();
				}
				hLat = hostPosition[0];
				hLng = hostPosition[1];
			}
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		}
		
		try {
			testConfig = ReadConfig.readTestConfiguration(numOfTestParameters);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		//vytvoreni instance tridy SequentialEstimation s parametry jako je pocet landmarku pouzitych
		//pro odhad a pozice hosta
		//SequentialEstimation est = new SequentialEstimation(numOfLandmarks, dispersion, maxRtt, hLat, hLng);
		SequentialEstimation est = new SequentialEstimation();

		for(int k=0; k<testConfig.length; k++) {
			est.setTestParameters(testConfig[k][0], testConfig[k][1], testConfig[k][2], hLat, hLng);
			for(int i=0; i<testConfig[k][numOfTestParameters-1]; i++) {
				//metoda estimation() slouzi pro samotny odhad pomoci techniky Simplex Downhill
				est.estimation();
/*
				try {
					//uspani merici aplikace na danou dobu v ms
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					System.out.println("Predcasne probuzeni!");
				}*/
			}	
			
		}
	}
	
	public static void main(String[] args) {
		MainGNP st = new MainGNP("Starter GNP");
		st.start();
	}
}
