package com.latenighters.runicarcana.common.items.armor;

import com.latenighters.runicarcana.RunicArcana;
import com.latenighters.runicarcana.util.Util;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.item.FireworkRocketEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.network.NetworkDirection;
import net.minecraftforge.fml.network.NetworkEvent;

import javax.annotation.Nullable;
import java.util.List;
import java.util.function.Supplier;

public class PrincipicBootsItem extends AbstractPrincipicArmor {

    private static final ItemStack rocketStack = new ItemStack(Items.FIREWORK_ROCKET, 1);
    private static final int boostCooldown = 30;


    public PrincipicBootsItem() {
        super(EquipmentSlotType.FEET);
    }

    @Override
    @OnlyIn(Dist.CLIENT)
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        if(isEnabled(stack))
            tooltip.add(Util.tooltipStyle("tooltip.runicarcana.principic_boots.effect_enabled"));
        else
            tooltip.add(Util.tooltipStyle("tooltip.runicarcana.principic_boots.effect_disabled"));
        tooltip.add(Util.tooltipStyle("tooltip.runicarcana.principic_boots"));
        tooltip.add(Util.tooltipStyle("tooltip.runicarcana.principic_boots.boost"));
        super.addInformation(stack, worldIn, tooltip, flagIn);
        tooltip.add(Util.loreStyle("lore.runicarcana.principic_boots"));
    }



    public static class StepSyncMessage
    {
        float stepHeight;

        public StepSyncMessage(float stepHeight) {
            this.stepHeight = stepHeight;
        }

        public static void encode(final StepSyncMessage msg, final PacketBuffer buf)
        {
            buf.writeFloat(msg.stepHeight);
        }

        public static StepSyncMessage decode(final PacketBuffer buf)
        {
            return new StepSyncMessage(buf.readFloat());
        }

        public static void handle(final StepSyncMessage msg, final Supplier<NetworkEvent.Context> contextSupplier) {
            final NetworkEvent.Context context = contextSupplier.get();
            if (context.getDirection().equals(NetworkDirection.PLAY_TO_CLIENT)) {
                context.enqueueWork(() -> {

                    RunicArcana.proxy.getPlayer().stepHeight = msg.stepHeight;

                    context.setPacketHandled(true);
                });
            }
        }
    }

    public static void checkTimerNBT(ItemStack stack) {
        CompoundNBT nbt = stack.getTag();
        if(nbt==null)
            nbt = new CompoundNBT();
        if(!nbt.contains("timer"))
            nbt.putInt("timer", 0);
        stack.setTag(nbt);
    }

    public static int getTimer(ItemStack stack){
        checkNBT(stack);
        return stack.getTag().getInt("timer");
    }

    public static void setTimer(ItemStack stack, Integer time){
        checkNBT(stack);
        CompoundNBT nbt = stack.getTag();
        nbt.putInt("timer", time);
        stack.setTag(nbt);
    }

    @Override
    public void onArmorTick(ItemStack stack, World world, PlayerEntity player) {
        // TODO: This breaks when you save the world until you land.
        if (player.isSteppingCarefully() && getTimer(stack) < player.ticksExisted && player.isElytraFlying()) {
            if (!world.isRemote){
                world.addEntity(new FireworkRocketEntity(world, rocketStack, player));
            }
            setTimer(stack, player.ticksExisted + boostCooldown);
        }
        if (!player.isElytraFlying() && player.onGround && getTimer(stack) != 0)
            setTimer(stack, 0);
    }

}

@@ -3055,35 +3055,36 @@
          nbt.put
-Int
+Long
 (%22timer%22, 0);%0A  
@@ -3078,16 +3078,17 @@
 imer%22, 0
+L
 );%0A     
@@ -3134,19 +3134,20 @@
  static 
-int
+long
  getTime
@@ -3224,19 +3224,20 @@
 ag().get
-Int
+Long
 (%22timer%22
@@ -3299,15 +3299,12 @@
 ck, 
-Integer
+Long
  tim
@@ -3393,11 +3393,12 @@
 .put
-Int
+Long
 (%22ti
@@ -3543,77 +3543,8 @@
 ) %7B%0A
-        // TODO: This breaks when you save the world until you land.%0A
     
@@ -3601,35 +3601,35 @@
 tack) %3C 
-player.ticksExisted
+world.getGameTime()
  && play
@@ -3818,27 +3818,27 @@
 ck, 
-player.ticksExisted
+world.getGameTime()
  + b
@@ -3972,16 +3972,17 @@
 stack, 0
+L
 );%0A    %7D

Change Hearthstone cooldown to use world ticks not player ticks.