// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      Magicbane Emulator Project © 2013 - 2022
//                www.magicbane.com


package engine.devcmd.cmds;

import engine.InterestManagement.Terrain;
import engine.devcmd.AbstractDevCmd;
import engine.gameManager.ZoneManager;
import engine.math.Vector2f;
import engine.objects.AbstractGameObject;
import engine.objects.PlayerCharacter;
import engine.objects.Zone;

public class GetHeightCmd extends AbstractDevCmd {

    public GetHeightCmd() {
        super("getHeight");
    }

    @Override
    protected void _doCmd(PlayerCharacter playerCharacter, String[] words,
                          AbstractGameObject target) {

        Zone currentZone;
        Zone parentZone;
        Zone heightmapZone;

        currentZone = ZoneManager.findSmallestZone(playerCharacter.getLoc());
        heightmapZone = Terrain.getNextZoneWithTerrain(currentZone);
        parentZone = Terrain.getNextZoneWithTerrain(currentZone.parent);

        Vector2f childZoneLoc = ZoneManager.worldToTerrainSpace(playerCharacter.getLoc(), heightmapZone);
        Vector2f childZoneOffset = ZoneManager.worldToZoneOffset(playerCharacter.getLoc(), heightmapZone);
        Vector2f normalizedOffset = new Vector2f(Math.abs(childZoneOffset.x) / heightmapZone.major_radius,
                Math.abs(childZoneOffset.y) / heightmapZone.minor_radius);
        Vector2f parentZoneLoc = ZoneManager.worldToTerrainSpace(playerCharacter.getLoc(), parentZone);

        float childHeight = heightmapZone.terrain.getInterpolatedTerrainHeight(childZoneLoc);
        childHeight = childHeight + heightmapZone.global_height;

        float parentHeight = parentZone.terrain.getInterpolatedTerrainHeight(parentZoneLoc);
        parentHeight += parentZone.global_height;

        float blendedHeight = Terrain.getWorldHeight(currentZone, playerCharacter.getLoc());

        Vector2f gridSquare = heightmapZone.terrain.getTerrainCell(childZoneLoc);
        gridSquare.x = (float) Math.floor(gridSquare.x);
        gridSquare.y = (float) Math.floor(gridSquare.y);

        this.throwbackInfo(playerCharacter, "Current Zone : " + currentZone.zoneName);
        this.throwbackInfo(playerCharacter, "Heightmap Zone : " + heightmapZone.zoneName);
        this.throwbackInfo(playerCharacter, "Parent Zone: " + parentZone.zoneName);

        this.throwbackInfo(playerCharacter, "Grid : " + "[" + gridSquare.x + "]" + "[" + gridSquare.y + "]");
        this.throwbackInfo(playerCharacter, "offset: " + "[" + childZoneOffset.x + "]" + "[" + childZoneOffset.y + "]");
        this.throwbackInfo(playerCharacter, "Normalized offset: " + "[" + normalizedOffset.x + "]" + "[" + normalizedOffset.y + "]");
        this.throwbackInfo(playerCharacter, "Blend coefficient: " + heightmapZone.terrain.getTerrainBlendCoefficient(childZoneOffset));

        this.throwbackInfo(playerCharacter, "------------");

        this.throwbackInfo(playerCharacter, "Child Height at loc: " + Math.ceil(childHeight));
        this.throwbackInfo(playerCharacter, "Parent Height at loc: " + Math.ceil(parentHeight));
        this.throwbackInfo(playerCharacter, "Blended Height (Ceil): " + blendedHeight + " (" + Math.ceil(blendedHeight) + ")");

    }

    @Override
    protected String _getHelpString() {
        return "Queries heightmap engine";
    }

    @Override
    protected String _getUsageString() {
        return "' /getheight";
    }

}
