package de.siphalor.was.visual.jfx;

import de.siphalor.was.WhatAStorage;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.input.*;

@SuppressWarnings("unused")
public class StorageSlotController {
	private final int x;
	private final int y;

	@FXML
	private Parent slot;

	public ScalingImagePane slot0;
	public Label slot0Title;

	public ScalingImagePane slot1;
	public Label slot1Title;

	public ScalingImagePane slot2;
	public Label slot2Title;

	public StorageSlotController(int x, int y) {
		this.x = x;
		this.y = y;
	}

	private boolean canDropHere(DragEvent dragEvent) {
		if (dragEvent.getGestureSource() != slot && dragEvent.getDragboard().hasContent(DraggedProduct.FORMAT)) {
			DraggedProduct origin = (DraggedProduct) dragEvent.getDragboard().getContent(DraggedProduct.FORMAT);
			if (origin instanceof DraggedProduct.Quest) {
				return WhatAStorage.getInstance().canStoreProduct(((DraggedProduct.Quest) origin).index, x, y);
			} else if (origin instanceof DraggedProduct.Slot){
				return WhatAStorage.getInstance().canMoveProduct(((DraggedProduct.Slot) origin).x, ((DraggedProduct.Slot) origin).y, x, y);
			}
		}
		return false;
	}

	@FXML
	private void onDragDetected(MouseEvent event) {
		int index;
		Image image;
		if (slot2.getImage() != null) {
			image = JFXUtil.scaleTo(slot2.getImage(), 100);
		} else if (slot1.getImage() != null) {
			image = JFXUtil.scaleTo(slot1.getImage(), 100);
		} else if (slot0.getImage() != null){
			image = JFXUtil.scaleTo(slot0.getImage(), 100);
		} else {
			event.consume();
			return;
		}

		Dragboard dragboard = slot.startDragAndDrop(TransferMode.MOVE);
		dragboard.setDragView(image);
		ClipboardContent content = new ClipboardContent();
		content.put(DraggedProduct.FORMAT, new DraggedProduct.Slot(x, y));
		dragboard.setContent(content);

		event.consume();
	}

	@FXML
	private void onDragEntered(DragEvent dragEvent) {
		JFXUtil.setStyleClass(slot, "drop-hover", canDropHere(dragEvent));
	}

	@FXML
	private void onDragExited(DragEvent dragEvent) {
		JFXUtil.setStyleClass(slot, "drop-hover", false);
	}

	@FXML
	private void onDragOver(DragEvent dragEvent) {
		if (canDropHere(dragEvent)) {
			dragEvent.acceptTransferModes(TransferMode.MOVE);
		}
		dragEvent.consume();
	}

	@FXML
	private void onDragDropped(DragEvent dragEvent) {
		if (dragEvent.getGestureSource() != slot && dragEvent.getDragboard().hasContent(DraggedProduct.FORMAT)) {
			DraggedProduct origin = (DraggedProduct) dragEvent.getDragboard().getContent(DraggedProduct.FORMAT);
			if (origin instanceof DraggedProduct.Quest) {
				WhatAStorage.getInstance().storeProduct(((DraggedProduct.Quest) origin).index, x, y);
				dragEvent.setDropCompleted(true);
				return;
			} else if (origin instanceof DraggedProduct.Slot) {
				DraggedProduct.Slot slot = (DraggedProduct.Slot) origin;
				WhatAStorage.getInstance().moveProduct(slot.x, slot.y, x, y);
				dragEvent.setDropCompleted(true);
				return;
			}
		}
		dragEvent.setDropCompleted(false);
	}
}
