/*
 * Caveworld
 *
 * Copyright (c) 2014 kegare
 * https://github.com/kegare
 *
 * This mod is distributed under the terms of the Minecraft Mod Public License 1.0, or MMPL.
 * Please check the contents of the license located in http://www.mod-buildcraft.com/MMPL-1.0.txt
 */

package com.kegare.caveworld.core;

import java.util.Set;
import java.util.logging.Level;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.Achievement;
import net.minecraftforge.common.AchievementPage;

import com.google.common.base.Optional;
import com.google.common.collect.Sets;
import com.kegare.caveworld.block.CaveBlock;
import com.kegare.caveworld.util.CaveLog;

public class CaveAchievementList
{
	private static final Set<Achievement> CAVE_ACHIEVEMENTS = Sets.newHashSet();

	public static Optional<Achievement> caveworld = Optional.fromNullable(null);
	public static Optional<Achievement> miner = Optional.fromNullable(null);

	static void initialize()
	{
		CAVE_ACHIEVEMENTS.clear();

		if (Config.achievementInitIndex == 0)
		{
			return;
		}

		try
		{
			caveworld = Optional.of(new CaveAchievement(0, "caveworld", 0, 0, CaveBlock.portalCaveworld, null).setIndependent().registerAchievement());
			miner = Optional.of(new CaveAchievement(1, "miner", 2, 1, Item.pickaxeIron, caveworld.get()).registerAchievement());
		}
		catch (Exception e)
		{
			CAVE_ACHIEVEMENTS.clear();

			CaveLog.log(Level.SEVERE, e, "An error occurred trying to initialize cave achievements");
		}
	}

	static void register()
	{
		if (!CAVE_ACHIEVEMENTS.isEmpty())
		{
			AchievementPage.registerAchievementPage(new AchievementPage("Caveworld", CAVE_ACHIEVEMENTS.toArray(new Achievement[CAVE_ACHIEVEMENTS.size()])));
		}
	}

	private static class CaveAchievement extends Achievement
	{
		public CaveAchievement(int id, String name, int column, int row, Block block, Achievement achievement)
		{
			this(id, name, column, row, new ItemStack(block), achievement);
		}

		public CaveAchievement(int id, String name, int column, int row, Item item, Achievement achievement)
		{
			this(id, name, column, row, new ItemStack(item), achievement);
		}

		public CaveAchievement(int id, String name, int column, int row, ItemStack itemstack, Achievement achievement)
		{
			super(Config.achievementInitIndex + id, "caveworld." + name, column, row, itemstack, achievement);
		}

		@Override
		public Achievement registerAchievement()
		{
			super.registerAchievement();
			CAVE_ACHIEVEMENTS.add(this);

			return this;
		}
	}
}