package net.ddns.mahaljsp.brick;
import javafx.scene.media.AudioClip;
import javafx.application.Platform;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;

public class Ball extends Circle implements Runnable{
    double xspeed=0.8;
    double yspeed=0.8;
    double radius=10;
    double x=000, y=600-radius;
    boolean isRunning=true;
    
    public Ball(){
        setRadius(10);
        setFill(
            /*
            new RadialGradient(
                0,
                0,
                0.3,
                0.3,
                1,
                true,
                CycleMethod.NO_CYCLE,

                new Stop(0, Color.WHITE),
                new Stop(0.4, Color.YELLOW),
                new Stop(1, Color.ORANGE)
            )            
            */
            new RadialGradient(
                0,
                0,
                0.3,
                0.3,
                1,
                true,
                CycleMethod.NO_CYCLE,

                new Stop(0, Color.WHITE),
                new Stop(0.3, Color.LIGHTBLUE),
                new Stop(1, Color.DARKBLUE)
            )
            
        );
        //setEffect(new DropShadow(
        //    20,
        //    Color.YELLOW
        //));        
        new Thread(this).start();
    }
    public void close(){
        isRunning=false;
    }
    double clamp(double value, double min, double max) {
        return Math.max(min, Math.min(value, max));
    }
    public void run() {
        while (isRunning){
            try {
                x+=xspeed;
                y+=yspeed;
                if (x>800-radius){
                    xspeed*=-1;
                    x=800-radius;
                }
                if (x<radius){
                    xspeed*=-1;
                    x=radius;
                }
                if (y>600-radius){
                    yspeed*=-1;
                    y=600-radius;
                }
                if (y<radius){
                    yspeed*=-1;
                    y=radius;
                }
                for(var bricks:App.bricks){
                    for(var brick:bricks){
                        if (!brick.isVisible())continue;
                        double closestX = clamp(x, brick.x, brick.x + brick.w);
                        double closestY = clamp(y, brick.y, brick.y + brick.h);

                        double dx = x - closestX;
                        double dy = y - closestY;

                        if (dx * dx + dy * dy < radius * radius) {
                            App.clip.play();
                            yspeed*=-1;
                            brick.setVisible(false);
                        }
                    }
                }

                double closestX = clamp(x, App.bar.getX(), App.bar.getX() + App.bar.getWidth());
                double closestY = clamp(y, App.bar.getY(), App.bar.getY() + App.bar.getHeight());

                double dx = x - closestX;
                double dy = y - closestY;

                if (dx * dx + dy * dy < radius * radius) {
                    y=600-radius-App.bar.getHeight();
                    yspeed*=-1;
                }

                boolean finished=false;
                /*
                for(var bricks:App.bricks){
                    for(var brick:bricks){
                        if (brick.isVisible()){
                            finished=false;
                            break;
                        }
                    }
                }
                */
                if(finished){
                    isRunning=false;
                    Platform.runLater(()->{
                        this.setVisible(false);
                        var label = new Label("You Win!");
                        label.setStyle("-fx-font-size: 48px; -fx-text-fill: white;");
                        label.setLayoutX(300);
                        label.setLayoutY(250);
                        App.c.panel.getChildren().add(label);
                    });
                }
                else{
                    Thread.sleep(1);
                    Platform.runLater(()->{
                        setCenterX(x);
                        setCenterY(y);  
                    });
                }
                
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }    
}