package area.gui;

import java.util.Arrays;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;

import battle.BattleCalculator;
import effect.SoundEffect;
import main.MainServerOption;
import util.GuiUtil;
import util.YamlLoader;

public class AreaSpawnSpecialMonsterListGui extends GuiUtil{

	private String uniqueCode = "¡×0¡×0¡×2¡×0¡×8¡×r";
	
	public void areaSpawnSpecialMonsterListGui(Player player, int page,String areaName,String ruleCount)
	{
	  	YamlLoader monsterYaml = new YamlLoader();
		monsterYaml.getConfig("Monster/MonsterList.yml");
		Inventory inv = Bukkit.createInventory(null, 54, uniqueCode + "¡×0¿µ¿ª Æ¯¼ö ¸ó½ºÅÍ  : " + (page+1));

		String[] monsterList= monsterYaml.getKeys().toArray(new String[0]);

		byte loc=0;
		String monsterName = null;
		StringBuilder sb = new StringBuilder();
		for(int count = page*45; count < monsterList.length;count++)
		{
			if(count > monsterList.length || loc >= 45) break;
			monsterName = monsterList[count];
			sb = new StringBuilder();
			sb.append("%enter%¡×f¡×l ÀÌ¸§ : ¡×f");
			sb.append(monsterYaml.getString(monsterName+".Name"));
			sb.append("%enter%");
			sb.append("¡×f¡×l Å¸ÀÔ : ¡×f");
			sb.append(monsterYaml.getString(monsterName+".Type"));
			sb.append("%enter%");
			sb.append("¡×f¡×l ½ºÆù ¹ÙÀÌ¿È : ¡×f");
			sb.append(monsterYaml.getString(monsterName+".Biome"));
			sb.append("%enter%");
			sb.append("¡×c¡×l »ý¸í·Â : ¡×f");
			sb.append(monsterYaml.getInt(monsterName+".HP"));
			sb.append("%enter%");
			sb.append("¡×b¡×l °æÇèÄ¡ : ¡×f");
			sb.append(monsterYaml.getInt(monsterName+".EXP"));
			sb.append("%enter%");
			sb.append("¡×e¡×l µå¶ø ±Ý¾× : ¡×f");
			sb.append(monsterYaml.getInt(monsterName+".MIN_Money"));
			sb.append(" ~ ");
			sb.append(monsterYaml.getInt(monsterName+".MAX_Money"));
			sb.append("%enter%");
			sb.append("¡×c¡×l ");
			sb.append(MainServerOption.statSTR);
			sb.append(" : ¡×f");
			sb.append(monsterYaml.getInt(monsterName+".STR"));
			sb.append("¡×7 [¹°°ø : ");
			sb.append(BattleCalculator.getCombatDamage(null, 0, monsterYaml.getInt(monsterName+".STR"), true));
			sb.append(" ~ ");
			sb.append(BattleCalculator.getCombatDamage(null, 0, monsterYaml.getInt(monsterName+".STR"), false));
			sb.append("]%enter%");
			sb.append("¡×a¡×l ");
			sb.append(MainServerOption.statDEX);
			sb.append(" : ¡×f");
			sb.append(monsterYaml.getInt(monsterName+".DEX"));
			sb.append("¡×7 [È°°ø : ");
			sb.append(BattleCalculator.returnRangeDamageValue(null, monsterYaml.getInt(monsterName+".DEX"), 0, true));
			sb.append(" ~ ");
			sb.append(BattleCalculator.returnRangeDamageValue(null, monsterYaml.getInt(monsterName+".DEX"), 0, false));
			sb.append("]%enter%");
			sb.append("¡×9¡×l ");
			sb.append(MainServerOption.statINT);
			sb.append(" : ¡×f");
			sb.append(monsterYaml.getInt(monsterName+".INT"));
			sb.append("¡×7 [Æø°ø : ");
			sb.append(monsterYaml.getInt(monsterName+".INT")/4);
			sb.append(" ~ ");
			sb.append((int)(monsterYaml.getInt(monsterName+".INT")/2.5));
			sb.append("]%enter%");
			sb.append("¡×7¡×l ");
			sb.append(MainServerOption.statWILL);
			sb.append(" : ¡×f");
			sb.append(monsterYaml.getInt(monsterName+".WILL"));
			sb.append("¡×7 [Å©¸® : ");
			sb.append(BattleCalculator.getCritical(null,monsterYaml.getInt(monsterName+".LUK"), (int)monsterYaml.getInt(monsterName+".WILL"),0));
			sb.append(" %]%enter%");
			sb.append("¡×e¡×l ");
			sb.append(MainServerOption.statLUK);
			sb.append(" : ¡×f");
			sb.append(monsterYaml.getInt(monsterName+".LUK"));
			sb.append("¡×7 [Å©¸® : ");
			sb.append(BattleCalculator.getCritical(null,monsterYaml.getInt(monsterName+".LUK"), (int)monsterYaml.getInt(monsterName+".WILL"),0));
			sb.append(" %]%enter%");

			sb.append("¡×7¡×l ¹æ¾î : ¡×f");
			sb.append(monsterYaml.getInt(monsterName+".DEF"));
			sb.append("%enter%");
			sb.append("¡×b¡×l º¸È£ : ¡×f");
			sb.append(monsterYaml.getInt(monsterName+".Protect"));
			sb.append("%enter%");
			sb.append("¡×9¡×l ¸¶¹ý ¹æ¾î : ¡×f");
			sb.append(monsterYaml.getInt(monsterName+".Magic_DEF"));
			sb.append("%enter%");
			sb.append("¡×1¡×l ¸¶¹ý º¸È£ : ¡×f");
			sb.append(monsterYaml.getInt(monsterName+".Magic_Protect"));
			sb.append("%enter%");
			sb.append("%enter%¡×e¡×l[ÁÂ Å¬¸¯½Ã ¸ó½ºÅÍ µî·Ï]%enter%");

			String[] scriptA = sb.toString().split("%enter%");
			for(int counter = 0; counter < scriptA.length; counter++)
				scriptA[counter] =  " "+scriptA[counter];
			short id = 383;
			byte data = 0;
			switch(monsterYaml.getString(monsterName+".Type"))
			{
				case "¹ø°³Å©¸®ÆÛ" : case "Å©¸®ÆÛ" : data=50; break;
				case "³×´õ½ºÄÌ·¹Åæ" : case "½ºÄÌ·¹Åæ" : data=51; break;
				case "°Å¹Ì" : data=52; break;
				case "Á»ºñ" :case "ÀÚÀÌ¾ðÆ®" : data=54; break;
				case "ÃÊ´ëÇü½½¶óÀÓ" :case "Æ¯´ë½½¶óÀÓ" : case "Å«½½¶óÀÓ" :case "º¸Åë½½¶óÀÓ" : case "ÀÛÀº½½¶óÀÓ" : data=55; break;
				case "°¡½ºÆ®" : data=56; break;
				case "Á»ºñÇÇ±×¸Ç" : data=57; break;
				case "¿£´õ¸Ç" : data=58; break;
				case "µ¿±¼°Å¹Ì" : data=59; break;
				case "Á»¹ú·¹" : data=60; break;
				case "ºí·¹ÀÌÁî" : data=61; break;
				case "Å«¸¶±×¸¶Å¥ºê" :case "Æ¯´ë¸¶±×¸¶Å¥ºê" : case "º¸Åë¸¶±×¸¶Å¥ºê": case "¸¶±×¸¶Å¥ºê" : case "ÀÛÀº¸¶±×¸¶Å¥ºê" : data=62; break;
				case "¹ÚÁã" : data=65; break;
				case "¸¶³à" : data=66; break;
				case "¿£´õÁøµå±â" : data=67; break;
				case "¼öÈ£ÀÚ" : data=68; break;
				case "µÅÁö" : data=90; break;
				case "¾ç" : data=91; break;
				case "¼Ò" : data=92; break;
				case "´ß" : data=93; break;
				case "¿ÀÂ¡¾î" : data=94; break;
				case "´Á´ë" : data=95; break;
				case "¹ö¼¸¼Ò" : data=96; break;
				case "¿À¼¿·Ô" : data=98; break;
				case "¸»" : data=100; break;
				case "Åä³¢" : data=101; break;
				case "ÁÖ¹Î" : data=120; break;
				case "À§´õ" : id=399; break;
				case "¿£´õµå·¡°ï" : id=122; break;
				case "¿£´õÅ©¸®½ºÅ»" : id=46; break;
			}
			
			stack("¡×f"+monsterName, id, data, 1,Arrays.asList(scriptA), loc, inv);
			loc++;
		}
		
		if(monsterList.length-(page*44)>45)
			stack("¡×f¡×l´ÙÀ½ ÆäÀÌÁö", 323,0,1,Arrays.asList("¡×7´ÙÀ½ ÆäÀÌÁö·Î ÀÌµ¿ ÇÕ´Ï´Ù."), 50, inv);
		if(page!=0)
			stack("¡×f¡×lÀÌÀü ÆäÀÌÁö", 323,0,1,Arrays.asList("¡×7ÀÌÀü ÆäÀÌÁö·Î ÀÌµ¿ ÇÕ´Ï´Ù."), 48, inv);

		stack("¡×f¡×lÃë¼Ò", 166,0,1,Arrays.asList("¡×7ÁöÁ¤ ¸ó½ºÅÍ ½ºÆù´ë½Å","¡×7¿µ¿ª¿¡ µî·Ï µÈ ¸ó½ºÅÍ¸¦","¡×7·£´ýÇÏ°Ô ½ºÆù ÇÕ´Ï´Ù.","¡×0"+areaName,"¡×0"+ruleCount), 49, inv);
		player.openInventory(inv);
	}


	public void areaSpawnSpecialMonsterListGuiClick(InventoryClickEvent event)
	{
		Player player = (Player) event.getWhoClicked();
		int slot = event.getSlot();

		String areaName = event.getInventory().getItem(49).getItemMeta().getLore().get(3).substring(2);
		String ruleCounter = ChatColor.stripColor(event.getInventory().getItem(49).getItemMeta().getLore().get(4));
		int page =  Integer.parseInt(event.getInventory().getTitle().split(" : ")[1])-1;
		if(slot == 49)//³ª°¡±â
		{
			SoundEffect.playSound(player, Sound.BLOCK_PISTON_CONTRACT, 0.8F, 1.8F);
			new AreaMonsterSpawnSettingGui().areaMonsterSpawnSettingGui(player, 0, areaName);
			new area.AreaAPI().areaMonsterSpawnAdd(areaName, ruleCounter);
		}
		else if(slot == 48)//ÀÌÀü ÆäÀÌÁö
			areaSpawnSpecialMonsterListGui(player, page-1, areaName, ruleCounter);
		else if(slot == 50)//´ÙÀ½ ÆäÀÌÁö
			areaSpawnSpecialMonsterListGui(player, page+1, areaName, ruleCounter);
		else
		{
			String mobName = ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName());
			SoundEffect.playSound(player, Sound.BLOCK_ANVIL_LAND, 0.8F, 1.0F);
		  	YamlLoader areaYaml = new YamlLoader();
			areaYaml.getConfig("Area/AreaList.yml");
			areaYaml.set(areaName+".MonsterSpawnRule."+ruleCounter+".Monster", mobName);
			areaYaml.saveConfig();
			new AreaMonsterSpawnSettingGui().areaMonsterSpawnSettingGui(player, 0, areaName);
			
			new area.AreaAPI().areaMonsterSpawnAdd(areaName, ruleCounter);
		}
	}
}
