package ch.zhaw.gartenverwaltung;

import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel;
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.plantList.PlantListModel;
import ch.zhaw.gartenverwaltung.taskList.TaskListModel;
import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.Plant;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.*;

public class MyPlantsController implements Initializable {
    MainFXMLController mainController;
    private final TaskListModel taskListModel = new TaskListModel();
    private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel);
    private final PlantListModel plantListModel = new PlantListModel();

    @FXML
    private VBox myPlants_vbox;

    public MyPlantsController() throws IOException {
    }

    @FXML
    void addPlant(ActionEvent event) throws IOException {
        mainController.loadPane("Plants.fxml");
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        //ToDo update, when new crops are added
        try {
            loadCropList();
        } catch (HardinessZoneNotSetException | IOException e) {
            e.printStackTrace();
        }
    }

    private void loadCropList() throws HardinessZoneNotSetException, IOException {
        List<Crop> cropList = new LinkedList<>();
        try {
            cropList = getCropList();
        } catch (IOException e) {
            e.printStackTrace();
        }
        createPlantView(cropList);
    }

    private void createPlantView(List<Crop> crops) throws HardinessZoneNotSetException, IOException {
        myPlants_vbox.getChildren().clear();
        for(Crop crop : crops) {
            HBox hBox = createPlantView(crop);
            myPlants_vbox.getChildren().add(hBox);
        }
    }

    public void getMainController(MainFXMLController controller) {
        mainController = controller;
    }

    private List<Crop> getCropList() throws IOException {
        List<Crop> cropList;
        cropList = gardenplanmodel.getCrops();
        return cropList;
    }

    private HBox createPlantView(Crop crop) throws HardinessZoneNotSetException, IOException {
        //ToDo add better design
        Plant plant = plantListModel.getFilteredPlantListById(Config.getCurrentHardinessZone(), crop.getPlantId()).get(0);
        HBox hBox = new HBox(10);
        ImageView imageView = new ImageView();
        imageView.setPreserveRatio(false);
        imageView.setFitHeight(100);
        imageView.setFitWidth(100);
        imageView.maxHeight(100);
        if (plant.image() != null) {
            imageView.setImage(plant.image());
        }
        hBox.setMinHeight(100);
        Label label = new Label(plant.name());
        label.setMaxWidth(2000);
        HBox.setHgrow(label, Priority.ALWAYS);
        Button details = new Button("Details");
        Button delete = new Button("delete");
        details.setOnAction(getGoToCropDetailEvent(crop));
        delete.setOnAction(getDeleteCropEvent(crop));
        hBox.getChildren().addAll(imageView, label, details, delete);
        return hBox;
    }

    private EventHandler<ActionEvent> getGoToCropDetailEvent(Crop crop) {
        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Parent root;
                FXMLLoader fxmlLoader = new FXMLLoader(Objects.requireNonNull(getClass().getResource("CropDetail.fxml")));
                try {
                    root = fxmlLoader.load();
                    CropDetailController controller = fxmlLoader.getController();
                    controller.setPlantFromCrop(crop);
                    Stage stage = new Stage();
                    stage.setScene(new Scene(root));
                    stage.initModality(Modality.APPLICATION_MODAL);
                    stage.setResizable(true);
                    stage.showAndWait();
                } catch (IOException | HardinessZoneNotSetException e) {
                    e.printStackTrace();
                }
            }
        };
        return event;
    }

    private EventHandler<ActionEvent> getDeleteCropEvent(Crop crop) {
        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                try {
                    showConfirmation(crop);
                } catch (IOException | HardinessZoneNotSetException e) {
                    e.printStackTrace();
                }
            }
        };
        return event;
    }

    private void showConfirmation(Crop crop) throws IOException, HardinessZoneNotSetException {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Delete Crop");
        alert.setHeaderText("Are you sure want to delete this Crop?");
        alert.setContentText("placeholder");

        Optional<ButtonType> option = alert.showAndWait();

        if (option.get() == ButtonType.OK) {
            gardenplanmodel.removeCrop(crop);
            loadCropList();
        }
    }
}
