package me.km.networking;

import java.util.Collection;
import java.util.LinkedList;
import java.util.TreeMap;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.LanguageMap;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class StatusDisplayGui extends AbstractGui
{
    public final static StatusDisplayGui INSTANCE = new StatusDisplayGui(Minecraft.getInstance());

    private class LineData implements Comparable<LineData>
    {
        private int time;
        private final String text;
        
        public LineData(int time, String text)
        {
            this.time = time;
            int index = text.indexOf('#');
            if(index != -1)
            {
                this.text = text.substring(0, index) + LanguageMap.getInstance().translateKey(text.substring(index + 1));
            }
            else
            {
                this.text = text;
            }
        }
        
        public String getText()
        {
            return text;
        }
        
        public String getTimeText()
        {
            if(time == Integer.MAX_VALUE)
            {
                return "";
            }
            return (time / 20) + "s ";
        }
        
        public int getTimeWidth()
        {
            return time == Integer.MAX_VALUE ? 0 : mc.fontRenderer.getStringWidth(getTimeText());
        }
        
        public int getTextWidth()
        {
            return mc.fontRenderer.getStringWidth(getText());
        }

        @Override
        public int compareTo(LineData o) 
        {
            return -Integer.compare(time, o.time);
        }
    }
    
    private final TreeMap<Integer, LineData> strings;
    private final Minecraft mc;
    
    public StatusDisplayGui(Minecraft mc)
    {
        this.mc = mc;
        this.strings = new TreeMap<>(); 
    }
    
    public void tick()
    {
        strings.entrySet().removeIf(e -> 
        {
            if(e.getValue().time < Integer.MAX_VALUE)
            {
                e.getValue().time--;
                if(e.getValue().time < 0)
                {
                    return true;
                }
            }
            return false;
        });
    }
    
    public void add(int i, int time, String s)
    {
        strings.put(i, new LineData(time, s));
    }
    
    public void remove(int i)
    {
        strings.remove(i);
    }
    
    public void clear()
    {
        strings.clear();
    }

    public void paint()
    {
        Collection<EffectInstance> collection = this.mc.player.getActivePotionEffects();
        if(strings.isEmpty() && collection.isEmpty())
        {
            return;
        }
        // getting data
        LinkedList<LineData> list = new LinkedList<>(strings.values());
        EffectInstance e;
        collection.forEach(effect -> 
        {
            StringBuilder sb = new StringBuilder();
            if(effect.getPotion().isBeneficial())
            {
                sb.append(TextFormatting.GREEN);
            }
            else
            {
                sb.append(TextFormatting.RED);
            }
            sb.append(LanguageMap.getInstance().translateKey(effect.getEffectName()));
            list.add(new LineData(effect.getDuration(), sb.toString()));
        });
        // sorting by time
        list.sort(null);
        
        int lineHeight = mc.fontRenderer.FONT_HEIGHT;
        int height = 4 + lineHeight * (strings.size() + collection.size());
        
        int maxTextWidth = 0;
        int maxTimeWidth = 0;
        
        for(LineData data : list)
        {
            maxTextWidth = Math.max(data.getTextWidth(), maxTextWidth);
            maxTimeWidth = Math.max(data.getTimeWidth(), maxTimeWidth);
        }
        
        int screenWidth = mc.getMainWindow().getScaledWidth();
        
        int x = screenWidth - maxTextWidth - maxTimeWidth - 4;
        int y = 2;
        
        fill(x, 0, screenWidth, height, 1342177280);
        
        x += 2;
        String time;

        for(LineData data : list)
        {
            time = data.getTimeText();
            mc.fontRenderer.drawString(time + data.getText(), x + maxTimeWidth - mc.fontRenderer.getStringWidth(time), y + 1, 0xFFFFFF);
            y += lineHeight;
        }
    }
}
