package model;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by Gondr on 5/04/2017.
 */
public class Trip {

    public transient ObservableList<TripStop> route;
    private ArrayList<TripStop> serialisedRoute;
    public String direction;
    public Ride ride;
    public boolean[] days;
    public boolean reoccur;
    public String endDate;
    public String name;

    public static int MONDAY = 0;
    public static int TUESDAY = 1;
    public static int WEDNESDAY = 2;
    public static int THURSDAY = 3;
    public static int FRIDAY = 4;
    public static int SATURDAY = 5;
    public static int SUNDAY = 6;

    public static String TO_UNI = "University";
    public static String TO_HOME = "Home";

    public enum Days{
        MONDAY("Monday", 0), TUESDAY("Tuesday", 1), WEDNESDAY("Wednesday", 2), THURSDAY("THURSDAY", 3), FRIDAY("Friday", 4),
        SATURDAY("SATURDAY", 5), SUNDAY("SUNDAY", 6);

        public String dayName;
        public int dayIndex;

        Days(String dayName, int dayIndex){
            this.dayName = dayName;
            this.dayIndex = dayIndex;
        }

        public static Days getValue(int dayIndex){
            switch(dayIndex){
                case 0:
                    return MONDAY;
                case 1:
                    return TUESDAY;
                case 2:
                    return WEDNESDAY;
                case 3:
                    return THURSDAY;
                case 4:
                    return FRIDAY;
                case 5:
                    return SATURDAY;
                case 6:
                    return SUNDAY;
            }
            return null;
        }
    }

    public Trip(String name, ObservableList<TripStop> route, String direction, Ride ride, boolean[] days, boolean reoccur, String endDate){
        this.name = name;
        this.route = route;
        this.direction = direction;
        this.ride = ride;
        this.days = days;
        this.reoccur = reoccur;
        this.endDate = endDate;
    }

    public String toString(){
        return name;
    }

    public boolean equals(Trip trip){
        if (trip.name.equals(name) && trip.direction.equals(direction) && ride.equals(trip.ride) && Arrays.equals(trip.days,days) && trip.reoccur == reoccur
                && trip.endDate.equals(endDate)){
            for (int i = 0; i < trip.route.size(); i++){
                if (!route.get(i).equals(trip.route.get(i))){
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public void serialise(){
        serialisedRoute = new ArrayList<>(route);
        for (TripStop trip: serialisedRoute){
            trip.serialise();
        }
    }

    public void deserialise(){
        route = FXCollections.observableArrayList(serialisedRoute);
        for (TripStop trip: serialisedRoute){
            trip.deserialise();
        }
    }

}
