/*
 * This source file was generated by the Gradle 'init' task
 */
package org.example;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class App {

    ArrayList<Train> trains;

    enum TrainType {
        INTERCITY,
        SPRINTER,
    }

    class Station {
        String name;
        Boolean isIntercity;

        public Station(String name, Boolean isIntercity) {
            this.name = name;
            this.isIntercity = isIntercity;
        }

        public String getName() {
            return this.name;
        }

        public Boolean isIntercity() {
            return this.isIntercity;
        }

        public String toString() {
            if (this.isIntercity) {
                return "(" + this.name + ")";
            }
            return this.name;
        }
    }

    class Train {
        TrainType type;
        String departTime;
        String arrivalTime;
        Station[] stops;

        public Train(TrainType type, String departTime, String arrivalTime, Station[] stops) {
            this.type = type;
            this.departTime = departTime;
            this.arrivalTime = arrivalTime;
            this.stops = stops.clone();
        }

        public Station getStartStation() {
            return this.stops[0];
        }

        public Station getFinalStation() {
            return this.stops[this.stops.length - 1];
        }
    }

    class LineData {
        private String name;
        private String data;

        public String getName() {
            return this.name;
        }

        public String getData() {
            return this.data;
        }

        public LineData(String name, String data) {
            this.name = name;
            this.data = data;
        }
    }

    LineData getDataFromLine(String textLine) {
        String[] result = textLine.split(": ");
        LineData data = new LineData(result[0], result[1]);
        return data;
    }

    ArrayList<Train> trainsFromTo(String fromStationName, String toStationName) {
        ArrayList<Train> matches = new ArrayList<>();
        for (Train train : this.trains) {
            Boolean hasFromStation = false;
            Boolean hasToStation = false;
            for (Station stop : train.stops) {
                if (train.type == TrainType.INTERCITY) {
                    if (stop.isIntercity() && stop.getName().equals(fromStationName) && !hasFromStation) {
                        hasFromStation = true;
                    }
                    if (stop.isIntercity() && stop.getName().equals(toStationName) && hasFromStation && !hasToStation) {
                        hasToStation = true;
                    }
                } else {
                    if (stop.getName().equals(fromStationName) && !hasFromStation) {
                        hasFromStation = true;
                    }
                    if (stop.getName().equals(toStationName) && hasFromStation && !hasToStation) {
                        hasToStation = true;
                    }
                }
            }
            if (hasFromStation && hasToStation) {
                matches.add(train);
            }
        }
        return matches;
    }

    void readTrains(String filepath) throws FileNotFoundException {
        ArrayList<Train> trains = new ArrayList<>();
        Scanner sc = new Scanner(new File("test.txt"));
        while (sc.hasNext()) {
            String trainTypeString = getDataFromLine(sc.nextLine()).data;
            TrainType type;
            if (trainTypeString.equals("INTERCITY")) {
                type = TrainType.INTERCITY;
            } else {
                type = TrainType.SPRINTER;
            }
            String departTime = getDataFromLine(sc.nextLine()).data;
            String arrivalTime = getDataFromLine(sc.nextLine()).data;
            ArrayList<Station> stops = new ArrayList<>();
            while (sc.hasNext()) {
                String line = sc.nextLine();
                if (line.equals("END")) {
                    break;
                }
                LineData lineData = getDataFromLine(line);
                Boolean isIntercityStation;
                if (lineData.name.equals("IC-STATION")) {
                    isIntercityStation = true;
                } else {
                    isIntercityStation = false;
                }
                String stationName = lineData.data;
                Station stop = new Station(stationName, isIntercityStation);
                stops.add(stop);
            }
            Station[] allStations = new Station[stops.size()];
            Train train = new Train(type, departTime, arrivalTime, stops.toArray(allStations));
            trains.add(train);
        }
        sc.close();
        this.trains = trains;
    }

    public String getGreeting() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        String filepath;
        if (args.length < 1) {
            System.out.print("Filename: ");
            Scanner sc = new Scanner(System.in);
            filepath = sc.next();
            System.out.println();
            sc.close();
        } else {
            filepath = args[0];
        }
        App a = new App();
        System.out.println("Reading input from: " + filepath);
        ArrayList<Train> trains;
        try {
            a.readTrains(filepath);
            trains = a.trains;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            trains = null;
        }
        if (trains == null) {
            return;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("Get from: ");
        String fromStationName = sc.nextLine();
        System.out.println("Get to: ");
        String toStationName = sc.nextLine();
        ArrayList<Train> trains2 = a.trainsFromTo(fromStationName, toStationName);
        for (Train t : trains2) {
            System.out.println("found " + t.type.toString() + " from " + t.getStartStation().getName() + " to "
                    + t.getFinalStation().getName());
        }
        sc.close();
    }
}
