import javafx.scene.Group;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Ellipse;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.util.Duration;
import javafx.scene.control.Label;
import javafx.scene.paint.ImagePattern;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;

import java.util.Locale;
import java.io.*;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.util.Duration;

import javafx.scene.media.AudioClip;


public class Pterodactyl extends Group{
    public Pterodactyl(Score _score){
        score = _score;

        cuerpo = new Rectangle(800/2-width/2, 700/2-height/2, width, height);
        hit = new Ellipse(800/2, 700/2, width/2-10, height/2-10);
        getChildren().add(cuerpo);
        //getChildren().add(hit);
        animation = new Timeline(new KeyFrame(Duration.millis(10), e->movement()));
        animation.setCycleCount(Timeline.INDEFINITE);

        estado = State.STOPED;
        borde = State.NOT_HITTING;
        gameOver = false;

        der = new Image("img/obj/pterodactyl_der.png");
        ipder = new ImagePattern(der);
        /*
        izq = new Image("img/obj/pterodactyl.png");
        ipizq = new ImagePattern(izq);
        */
        
        jumping = new Image("img/obj/aleteo_derecha.png");
        ipjumping = new ImagePattern(jumping);;   

        cuerpo.setFill(ipder);
   
        
        jump_a = new AudioClip(jumpaudio);

        Media media = new Media(MEDIA_URL);
        mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setVolume(0.7);
        mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);

        Media dead = new Media(deadaudio);
        deadPlayer = new MediaPlayer(dead);
        deadPlayer.setVolume(0.1);
    }

    private static final String jumpaudio = Pterodactyl.class.getResource("/sound/jump.mp3").toExternalForm();

    private static final String deadaudio = Pterodactyl.class.getResource("/sound/dead.mp3").toExternalForm();
    private static final String MEDIA_URL = Pterodactyl.class.getResource("/sound/backg.mp3").toExternalForm();
    private MediaPlayer mediaPlayer;
    private MediaPlayer deadPlayer;

    public void play(){
        animation.play();
        mediaPlayer.play();
        deadPlayer.stop();
        velocityY = 0;
        estado = State.GOING_RIGHT;
        time = AppTime.getCurrentTime();
        gameOver = false;
    }
    public void stop(){
        deadPlayer.play();
        animation.stop();
        level = 0;
        estado = State.STOPED;
        cuerpo.setX(800/2-width/2);
        cuerpo.setY(700/2-height/2+50);
        hit.setCenterX(800/2);
        hit.setCenterY(700/2+50);
        cuerpo.setScaleY(1);
        cuerpo.setScaleX(1);
        cuerpo.setFill(ipder);
        gameOver = true;
        velocityX =3;
        gravity = 13;
        jumpForce = 5;
        score.resetLevel();
    }
    public void movement(){
        int x = (int)cuerpo.getX();
        if(x+width >= 800 || x <= 0) changeDirection();
        flying();
    }
    public void flying(){
        double current_t = AppTime.getCurrentTime();
        double delta_t = current_t - time;
        
        int x = (int)cuerpo.getX();
        if(estado == State.GOING_RIGHT){
            cuerpo.setX(x+velocityX);
            hit.setCenterX(x+width/2+velocityX);
        }
        else if (estado == State.GOING_LEFT){
            cuerpo.setX(x-velocityX);
            hit.setCenterX(x+width/2+velocityX);
        }
        falling(delta_t);
        

        
        if(cuerpo.getY() >= 700-height) stop();
        else if(cuerpo.getY() <= 0) stop();
        else {
            cuerpo.setY(cuerpo.getY() + velocityY);
            hit.setCenterY(hit.getCenterY() + velocityY);
        }
        time = current_t;
    }
    public void stopJumping(){
        cuerpo.setFill(ipder);
    }
    public void changeDirection(){
        level += 1;
		score.nextLevel();
        borde = State.HITTING;
        cuerpo.setScaleX(-1*cuerpo.getScaleX());
        if(estado == State.GOING_RIGHT){
            estado = State.GOING_LEFT;
        } 
        else if(estado == State.GOING_LEFT){
            estado = State.GOING_RIGHT;
        } 
        
        if(level%8==0){
            velocityX = velocityX + 1;
        }
        if(level%16==0){
            cuerpo.setScaleY(-1*cuerpo.getScaleY());
            velocityX = 3;
            gravity = gravity * -1;
            jumpForce = jumpForce * -1; 
        }
        
    }
    public void jump(){
        velocityY =- jumpForce;
        jump_a.play(0.1);
        cuerpo.setFill(ipjumping);
    }
    public void falling(double time){
        velocityY = velocityY + gravity*time;
        if (velocityY > terminal_vel)
        {
            velocityY = terminal_vel;
        }
    }
    public boolean isFlying(){
        if(estado != State.STOPED) return true;
        return false;
    }
    public State getState(){
        return estado;
    }
    public State getBorder(){
        return borde;
    }
    public void setBorder(){
        borde = State.NOT_HITTING;
    }

    public int getLevel(){
        return level;
    }

    public Ellipse getHitBox(){
        return hit;
    }

    Image der;
    ImagePattern ipder;
    Image izq;
    ImagePattern ipizq;
    Image jumping;
    ImagePattern ipjumping;

    double velocityY = 0;
    double velocityX = 3;
    double gravity = 13;
    double terminal_vel = 10;
    double jumpForce = 5;
    Score score;
    private double time;
    int bScore;
    int level = 0;
    int width = 92;
    int height = 53;
    Rectangle cuerpo;
    private Ellipse hit;
    Timeline animation;
    State estado;
    State borde;
    boolean gameOver;
    AudioClip jump_a;
}