package pt.up.fe.aiad.gui.controllers;

import jade.core.AID;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;
import pt.up.fe.aiad.scheduler.ScheduleEvent;
import pt.up.fe.aiad.scheduler.SchedulerAgent;
import pt.up.fe.aiad.scheduler.constraints.ScheduleConstraint;
import pt.up.fe.aiad.utils.FXUtils;
import pt.up.fe.aiad.utils.TimeInterval;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.stream.Collectors;

public class EditEventController {
    @FXML
    private ListView<TimeInterval> _domainView;
    private ObservableList<TimeInterval> _domain = FXCollections.observableArrayList();

    @FXML
    private ListView<ScheduleConstraint> _constraintView;
    private ObservableList<ScheduleConstraint> _constraints = FXCollections.observableArrayList();

    @FXML
    private ListView<String> _participantsView;
    private ObservableList<String> _participants = FXCollections.observableArrayList();

    @FXML
    private Button _doneButton;

    @FXML
    private TextField _durationTextField;
    @FXML
    private TextField _minDateTextField;
    @FXML
    private TextField _maxDateTextField;

    private boolean firstTimeSetup = false;

    private Stage _stage;

    private long _duration;
    private TimeInterval _maxBounds;
    private ScheduleEvent _ev;
    private SchedulerAgent _agent;

    public void initData(final Stage stage, ScheduleEvent ev, SchedulerAgent agent) {
        _agent = agent;
        _ev = ev;
        _domainView.setItems(_domain.sorted());
        _constraintView.setItems(_constraints);
        _participantsView.setItems(_participants.sorted());
        _stage = stage;

        if (ev.isInitialized()) {
            _domain.addAll(ev._availableIntervals);
            _constraints.addAll(ev._constraints);
        } else
            firstTimeSetup = true;
        _domain.addListener((ListChangeListener<TimeInterval>) c -> validateData());

        _participants.addAll(ev._participants.stream().map(AID::getName).collect(Collectors.toList()));

        _duration = ev.getDuration();

        _maxBounds = ev.getMaxBounds();

        long minutes = _duration / 60;
        _durationTextField.setText(Long.toString(minutes / 60) + "h" + Long.toString(minutes % 60) + "m");

        Calendar c1 = Calendar.getInstance();
        c1.setTimeInMillis(_maxBounds.getStartDate() * 1000);
        _minDateTextField.setText(c1.getTime().toString());
        c1.setTimeInMillis(_maxBounds.getEndDate() * 1000);
        _maxDateTextField.setText(c1.getTime().toString());

        validateData();
    }

    void validateData() {
        _doneButton.setDisable(true);

        if (_domain.size() <= 0)
            return;

        _doneButton.setDisable(false);
    }

    @FXML
    void addAvailability(ActionEvent event) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("../views/createavailability.fxml"));
            Stage stage = new Stage();
            stage.setTitle("Add Availability");
            Scene scene = new Scene(loader.load());
            scene.getStylesheets().add(getClass().getResource("../views/main.css").toExternalForm());
            stage.setScene(scene);
            stage.initModality(Modality.APPLICATION_MODAL);

            CreateAvailabilityController controller = loader.<CreateAvailabilityController>getController();
            controller.initData(stage, _domain, _duration, _maxBounds);

            stage.show();
        } catch (Exception e) {
            FXUtils.showExceptionDialog(e);
        }

        validateData();
    }

    @FXML
    void removeAvailability(ActionEvent event) {
        if (_domainView.getItems().size() > 0 && _domainView.getSelectionModel().getSelectedItem() != null) {
            _domain.remove(_domainView.getSelectionModel().getSelectedItem());
            validateData();
        }
    }

    @FXML
    void addPreference(ActionEvent event) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("../views/createpreference.fxml"));
            Stage stage = new Stage();
            stage.setTitle("Add Preference");
            Scene scene = new Scene(loader.load());
            scene.getStylesheets().add(getClass().getResource("../views/main.css").toExternalForm());
            stage.setScene(scene);
            stage.initModality(Modality.APPLICATION_MODAL);

            CreatePreferenceController controller = loader.<CreatePreferenceController>getController();
            controller.initData(stage, _constraints);

            stage.show();
        } catch (Exception e) {
            FXUtils.showExceptionDialog(e);
        }

        validateData();
    }

    @FXML
    void removePreference(ActionEvent event) {
        if (_constraintView.getItems().size() > 0 && _constraintView.getSelectionModel().getSelectedItem() != null) {
            _constraints.remove(_constraintView.getSelectionModel().getSelectedItem());
            validateData();
        }
    }

    @FXML
    void cancelEdit() {
        _stage.close();
    }

    @FXML
    void saveEventInfo(ActionEvent event) {
        ArrayList<TimeInterval> dom = new ArrayList<>();
        ArrayList<ScheduleConstraint> consts = new ArrayList<>();

        dom.addAll(_domain.stream().collect(Collectors.toList()));
        consts.addAll(_constraints.stream().collect(Collectors.toList()));

        _ev.initialize(dom, consts);
        if (firstTimeSetup) {
            _agent.acceptInvitation(_ev);
        }

        _stage.close();
    }
}

@@ -704,16 +704,51 @@
 erval;%0A%0A
+import java.text.SimpleDateFormat;%0A
 import j
@@ -2639,24 +2639,104 @@
 0) + %22m%22);%0A%0A
+        SimpleDateFormat dateFormat = new SimpleDateFormat(%22dd/MM/yyyy HH:mm%22);%0A
         Cale
@@ -2765,24 +2765,25 @@
 Instance();%0A
+%0A
         c1.s
@@ -2858,32 +2858,50 @@
 xtField.setText(
+dateFormat.format(
 c1.getTime().toS
@@ -2892,34 +2892,24 @@
 c1.getTime()
-.toString(
 ));%0A        
@@ -2994,16 +2994,34 @@
 setText(
+dateFormat.format(
 c1.getTi
@@ -3024,26 +3024,16 @@
 etTime()
-.toString(
 ));%0A%0A   

Change event max bounds text in UI