import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;

import java.io.File; 
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import java.io.FileWriter;   // Import the FileWriter class
import java.io.IOException;  // Import the IOException class to handle errors


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

public class Score{
    public Score(Level _level){
        level = _level;
        try {
            File myObj = new File("sc.txt");
            Scanner myReader = new Scanner(myObj);
            best = (int)myReader.nextFloat();
            myReader.close();
        } catch (FileNotFoundException e){
            best = 0;
        }
        view = new Label();
        view.setFont(Font.loadFont("file:Font.ttf", 30));
        view.setTranslateY(20);
        view.setTranslateX(50);
        view.setTextFill(Color.BLACK);
        view.setText("Press SPACE to start!!\nBest:" + best);

        media = new Media(gain);
        mediaGain = new MediaPlayer(media);
        //mediaGain.setDuration(500);
        //mediaGain.setVolumen(0.5);
    }
    public int getBest(){
        return best;
    }
    public int getScore(){
        return score;
    }
    private Media media;
    private MediaPlayer mediaGain;
    private static final String gain = Pterodactyl.class.getResource("/sound/concepto.mp3").toExternalForm();
    public void nextLevel(){
        
        //mediaGain.play();
        score += 1;
        level.levelTransition(score);
        if(score > best) setBestScore();
        if(!level.isWhite()) view.setTextFill(Color.WHITE);
        view.setTranslateX(150);
        actualizar();
    }
    public void resetLevel(){
        view.setTextFill(Color.BLACK);
        actualizar();
        score = 0;
        level.levelTransition(score);
    }
    public void setScore(int _score){
        score = _score;
    }
    public void setBestScore(){
        best = score;
        try {
            FileWriter myWriter = new FileWriter("sc.txt");
            myWriter.write(""+best);
            myWriter.close();
        } catch (IOException e) {
            System.out.println("ERROR");
            e.printStackTrace();
        }
    }
    public Label getView(){
        return view;
    }
    public void actualizar(){
        view.setText("Score: " + score + " Best: " + best);
    }

    private int score;
    private int best;
    private Level level;
    private Label view;
}

        