// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.builtin.buildertools.tooloperations.transform;

import com.hypixel.hytale.protocol.packets.buildertools.BrushAxis;
import com.hypixel.hytale.protocol.Rotation;
import javax.annotation.Nonnull;
import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.math.Axis;

public class Rotate implements Transform
{
    public static final Transform X_90;
    public static final Transform X_180;
    public static final Transform X_270;
    public static final Transform Y_90;
    public static final Transform Y_180;
    public static final Transform Y_270;
    public static final Transform Z_90;
    public static final Transform Z_180;
    public static final Transform Z_270;
    public static final Transform FACING_NORTH;
    public static final Transform FACING_EAST;
    public static final Transform FACING_SOUTH;
    public static final Transform FACING_WEST;
    private final Axis axis;
    private final int rotations;
    
    public Rotate(final Axis axis) {
        this(axis, 90);
    }
    
    public Rotate(final Axis axis, int angle) {
        angle = Math.floorMod(angle, 360);
        final int rotations = angle / 90;
        this.axis = axis;
        this.rotations = rotations;
    }
    
    @Override
    public void apply(@Nonnull final Vector3i vector3i) {
        if (this.rotations == 1) {
            this.axis.rotate(vector3i);
        }
        else if (this.rotations > 1) {
            for (int i = 0; i < this.rotations; ++i) {
                this.axis.rotate(vector3i);
            }
        }
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "Rotate{axis=" + String.valueOf(this.axis) + ", rotations=" + this.rotations;
    }
    
    public static Transform forDirection(@Nonnull final Vector3i direction, final Rotation angle) {
        if (direction.getX() < 0) {
            return selectRotation(angle, Rotate.FACING_WEST, Rotate.FACING_NORTH, Rotate.FACING_EAST, Rotate.FACING_SOUTH);
        }
        if (direction.getX() > 0) {
            return selectRotation(angle, Rotate.FACING_EAST, Rotate.FACING_SOUTH, Rotate.FACING_WEST, Rotate.FACING_NORTH);
        }
        if (direction.getZ() < 0) {
            return selectRotation(angle, Rotate.FACING_NORTH, Rotate.FACING_EAST, Rotate.FACING_SOUTH, Rotate.FACING_WEST);
        }
        if (direction.getZ() > 0) {
            return selectRotation(angle, Rotate.FACING_SOUTH, Rotate.FACING_WEST, Rotate.FACING_NORTH, Rotate.FACING_EAST);
        }
        return Rotate.NONE;
    }
    
    public static Transform forAxisAndAngle(final BrushAxis axis, final Rotation angle) {
        if (axis == BrushAxis.X) {
            return selectRotation(angle, Rotate.NONE, Rotate.X_90, Rotate.X_180, Rotate.X_270);
        }
        if (axis == BrushAxis.Y) {
            return selectRotation(angle, Rotate.NONE, Rotate.Y_90, Rotate.Y_180, Rotate.Y_270);
        }
        if (axis == BrushAxis.Z) {
            return selectRotation(angle, Rotate.NONE, Rotate.Z_90, Rotate.Z_180, Rotate.Z_270);
        }
        return Rotate.NONE;
    }
    
    private static Transform selectRotation(final Rotation angle, final Transform rotate0, final Transform rotate90, final Transform rotate180, final Transform rotate270) {
        if (angle == Rotation.Ninety) {
            return rotate90;
        }
        if (angle == Rotation.OneEighty) {
            return rotate180;
        }
        if (angle == Rotation.TwoSeventy) {
            return rotate270;
        }
        return rotate0;
    }
    
    static {
        X_90 = new Rotate(Axis.X, 90);
        X_180 = new Rotate(Axis.X, 180);
        X_270 = new Rotate(Axis.X, 270);
        Y_90 = new Rotate(Axis.Y, 90);
        Y_180 = new Rotate(Axis.Y, 180);
        Y_270 = new Rotate(Axis.Y, 270);
        Z_90 = new Rotate(Axis.Z, 90);
        Z_180 = new Rotate(Axis.Z, 180);
        Z_270 = new Rotate(Axis.Z, 270);
        FACING_NORTH = Rotate.X_90;
        FACING_EAST = Rotate.Z_90;
        FACING_SOUTH = Rotate.X_90.then(Rotate.Y_180);
        FACING_WEST = Rotate.Z_90.then(Rotate.Y_180);
    }
}
