package com.zivilon.cinder_loe.mixins;

import lotr.common.LOTRAchievement;
import lotr.common.LOTRLevelData;
import lotr.common.block.LOTRBlockSaplingBase;
import lotr.common.entity.npc.LOTREntityDarkHuorn;
import lotr.common.entity.npc.LOTREntityHuorn;
import lotr.common.entity.npc.LOTREntityTree;
import lotr.common.entity.npc.LOTRHiredNPCInfo;
import lotr.common.fac.LOTRAlignmentValues;
import lotr.common.fac.LOTRFaction;
import lotr.common.item.LOTRItemEntDraught;
import lotr.common.item.LOTRItemPotion;
import lotr.common.item.LOTRPoisonedDrinks;
import net.minecraft.block.Block;
import net.minecraft.block.BlockSapling;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.init.Items;

import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.*;
import net.minecraft.world.World;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;

import java.util.List;
import java.util.Random;

@Mixin(LOTRItemEntDraught.class)
public abstract class MixinLOTRItemEntDraught {


    private LOTRItemEntDraught.DraughtInfo getDraughtInfo(ItemStack itemstack) {
        int i = itemstack.getItemDamage();
        if (i >= LOTRItemEntDraught.draughtTypes.length) {
            i = 0;
        }
        return LOTRItemEntDraught.draughtTypes[i];
    }


    /**
    * @author KeyLime17
    * @reason ???
    */
    @Overwrite(remap = false)
    public ItemStack func_77654_b(ItemStack itemstack, World world, EntityPlayer entityplayer) {

        final Random rand = new Random();
        int chance = rand.nextInt(100);

        if (chance < 20) {
            if (!world.isRemote) {
                entityplayer.addPotionEffect(new PotionEffect(Potion.poison.id, 400));
                ChatComponentText message = new ChatComponentText("As you drink, you taste the bitter tears of Yavanna's Shepherds...");
                ChatStyle style = new ChatStyle();
                style.setColor(EnumChatFormatting.RED); // Set color to red
                message.setChatStyle(style);
                entityplayer.addChatMessage(message);
            }
        } else {
            if (entityplayer.canEat(false)) {
                entityplayer.getFoodStats().addStats(this.getDraughtInfo((ItemStack)itemstack).heal, this.getDraughtInfo((ItemStack)itemstack).saturation);
            }
            if (!world.isRemote) {
                List effects = this.getDraughtInfo((ItemStack)itemstack).effects;
                for (int i = 0; i < effects.size(); ++i) {
                    PotionEffect effect = (PotionEffect)effects.get(i);
                    entityplayer.addPotionEffect(new PotionEffect(effect.getPotionID(), effect.getDuration()));
                }
            }
            if (!world.isRemote && entityplayer.getCurrentEquippedItem() == itemstack) {
                LOTRLevelData.getData(entityplayer).addAchievement(LOTRAchievement.drinkEntDraught);
            }
        }
        return !entityplayer.capabilities.isCreativeMode ? new ItemStack(Items.bowl) : itemstack;
    }

    /**
    * @author KeyLime17
    * @reason ???
    */
    @Overwrite(remap = false)
    public boolean func_77648_a(ItemStack itemstack, EntityPlayer entityplayer, World world, int i, int j, int k, int side, float f, float f1, float f2) {
        if (this.getDraughtInfo((ItemStack)itemstack).name.equals("gold")) {
            if (LOTRLevelData.getData(entityplayer).getAlignment(LOTRFaction.FANGORN) < 500.0f) {
                if (!world.isRemote) {
                    LOTRAlignmentValues.notifyAlignmentNotHighEnough(entityplayer, 500.0f, LOTRFaction.FANGORN);
                }
                return false;
            }
            Block block = world.getBlock(i, j, k);
            int meta = world.getBlockMetadata(i, j, k);
            if (block instanceof BlockSapling || block instanceof LOTRBlockSaplingBase) {
                meta &= 7;
                for (int huornType = 0; huornType < LOTREntityTree.TYPES.length; ++huornType) {
                    if (block != LOTREntityTree.SAPLING_BLOCKS[huornType] || meta != LOTREntityTree.SAPLING_META[huornType]) continue;
                    LOTREntityDarkHuorn darkhuorn = new LOTREntityDarkHuorn(world);
                    darkhuorn.setTreeType(huornType);
                    darkhuorn.isNPCPersistent = true;
                    darkhuorn.liftSpawnRestrictions = true;
                    darkhuorn.setLocationAndAngles((double)i + 0.5, j, (double)k + 0.5, 0.0f, 0.0f);
                    if (!darkhuorn.getCanSpawnHere()) continue;
                    if (!world.isRemote) {
                        world.spawnEntityInWorld((Entity)darkhuorn);
                        ChatComponentText message = new ChatComponentText("You have raised a Huorn.. However it cries for the deaths of its kin");
                        ChatStyle style = new ChatStyle();
                        style.setColor(EnumChatFormatting.RED); // Set color to red
                        message.setChatStyle(style);
                        entityplayer.addChatMessage(message);
                        LOTRLevelData.getData(entityplayer).addAchievement(LOTRAchievement.summonHuorn);
                    }
                    for (int l = 0; l < 24; ++l) {
                        double d = (double)i + 0.5 - world.rand.nextDouble() * 2.0 + world.rand.nextDouble() * 2.0;
                        double d1 = (double)j + world.rand.nextDouble() * 4.0;
                        double d2 = (double)k + 0.5 - world.rand.nextDouble() * 2.0 + world.rand.nextDouble() * 2.0;
                        world.spawnParticle("angryVillager", d, d1, d2, 0.0, 0.0, 0.0);
                    }
                    if (!entityplayer.capabilities.isCreativeMode) {
                        entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, new ItemStack(Items.bowl));
                    }
                    return true;
                }
            }
        }
        return false;
    }
}
