package controllers;

import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.paint.Color;
import model.*;

import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

/**
 * Created by Gondr on 24/05/2017.
 */
public class MyTripsController extends Controller{
    @FXML
    private Label rideLabel;
    @FXML
    private Label directionLabel;
    @FXML
    private Label mondayLabel;
    @FXML
    private Label tuesdayLabel;
    @FXML
    private Label wednesdayLabel;
    @FXML
    private Label thursdayLabel;
    @FXML
    private Label fridayLabel;
    @FXML
    private Label saturdayLabel;
    @FXML
    private Label sundayLabel;
    @FXML
    private Label reoccuringLabel;
    @FXML
    private Label endDateLabel;
    @FXML
    private TableView<TripStop> stopsList;
    @FXML
    private ListView<Trip> tripsList;
    private ObservableList<Trip> trips;
    @FXML
    private TableColumn<TripStop, String> stopTimeColumn;
    @FXML
    private TableColumn<TripStop, String> stopNameColumn;
    @FXML
    private TextField seatsAvailable;
    private Trip selectedTrip;

    private void setRideDays(Label day, boolean travelling){
        if (travelling){
            day.setTextFill(Color.GREEN);
        }else{
            day.setTextFill(Color.RED);
        }
    }

    public void shareThisRide(){
        if (selectedTrip != null && seatsAvailable.getText() != null && seatsAvailable.getText() != ""){
            int seats;
            try{
                seats = Integer.parseInt(seatsAvailable.getText());
            }catch(NumberFormatException e){
                popUp(Alert.AlertType.WARNING, "WARNING!", "Error in Seats Available Input.", "Seats Input must be an Integer.");
                return;
            }
            if (selectedTrip.ride.getNumSeats() < 0){
                Optional<ButtonType> result = popUp(Alert.AlertType.WARNING, "WARNING!", "Seats less than 0.", "This will make all seats unavailable is this what you want?", ButtonSets.YesNo);
                if (result.get() == ButtonTypes.No){
                    return;
                }
            }
            if (selectedTrip.ride.getNumSeats() > seats){
                parent.getSession().getDataManager().addSharedTrip(new SharedTrip(selectedTrip, seats));
                popUp(Alert.AlertType.CONFIRMATION, "SUCCESS!", "Trip has been successfully shared", "The trip can now be seen by other users when there are seats available.");
                try {
                    changeScene(SceneCode.SHARED_RIDES);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else{
                popUp(Alert.AlertType.WARNING, "WARNING!", "Error in Seats Available Input.", "You cannot share more seats than you have available (remember you need one for you to drive in).");
            }
        }
    }

    @Override
    public void runLater(){
        //fill tables etc;
        trips = parent.getSession().getDataManager().getTrips();
        tripsList.setItems(trips);
        stopTimeColumn.setCellValueFactory(p -> p.getValue().nameProperty());
        stopNameColumn.setCellValueFactory(p -> p.getValue().nameProperty());
        tripsList.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            selectedTrip = tripsList.getSelectionModel().getSelectedItem();
            stopsList.setItems(selectedTrip.route);
            rideLabel.setText(selectedTrip.ride.toString());
            directionLabel.setText(selectedTrip.direction);
            setRideDays(mondayLabel, selectedTrip.days[Trip.MONDAY]);
            setRideDays(tuesdayLabel, selectedTrip.days[Trip.TUESDAY]);
            setRideDays(wednesdayLabel, selectedTrip.days[Trip.WEDNESDAY]);
            setRideDays(thursdayLabel, selectedTrip.days[Trip.THURSDAY]);
            setRideDays(fridayLabel, selectedTrip.days[Trip.FRIDAY]);
            setRideDays(saturdayLabel, selectedTrip.days[Trip.SATURDAY]);
            setRideDays(sundayLabel, selectedTrip.days[Trip.SUNDAY]);
            if (selectedTrip.reoccur){
                reoccuringLabel.setText("Yes");
                endDateLabel.setText(selectedTrip.endDate);
            }else{
                reoccuringLabel.setText("No");
                endDateLabel.setText("-");
            }
        });
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        rideLabel.setText("-");
        directionLabel.setText("-");
        reoccuringLabel.setText("-");
        endDateLabel.setText("-");
    }
}
