package com.teamwizardry.refraction.common.tile;

import com.google.common.collect.Lists;
import com.teamwizardry.librarianlib.common.base.block.TileMod;
import com.teamwizardry.librarianlib.common.util.autoregister.TileRegister;
import com.teamwizardry.librarianlib.common.util.saving.Save;
import com.teamwizardry.refraction.api.Utils;
import com.teamwizardry.refraction.api.beam.Beam;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ITickable;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Saad on 9/11/2016.
 */
@TileRegister("spectrometer")
public class TileSpectrometer extends TileMod implements ITickable {

    @Save
    public Color maxColor = new Color(0, 0, 0, 0);
    public Color currentColor = new Color(0, 0, 0, 0);
    @NotNull
    private List<Beam> beams = new ArrayList<>();
    @Save
    private int beamHandledTicks = 5;

    @Override
    public void readCustomNBT(NBTTagCompound compound) {
        this.currentColor = new Color(compound.getInteger("current_color"), true);
    }

    @Override
    public void writeCustomNBT(NBTTagCompound compound, boolean sync) {
        compound.setInteger("current_color", currentColor.getRGB());
    }

    public void handle(Beam... beams) {
        beamHandledTicks = 5;
        markDirty();
        this.beams.addAll(Lists.newArrayList(beams));
    }

    @Override
    public void update() {
        if (worldObj.isRemote) return;

        if (beamHandledTicks > 0)
            beamHandledTicks--;
        else {
            beams.clear();
            maxColor = new Color(0, 0, 0, 0);
            worldObj.notifyBlockUpdate(pos, worldObj.getBlockState(pos), worldObj.getBlockState(pos), 3);
            markDirty();
        }

        if (currentColor.getRGB() != maxColor.getRGB()) {
            currentColor = Utils.mixColors(currentColor, maxColor, 0.9);
            worldObj.notifyBlockUpdate(pos, worldObj.getBlockState(pos), worldObj.getBlockState(pos), 3);
        }

        if (beams.isEmpty()) return;

        int red = 0;
        int green = 0;
        int blue = 0;
        int alpha = 0;

        for (Beam beam : beams) {
            Color color = beam.color;
            red += color.getRed() * (color.getAlpha() / 255f);
            green += color.getGreen() * (color.getAlpha() / 255f);
            blue += color.getBlue() * (color.getAlpha() / 255f);
            alpha += color.getAlpha();
        }
        red = Math.min(red / beams.size(), 255);
        green = Math.min(green / beams.size(), 255);
        blue = Math.min(blue / beams.size(), 255);

        float[] hsbvals = Color.RGBtoHSB(red, green, blue, null);
        Color color = new Color(Color.HSBtoRGB(hsbvals[0], hsbvals[1], 1));
        color = new Color(color.getRed(), color.getGreen(), color.getBlue(), Math.min(alpha, 255));

        if (color.getRGB() == maxColor.getRGB()) return;
        this.maxColor = color;
        markDirty();
    }
}

@@ -896,56 +896,8 @@
 %3E();
-%0A    @Save%0A    private int beamHandledTicks = 5;
 %0A%0A  
@@ -1261,38 +1261,8 @@
 ) %7B%0A
-        beamHandledTicks = 5;%0A
     
@@ -1436,147 +1436,124 @@
 if (
-beamHandledTicks %3E 0)%0A            beamHandledTicks--;%0A        else %7B%0A            beams.clear();%0A            maxColor = new Color(0, 0, 0
+currentColor.getRGB() != maxColor.getRGB()) %7B%0A            currentColor = Utils.mixColors(currentColor, maxColor
 , 0
+.9
 );%0A 
@@ -1669,88 +1669,36 @@
     
-    markDirty();%0A        %7D%0A%0A        if (currentColor.getRGB() != maxColor.getRGB
+%7D%0A%0A        if (beams.isEmpty
 ()) 
@@ -1707,39 +1707,35 @@
             
-current
+max
 Color = 
 Utils.mixCol
@@ -1726,51 +1726,28 @@
 r = 
-Utils.mixColors(currentColor, maxColor
+new Color(0, 0, 0
 , 0
-.9
 );%0A 
@@ -1863,47 +1863,54 @@
     
-%7D%0A%0A        if (beams.isEmpty()) return;
+    markDirty();%0A            return;%0A        %7D
 %0A%0A  
@@ -2793,24 +2793,48 @@
 lor = color;
+%0A%0A        beams.clear();
 %0A        mar

revert saad's "fix"