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

import de.siphalor.was.WhatAStorage;
import de.siphalor.was.content.quest.Quest;
import de.siphalor.was.visual.jfx.util.DraggedProduct;
import de.siphalor.was.visual.jfx.util.JFXUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.input.*;
import javafx.scene.layout.GridPane;

@SuppressWarnings("unused")
public class QuestController {
	private final Quest.Type type;

	@FXML
	private GridPane questContainer;

	public Label title;
	public Label description;
	public Label reward;

	public ImageView image;

	public QuestController(Quest.Type type) {
		this.type = type;
	}

	@FXML
	private void abandon() {
		WhatAStorage.getInstance().userAbandonQuest(getIndex());
	}

	private boolean canDropHere(DragEvent dragEvent) {
		if (type != Quest.Type.OUT) {
			return false;
		}
		if (dragEvent.getDragboard().hasContent(DraggedProduct.FORMAT)) {
			DraggedProduct product = (DraggedProduct) dragEvent.getDragboard().getContent(DraggedProduct.FORMAT);
			if (product instanceof DraggedProduct.Quest) {
				if (WhatAStorage.getInstance().getOptions().getAllowQuestResolving()) {
					DraggedProduct.Quest quest = (DraggedProduct.Quest) product;
					return WhatAStorage.getInstance().canQuestsResolve(quest.index, getIndex());
				}
			} else if (product instanceof DraggedProduct.Slot) {
				DraggedProduct.Slot slot = (DraggedProduct.Slot) product;
				return WhatAStorage.getInstance().canDeliverProduct(getIndex(), slot.x, slot.y);
			}
		}
		return false;
	}

	@FXML
	private void onDragDetected(MouseEvent event) {
		if (type == Quest.Type.IN) {
			Dragboard dragboard = questContainer.startDragAndDrop(TransferMode.MOVE);
			dragboard.setDragView(JFXUtil.scaleTo(image.getImage(), 100));

			ClipboardContent content = new ClipboardContent();
			content.put(DraggedProduct.FORMAT, new DraggedProduct.Quest(getIndex()));
			dragboard.setContent(content);
		}

		event.consume();
	}

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

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

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

	@FXML
	private void onDragDropped(DragEvent dragEvent) {
		Dragboard dragboard = dragEvent.getDragboard();
		if (dragboard.hasContent(DraggedProduct.FORMAT)) {
			DraggedProduct product = (DraggedProduct) dragboard.getContent(DraggedProduct.FORMAT);
			if (product instanceof DraggedProduct.Slot) {
				DraggedProduct.Slot slot = (DraggedProduct.Slot) product;
				if (WhatAStorage.getInstance().userDeliverProduct(getIndex(), slot.x, slot.y)) {
					dragEvent.setDropCompleted(true);
					dragEvent.consume();
					return;
				}
			} else if (product instanceof DraggedProduct.Quest) {
				DraggedProduct.Quest quest = (DraggedProduct.Quest) product;
				if (WhatAStorage.getInstance().userResolveQuests(quest.index, getIndex())) {
					dragEvent.setDropCompleted(true);
					dragEvent.consume();
					return;
				}
			}
		}
		dragEvent.setDropCompleted(false);
	}

	private int getIndex() {
		return questContainer.getParent().getChildrenUnmodifiable().indexOf(questContainer);
	}
}
