package ot.item;

import li.cil.oc.api.driver.item.Chargeable;
import li.cil.oc.common.block.RobotProxy;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import ot.Config;
import ot.OpenTechnology;

import java.util.List;

/**
 * Created by Avaja on 14.12.2016.
 */
public class ItemScanner extends Item implements Chargeable {


    public ItemScanner() {
        setCreativeTab(OpenTechnology.tab);
        setTextureName(OpenTechnology.MODID + ":scanner");
        setUnlocalizedName(OpenTechnology.MODID + "_scanner");
        setMaxDamage(100);
    }

    @Override
    public void addInformation(ItemStack stack, EntityPlayer entityPlayer, List list, boolean b) {
        list.add(EnumChatFormatting.YELLOW + StatCollector.translateToLocal("lore.scanner.charge") +
                (getEnergy(stack) + "/" + Config.scannerEnergyCount));
    }

    @Override
    public boolean onItemUse(ItemStack stack, EntityPlayer entityPlayer, World world, int x, int y, int z, int side, float dx, float dy, float dz) {
        if(!world.isRemote && getEnergy(stack) >= Config.scannerUsageCost){
            setEnergy(stack, getEnergy(stack) - Config.scannerUsageCost);
            stack.setItemDamage(calcDamage(stack));
        }

        if(getEnergy(stack) < Config.scannerUsageCost)
            return true;

        int minX = x - Config.scannerWidth / 2;
        int minY = y - Config.scannerHeight / 2;
        int minZ = z - Config.scannerWidth / 2;

        int maxX = x + Config.scannerWidth / 2;
        int maxY = y + Config.scannerHeight / 2;
        int maxZ = z + Config.scannerWidth / 2;

        for(int ix = minX; ix < maxX; ix++){
            for(int iz = minZ; iz < maxZ; iz++){
                for(int iy = minY; iy < maxY; iy++){
                    Block block  = world.getBlock(ix, iy, iz);
                    if(block instanceof RobotProxy){
                        entityPlayer.addChatComponentMessage(new ChatComponentText(String.format(StatCollector.translateToLocal("chat.scanner"),
                                ix, iy, iz)));
                    }
                }
            }
        }
        return true;
    }

    private int calcDamage(ItemStack stack){
        float per = getEnergy(stack) / (Config.scannerEnergyCount / 100f);
        return stack.getMaxDamage() - (int)per;
    }

    private int getEnergy(ItemStack stack){
        if(stack.hasTagCompound() && stack.getTagCompound().hasKey("energy")){
            return stack.getTagCompound().getInteger("energy");
        }else{
            NBTTagCompound nbt = new NBTTagCompound();
            stack.setTagCompound(nbt);
            nbt.setInteger("energy", 0);
        }
        return 0;
    }

    private void setEnergy(ItemStack stack, int energyCount){
        if(stack.hasTagCompound() && stack.getTagCompound().hasKey("energy")){
            stack.getTagCompound().setInteger("energy", energyCount);
        }else{
            NBTTagCompound nbt = new NBTTagCompound();
            nbt.setInteger("energy", energyCount);
            stack.setTagCompound(nbt);
        }
    }

    @Override
    public void setDamage(ItemStack stack, int damage) {
        super.setDamage(stack, damage);
    }

    @Override
    public boolean canCharge(ItemStack stack) {
        return true;
    }

    @Override
    public double charge(ItemStack stack, double amount, boolean simulate) {
        int energy = Config.scannerEnergyCount - getEnergy(stack);

        if(energy >= amount){
            setEnergy(stack, (int) (getEnergy(stack) + amount));
            stack.setItemDamage(calcDamage(stack));
            return 0;
        }else{
            setEnergy(stack, Config.scannerEnergyCount);
            stack.setItemDamage(calcDamage(stack));
            return amount - energy;
        }
    }


}

@@ -128,24 +128,71 @@
 lock.Block;%0A
+import net.minecraft.creativetab.CreativeTabs;%0A
 import net.m
@@ -896,24 +896,54 @@
 amage(100);%0A
+        setHasSubtypes(true);%0A
     %7D%0A%0A    @
@@ -1221,32 +1221,130 @@
 Count));%0A    %7D%0A%0A
+    @Override%0A    public boolean showDurabilityBar(ItemStack stack) %7B%0A        return true;%0A    %7D%0A%0A
     @Override%0A  
@@ -1656,46 +1656,19 @@
     
-stack.setItemDamage(calcDamage(stack))
+return true
 ;%0A  
@@ -2604,30 +2604,59 @@
     
-private int calcDamage
+@Override%0A    public double getDurabilityForDisplay
 (Ite
@@ -2660,32 +2660,33 @@
 ItemStack stack)
+ 
 %7B%0A        float 
@@ -2683,19 +2683,43 @@
     
-float per =
+return (Config.scannerEnergyCount -
  get
@@ -2735,12 +2735,20 @@
 ack)
+)
  / (
+double)
 Conf
@@ -2772,64 +2772,8 @@
 ount
- / 100f);%0A        return stack.getMaxDamage() - (int)per
 ;%0A  
@@ -2783,21 +2783,23 @@
 %0A%0A    pr
-ivate
+otected
  int get
@@ -3161,13 +3161,15 @@
   pr
-ivate
+otected
  voi
@@ -3533,32 +3533,322 @@
        %7D%0A    %7D%0A%0A
+    @Override%0A    public void getSubItems(Item item, CreativeTabs creativeTabs, List list) %7B%0A        super.getSubItems(item, creativeTabs, list);%0A        ItemStack charged = new ItemStack(this, 1, 0);%0A        setEnergy(charged, Config.scannerEnergyCount);%0A        list.add(charged);%0A    %7D%0A%0A
     @Override%0A  
@@ -4197,17 +4197,16 @@
 stack);%0A
-%0A
         
@@ -4296,60 +4296,8 @@
 ));%0A
-            stack.setItemDamage(calcDamage(stack));%0A
     
@@ -4390,60 +4390,8 @@
 t);%0A
-            stack.setItemDamage(calcDamage(stack));%0A
     

add to creative tab charged scanner