/*
 * Copyright (c) 2018, Infinitay <https://github.com/Infinitay>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.runelite.client.plugins.kingdomofmiscellania;

import com.google.common.eventbus.Subscribe;
import com.google.common.primitives.Ints;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.inject.Inject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.Varbits;
import net.runelite.api.events.MapRegionChanged;
import net.runelite.api.events.VarbitChanged;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.ui.overlay.infobox.InfoBoxManager;

@PluginDescriptor(
	name = "Kingdom of Miscellania",
	enabledByDefault = false
)
@Slf4j
public class KingdomPlugin extends Plugin
{
	private static final int[] KINGDOM_REGION = {9787, 9788, 9789, 10043, 10044, 10045, 10299, 10300, 10301};

	@Inject
	private Client client;

	@Inject
	private InfoBoxManager infoBoxManager;

	@Getter
	private int favor = 0, coffer = 0;

	private KingdomCounter counter;
	private BufferedImage counterImage;

	@Override
	protected void startUp() throws Exception
	{
		synchronized (ImageIO.class)
		{
			counterImage = ImageIO.read(getClass().getResourceAsStream("teak_chest.png"));
		}
	}

	@Override
	protected void shutDown() throws Exception
	{
		removeKingdomInfobox();
	}

	@Subscribe
	public void onVarbitChanged(VarbitChanged event)
	{
		favor = client.getVar(Varbits.KINGDOM_FAVOR);
		coffer = client.getVar(Varbits.KINGDOM_COFFER);
		processInfobox();
	}

	@Subscribe
	public void onRegionChanged(MapRegionChanged event)
	{
		processInfobox();
	}

	private void processInfobox()
	{
		if (client.getGameState() == GameState.LOGGED_IN && hasCompletedQuest() && isInKingdom())
		{
			addKingdomInfobox();
		}
		else
		{
			removeKingdomInfobox();
		}

	}

	private void addKingdomInfobox()
	{
		if (counter == null)
		{
			counter = new KingdomCounter(counterImage, this);
			infoBoxManager.addInfoBox(counter);
			log.debug("Added Kingdom Infobox");
		}
	}

	private void removeKingdomInfobox()
	{
		if (counter != null)
		{
			infoBoxManager.removeInfoBox(counter);
			counter = null;
			log.debug("Removed Kingdom Infobox");
		}
	}

	private boolean isInKingdom()
	{
		return Ints.indexOf(client.getMapRegions(), KINGDOM_REGION) >= 0;
	}

	private boolean hasCompletedQuest()
	{
		return client.getVar(Varbits.THRONE_OF_MISCELLANIA_QUEST) == 1;
	}

	static int getFavorPercent(int favor)
	{
		return (favor * 100) / 127;
	}

}

@@ -1477,26 +1477,28 @@
 mon.
-eventbus.Subscribe
+collect.ImmutableSet
 ;%0Aim
@@ -1524,23 +1524,26 @@
 mon.
-primitives.Ints
+eventbus.Subscribe
 ;%0Aim
@@ -1822,25 +1822,25 @@
 .events.
-MapRegion
+GameState
 Changed;
@@ -2203,91 +2203,76 @@
 nal 
-int%5B%5D KINGDOM_REGION = %7B9787, 9788, 9789, 10043, 10044, 10045, 10299, 10300, 10301%7D
+ImmutableSet%3CInteger%3E KINGDOM_REGION = ImmutableSet.of(10044, 10300)
 ;%0A%0A%09
@@ -2959,49 +2959,108 @@
 d on
-RegionChanged(MapRegionChanged event)%0A%09%7B%0A
+GameStateChanged(GameStateChanged event)%0A%09%7B%0A%09%09if (event.getGameState() == GameState.LOGGED_IN)%0A%09%09%7B%0A%09
 %09%09pr
@@ -3071,24 +3071,28 @@
 sInfobox();%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09private
@@ -3715,65 +3715,89 @@
 urn 
-Ints.indexOf(client.getMapReg
+KINGDOM_REGION.contains(client.getLocalPlayer().getWorldLocat
 ion
-s
 ()
-, KINGDOM_REGION) %3E= 0
+.getRegionID())
 ;%0A%09%7D

Check player position on state change