package me.rigamortis.seppuku.impl.gui.hud.component;

import com.mojang.realmsclient.gui.ChatFormatting;
import me.rigamortis.seppuku.api.gui.hud.component.DraggableHudComponent;
import me.rigamortis.seppuku.api.util.Timer;
import net.minecraft.util.math.MathHelper;

/**
 * @author Seth
 * @author noil
 */
public final class DirectionComponent extends DraggableHudComponent {

    private final Timer directionTimer = new Timer();
    private String direction = "";

    public DirectionComponent() {
        super("Direction");
        this.setH(mc.fontRenderer.FONT_HEIGHT);
    }

    @Override
    public void render(int mouseX, int mouseY, float partialTicks) {
        super.render(mouseX, mouseY, partialTicks);

        if (mc.world != null) {
            if (this.directionTimer.passed(250)) { // 250ms
                direction = String.format(ChatFormatting.GRAY + "%s" + " " + ChatFormatting.RESET + "%s", this.getFacing(), this.getTowards());
                this.directionTimer.reset();
            }

            this.setW(mc.fontRenderer.getStringWidth(direction));
            mc.fontRenderer.drawStringWithShadow(direction, this.getX(), this.getY(), -1);
        } else {
            this.setW(mc.fontRenderer.getStringWidth("(direction)"));
            mc.fontRenderer.drawStringWithShadow("(direction)", this.getX(), this.getY(), 0xFFAAAAAA);
        }
    }

    private String getFacing() {
        switch (MathHelper.floor((double) (mc.player.rotationYaw * 8.0F / 360.0F) + 0.5D) & 7) {
            case 0:
                return "South";
            case 1:
                return "South West";
            case 2:
                return "West";
            case 3:
                return "North West";
            case 4:
                return "North";
            case 5:
                return "North East";
            case 6:
                return "East";
            case 7:
                return "South East";
        }
        return "Invalid";
    }

    private String getTowards() {
        switch (MathHelper.floor((double) (mc.player.rotationYaw * 8.0F / 360.0F) + 0.5D) & 7) {
            case 0:
                return "+Z";
            case 1:
                return "-X +Z";
            case 2:
                return "-X";
            case 3:
                return "-X -Z";
            case 4:
                return "-Z";
            case 5:
                return "+X -Z";
            case 6:
                return "+X";
            case 7:
                return "+X +Z";
        }
        return "Invalid";
    }
}