package totemic_commons.pokefenn.tileentity.totem;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import totemic_commons.pokefenn.api.music.MusicInstrument;
import totemic_commons.pokefenn.network.NetworkHandler;
import totemic_commons.pokefenn.network.client.PacketTotemEffectMusic;

public class StateTotemEffect extends TotemState
{
    public static final int ID = 0;
    public static final int MAX_EFFECT_MUSIC = 128;

    private int musicAmount = 0;
    private boolean musicAdded = false;

    public StateTotemEffect(TileTotemBase tile)
    {
        super(tile);
    }

    @Override
    public void update()
    {
        tile.getTotemEffectSet().entrySet().forEach(e -> {
            int horizontal = e.getElement().getHorizontalRange(); //TODO
            int vertical = e.getElement().getVerticalRange();
            e.getElement().effect(tile.getWorld(), tile.getPos(), tile, e.getCount(), horizontal, vertical);
        });

        //Diminish melody over time
        if(musicAmount > 0 && tile.getWorld().getTotalWorldTime() % 47 == 0)
        {
            musicAmount--;
            tile.markDirty();
        }

        if(musicAdded && !tile.getWorld().isRemote && tile.getWorld().getTotalWorldTime() % 20 == 0)
        {
            NetworkHandler.sendAround(new PacketTotemEffectMusic(tile.getPos(), musicAmount), tile, 32);
            musicAdded = false;
        }

        if(tile.getWorld().isRemote && tile.getWorld().getTotalWorldTime() % 40 == 0)
            spawnParticles();
    }

    @SideOnly(Side.CLIENT)
    private void spawnParticles()
    {
        for(int i = 0; i < musicAmount / 16; i++)
        {
            float xoff = 2 * tile.getWorld().rand.nextFloat() - 1;
            float zoff = 2 * tile.getWorld().rand.nextFloat() - 1;
            BlockPos pos = tile.getPos();
            tile.getWorld().spawnParticle(EnumParticleTypes.NOTE, pos.getX() + 0.5 + xoff, pos.getY(), pos.getZ() + 0.5 + zoff, 0, 0.5, 0);
        }
    }

    @Override
    public boolean addMusic(MusicInstrument instr, int amount)
    {
        int previous = musicAmount;
        musicAmount = Math.min(previous + amount / 2, MAX_EFFECT_MUSIC);
        if(musicAmount > previous)
        {
            musicAdded = true;
            return true;
        }
        else
            return false;
    }

    @Override
    public boolean canSelect()
    {
        return true;
    }

    @Override
    public void addSelector(MusicInstrument instr)
    {
        tile.setState(new StateSelection(tile, instr));
    }

    public int getMusicAmount()
    {
        return musicAmount;
    }

    public void setMusicAmount(int amount)
    {
        this.musicAmount = amount;
    }

    @Override
    public int getID()
    {
        return ID;
    }

    @Override
    public void writeToNBT(NBTTagCompound tag)
    {
        tag.setInteger("musicForTotemEffect", musicAmount);
    }

    @Override
    public void readFromNBT(NBTTagCompound tag)
    {
        musicAmount = tag.getInteger("musicForTotemEffect");
    }
}

@@ -790,32 +790,75 @@
  update()%0A    %7B%0A
+        int rangeBonus = getRangeBonus();%0A%0A
         tile.get
@@ -968,16 +968,22 @@
 ge()
-; //TODO
+ + rangeBonus;
 %0A   
@@ -1039,16 +1039,29 @@
 lRange()
+ + rangeBonus
 ;%0A      
@@ -1211,16 +1211,50 @@
 ver time
+, about 5 minutes to fully deplete
 %0A       
@@ -1738,32 +1738,32 @@
 me() %25 40 == 0)%0A
-
             spaw
@@ -1779,24 +1779,513 @@
 s();%0A    %7D%0A%0A
+    private int getRangeBonus()%0A    %7B%0A        int bonus = 0;%0A%0A        if(musicAmount %3E= 10)%0A            bonus += 1;%0A        else if(musicAmount %3E= 32)%0A            bonus += 2;%0A        else if(musicAmount %3E= 64)%0A            bonus += 3;%0A        else if(musicAmount %3E= 96)%0A            bonus += 4;%0A        else if(musicAmount %3E= 115)%0A            bonus += 5;%0A%0A        //bonus += tile.getWoodBonus() / 3;%0A%0A        if(tile.getPoleSize() %3E= 5)%0A            bonus += 2;%0A%0A        return bonus;%0A    %7D%0A%0A
     @SideOnl

Implement totem effect range bonus