package net.runelite.client.plugins.itemspawns;

import lombok.AccessLevel;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import mapped.util.RSPSConstants;
import net.runelite.api.*;
import net.runelite.api.coords.WorldPoint;
import net.runelite.api.events.FocusChanged;
import net.runelite.api.events.MenuEntryAdded;
import net.runelite.api.events.MenuOptionClicked;
import net.runelite.client.RuneLiteProperties;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.eventbus.EventBus;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.input.KeyManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.rs.ClientConfigLoader;
import net.runelite.client.ui.ClientToolbar;
import net.runelite.client.ui.NavigationButton;
import net.runelite.client.util.ImageUtil;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.awt.image.BufferedImage;

@PluginDescriptor(
		name = "Item Spawn Editor",
		description = "Edit Item spawns",
		tags = {"tool", "spawn", "item"},
		enabledByDefault = false,
		developerPlugin = true
)
@Slf4j
@Singleton
public class ItemSpawnsPlugin extends Plugin {

	@Inject
	private Client client;

	@Inject
	private ClientThread clientThread;

	@Inject
	private ClientToolbar clientToolbar;

	@Inject
	private KeyManager keyManager;

	@Inject
	private EventBus eventbus;

	private ItemSpawnsPanel uiPanel;

	private static final String WALK_HERE = "Walk here";
	// Option added to NPC menu
	private static final String TAG = "Remove G_Item";
	private static final String SPAWN = "Spawn G_Item";
	private NavigationButton uiNavigationButton;

	@Override
	protected void startUp() throws Exception {
		if (RuneLiteProperties.getLocalEnabled()) {
			final BufferedImage icon = ImageUtil.getResourceStreamFromClass(getClass(), "item.png");
			uiPanel = injector.getInstance(ItemSpawnsPanel.class);

			uiNavigationButton = NavigationButton.builder()
					.tooltip("Ground Item Spawns")
					.icon(icon)
					.priority(5)
					.panel(uiPanel)
					.build();

			clientToolbar.addNavigation(uiNavigationButton);
		}
	}

	@Override
	protected void shutDown() throws Exception {
		eventbus.unregister(this);
		clientToolbar.removeNavigation(uiNavigationButton);
	}

	@Subscribe
	private void onMenuEntryAdded(MenuEntryAdded event) {

		int type = event.getType();

		if (type >= MenuAction.MENU_ACTION_DEPRIORITIZE_OFFSET) {
			type -= MenuAction.MENU_ACTION_DEPRIORITIZE_OFFSET;
		}

		if (client.isKeyPressed(KeyCode.KC_SHIFT) && event.getOption().equals(WALK_HERE)) {
			final Tile selectedSceneTile = client.getSelectedSceneTile();

			if (selectedSceneTile == null) {
				return;
			}


			final Tile tile = client.getSelectedSceneTile();
			if (tile == null) {
				return;
			}
			client.createMenuEntry(-1)
					.setParam0(event.getActionParam0())
					.setParam1(event.getActionParam1())
					.setTarget(event.getTarget())
					.setOption(SPAWN)
					.setType(MenuAction.RUNELITE)
					.setIdentifier(event.getIdentifier());
		} else if (client.isKeyPressed(KeyCode.KC_SHIFT) && type == MenuAction.EXAMINE_ITEM_GROUND.getId()) {
			// Add tag option
			client.createMenuEntry(-1)
					.setParam0(event.getActionParam0())
					.setParam1(event.getActionParam1())
					.setTarget(event.getTarget())
					.setOption(TAG)
					.setType(MenuAction.RUNELITE)
					.setIdentifier(event.getIdentifier());
		}
	}

	@Subscribe
	private void onMenuOptionClicked(MenuOptionClicked click) {
		if (RuneLiteProperties.getLocalEnabled()) {
			if (click.getMenuAction() == MenuAction.RUNELITE && (click.getMenuOption().equals(SPAWN))) {
				Tile target = client.getSelectedSceneTile();

				if (target == null) {
					return;
				}
				WorldPoint worldPoint = target.getWorldLocation();
				int itemId = uiPanel.getItemId();
				int amount = uiPanel.getItemAmount();
				int respawnTime = uiPanel.getRespawnTime();
				String command = String.format("::itemspawn %d %d %d %d %d", itemId, amount, worldPoint.getX(), worldPoint.getY(), respawnTime);
				client.addChatMessage(ChatMessageType.CONSOLE, "", command, "RL+");

				client.runScript(ScriptID.CHAT_SEND, ChatMessageType.PUBLICCHAT, command);
				click.consume();
				return;
			}
			if (click.getMenuAction() == MenuAction.RUNELITE && (click.getMenuOption().equals(TAG))) {
				int itemId = click.getId();
				Scene scene = client.getScene();
				Tile tile = scene.getTiles()[client.getPlane()][click.getParam0()][click.getParam1()];
				ItemLayer tileItemPile = tile.getItemLayer();

				if (tileItemPile == null) {
					return;
				}

				String command = String.format("::removeitem %d %d %d", itemId, tile.getWorldLocation().getX(), tile.getWorldLocation().getY());
				client.runScript(ScriptID.CHAT_SEND, ChatMessageType.PUBLICCHAT, command);
				click.consume();
				return;
			}
		}
	}
}
