package be.iminds.ilabt.jfed.connectivity_tester;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * User: twalcari
 * Date: 12/19/13
 * Time: 5:16 PM
 */
public class Main {

    private static final int POS_HOSTNAME = 0;
    private static final int POS_PORT = 1;
    private static final int POS_DESCRIPTION = 2;
    private static final int POS_CATEGORY = 3;

    public static void main(String[] args) {


        List<ConnectivityTest> tests = new ArrayList<>();
        tests.add(new HasIPv4AddressTest());
        tests.add(new HasIPv6AddressTest());
        tests.add(new Ping6Test());

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream("/ports.csv")))) {

            //ignore first line
            String line = reader.readLine();
            assert (line != null);

            while ((line = reader.readLine()) != null) {

                String[] parts = line.split(";");
                tests.add(new HostAndPortTest(parts[POS_HOSTNAME], Integer.parseInt(parts[POS_PORT]), parts[POS_DESCRIPTION], parts[POS_CATEGORY]));

            }


        } catch (IOException e) {
            e.printStackTrace();
        }

        for (ConnectivityTest test : tests) {
            try {
                ConnectivityTest.ConnectivityTestResult result = test.call();
                System.out.println(test.getName() + " status: " + result.getStatus());
                if (result.getMessage() != null)
                    System.out.println(test.getName() + " message: " + result.getMessage());
                if (result.getException() != null)
                    System.out.println(test.getName() + " exception: " + result.getException());
            } catch (Exception e) {
                System.out.println(test.getName() + " threw an exception: " + e.getMessage());
                e.printStackTrace();
            }
        }


    }

}
