package com.zivilon.cinder_loe.mixins;

import com.zivilon.cinder_loe.potion.*;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.potion.*;
import net.minecraft.util.*;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(FoodStats.class)
public class MixinFoodStats {
    @Shadow
    private int foodLevel = 20;
    @Shadow
    private float foodSaturationLevel = 5.0F;
    @Shadow
    private float foodExhaustionLevel;
    @Shadow
    private int foodTimer;
    @Shadow
    private int prevFoodLevel = 20;
    @Shadow
    public void addExhaustion(float p_71020_1_) {};


    @Overwrite
    public void onUpdate(EntityPlayer player) {
        EnumDifficulty enumdifficulty = player.worldObj.difficultySetting;
        this.prevFoodLevel = this.foodLevel;

        if (this.foodExhaustionLevel > 4.0F) {
            this.foodExhaustionLevel -= 4.0F;

            if (this.foodSaturationLevel > 0.0F) {
                this.foodSaturationLevel = Math.max(this.foodSaturationLevel - 1.0F, 0.0F);
            } else if (enumdifficulty != EnumDifficulty.PEACEFUL) {
                this.foodLevel = Math.max(this.foodLevel - 1, 0);
            }
        }

        if (player.worldObj.getGameRules().getGameRuleBooleanValue("naturalRegeneration") && this.foodLevel >= 18 && player.shouldHeal()) {
            ++this.foodTimer;

            if (this.foodTimer >= 80) {
                PotionEffect potion = player.getActivePotionEffect(LoEPotions.overdose);
                if (potion == null || potion.getAmplifier() < 1)
                    player.heal(1.0F);
                this.addExhaustion(3.0F);
                this.foodTimer = 0;
            }
        } else if (this.foodLevel <= 0) {
            ++this.foodTimer;

            if (this.foodTimer >= 80) {
                if (player.getHealth() > 10.0F || enumdifficulty == EnumDifficulty.HARD || player.getHealth() > 1.0F && enumdifficulty == EnumDifficulty.NORMAL) {
                    player.attackEntityFrom(DamageSource.starve, 1.0F);
                }

                this.foodTimer = 0;
            }
        } else {
            this.foodTimer = 0;
        }
    }
}
