package Shape;

import Shape.Obstacle;
import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class ObstacleField {
//    public Obstacle(){
//        WindowSize = 500;
//        xTranslation = 605;
//        yTranslation = 205;
//        zTranslation = 5;
//
//        double x, y, z;
//        String posKey;
//        for(int i = 0; i<100; i++){
//            Random rand = new Random();
//            do {
//                x = rand.nextDouble() * 350 + 100; // Random position for x
//                y = rand.nextDouble() * 350 + 100; // Random position for y
//                z = rand.nextDouble() * 350 + 100; // Random position for z
//                posKey = x + "," + y + "," + z;   // Create a key for the set
//            } while (occupiedPositions.contains(posKey)); // Ensure uniqueness
//
//            occupiedPositions.add(posKey); // Add the new position to the set
//
//            Box cube = new Box(obSize,obSize,obSize);
//            cube.setMaterial(new PhongMaterial(Color.ORANGERED));
//            cube.setTranslateX(xTranslation + (x));
//            cube.setTranslateY(yTranslation + (y));
//            cube.setTranslateZ(zTranslation + (z));
//            obstacles.add(cube);
//            obstacleBounds.add(cube.getBoundsInParent());
//        }
//    }

    public ObstacleField(int windowSize, int xTranslation, int yTranslation,
                         int range, int min, int obSize, int objNum,
                         String shape, Color color){
        this.obSize = obSize/2;
        this.windowSize = windowSize;
        this.xTranslation = xTranslation + obSize/2;
        this.yTranslation = yTranslation + obSize/2;
        zTranslation = obSize/2;

        double x, y, z;
        String posKey;
        for(int i = 0; i<objNum; i++){
            Random rand = new Random();
            do {
                x = rand.nextDouble() * range + min; // Random position for x
                y = rand.nextDouble() * range + min; // Random position for y
                z = rand.nextDouble() * range + min; // Random position for z
                posKey = x + "," + y + "," + z;   // Create a key for the set
            } while (occupiedPositions.contains(posKey)); // Ensure uniqueness

            occupiedPositions.add(posKey); // Add the new position to the set

            Obstacle ob = new Obstacle(this.windowSize, this.xTranslation + x, this.yTranslation +y,
                    this.zTranslation + z, this.obSize, shape, color);
            obstacles.add(ob);
            obstacleBounds.add(ob.getShape().getBoundsInParent());
        }
    }


    public void addToGroup(Group group){
        for(Obstacle o: obstacles){
            group.getChildren().add(o.getShape());
        }
    }
    public ArrayList<Obstacle> getObstacles() {
        return obstacles;
    }

    public ArrayList<Bounds> getObstacleBounds() {
        return obstacleBounds;
    }

    public boolean checkCollision(Bounds otherBounds) {
        for (Bounds bounds : obstacleBounds) {
            if (bounds.intersects(otherBounds)) {
                return true;
            }
        }
        return false;
    }

    public void addToField(Obstacle o){
        obstacles.add(o);
        obstacleBounds.add(o.getShape().getBoundsInParent());
    }

    private int windowSize, obSize;
    private int xTranslation,yTranslation,zTranslation;
    Set<String> occupiedPositions = new HashSet<>();
    ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
    ArrayList<Bounds> obstacleBounds = new ArrayList<Bounds>();
}
