package de.siphalor.was.visual.jfx.controller;

import de.siphalor.was.WhatAStorage;
import de.siphalor.was.assets.AssetsManager;
import de.siphalor.was.visual.jfx.util.DraggedProduct;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;

@SuppressWarnings("unused")
public class MainController {
	private final WhatAStorage was;

	public Label budgetLabel;
	public Button balanceButton;
	public Button optionsButton;

	public Pane storagePane;

	public ImageView trash;

	public VBox questBox;
	public Button nextQuestButton;

	public MainController(WhatAStorage was) {
		this.was = was;
	}

	@FXML
	private void scheduleStop() {
		was.scheduleStop();
	}

	@FXML
	private void nextQuest() {
		was.userRequestQuest();
	}

	@FXML
	private void onTrashDragEntered(DragEvent dragEvent) {
		if (dragEvent.getDragboard().hasContent(DraggedProduct.FORMAT)) {
			AssetsManager.getStream("textures/bin_open.png").ifPresent(inputStream -> {
				trash.setImage(new Image(inputStream));
			});
		}
	}
	@FXML
	private void onTrashDragExited(DragEvent dragEvent) {
		AssetsManager.getStream("textures/bin_closed.png").ifPresent(inputStream -> {
			trash.setImage(new Image(inputStream));
		});
	}
	@FXML
	private void onTrashDragOver(DragEvent dragEvent) {
		if (dragEvent.getDragboard().hasContent(DraggedProduct.FORMAT)) {
			dragEvent.acceptTransferModes(TransferMode.MOVE);
		}

		dragEvent.consume();
	}

	@FXML
	private void onTrashDragDropped(DragEvent dragEvent) {
		if (dragEvent.getDragboard().hasContent(DraggedProduct.FORMAT)) {
			DraggedProduct origin = (DraggedProduct) dragEvent.getDragboard().getContent(DraggedProduct.FORMAT);

			if (origin instanceof DraggedProduct.Quest) {
				DraggedProduct.Quest questOrigin = (DraggedProduct.Quest) origin;
				was.userAbandonQuest(questOrigin.index);

				dragEvent.setDropCompleted(true);
				dragEvent.consume();
				return;
			} else if (origin instanceof DraggedProduct.Slot) {
				was.userDestroyProduct(((DraggedProduct.Slot) origin).x, ((DraggedProduct.Slot) origin).y);

				dragEvent.setDropCompleted(true);
				dragEvent.consume();
				return;
			}
		}
		dragEvent.setDropCompleted(false);
	}
}
