import javafx.scene.shape.Rectangle;
import javafx.scene.Group;
import java.util.ArrayList;
import javafx.scene.shape.Ellipse;

import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.util.Duration;

import javafx.scene.paint.ImagePattern;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;

public class spike extends Group{
    public spike(Pterodactyl _tero){
        tero = _tero;

        der = new Image("img/obj/spike_der.png");
        ipder = new ImagePattern(der);
        izq = new Image("img/obj/spike_izq.png");
        ipizq = new ImagePattern(izq);

        animation = new Timeline(new KeyFrame(Duration.millis(10), e->takeAction()));
        animation.setCycleCount(Timeline.INDEFINITE);
        columns = new ArrayList<Ellipse>();
        animation.play();

        spikes=1;
    }

    public void takeAction(){
        Spawner();
        Collision();
    }
    public void Collision(){
        for(Ellipse column:columns) {
            if((column.getBoundsInParent().intersects(tero.getHitBox().getBoundsInParent()))){
                tero.stop();
                spikes = 1;
            }
        }
    }
    public void Spawner(){ 
        int level = tero.getLevel();
        
        if(tero.getState() == State.STOPED){
            delSpikes();
            spikes = 1;
        } 
        if(tero.getBorder() == State.HITTING){
            if(level <= 8){ //funcion que define cantidad de spikes
                if (level%2==0){ //si es par, se agrega 1 spike hasta alcanzar max 5 spikes
                    spikes=spikes+1;
                }
            }
            
            if(tero.getState() == State.GOING_LEFT){ //se crean spikes en la izq
                newSpikes(0, spikes);
            }
            else if(tero.getState() == State.GOING_RIGHT){ //se crean spikes en la der
                newSpikes(800, spikes);
            }
            tero.setBorder();
        }
    }
    public void newSpikes(int xPos, int cant){ //Funcion para crear spikes
        delSpikes();
        
        int i;
        int x = 0;
        int space = 10;
        if(xPos == 800){
            x = 800;
        } 
        for(i=0; i < cant ; i++){
            int y = (int)i*(700/cant) + height/2 +(int)(Math.random()*(700/cant - 2*height + 1));
            Ellipse sp = new Ellipse(x, y, width, height);
            if(xPos == 800) sp.setFill(ipder);
            else sp.setFill(ipizq);
            //sp.setFill(Color.RED);
            columns.add(sp);
        }
        getChildren().addAll(columns);
    }
    public void delSpikes(){ //Funcionar borrar spikes
        if (columns.isEmpty()){
            return;
        }
        getChildren().removeAll(columns);
        columns.clear();
    }

    public ArrayList<Ellipse> getColumnas(){
        return columns;
    }
    private Timeline animation;
    private Pterodactyl tero;
    private int width = 70;
    private int height = 25;
    private ArrayList<Ellipse> columns;
    
    private Image der;
    private ImagePattern ipder;
    private Image izq;
    private ImagePattern ipizq;
 
    private int spikes;
}

