package leigh.elements.javafx.type1;

import javafx.beans.property.SimpleStringProperty;
import javafx.concurrent.Task;
import javafx.scene.control.ProgressIndicator;

import java.io.File;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;

public class FilePanelEntry extends Task<Void> {
    private static final Random rng = new Random();
    private final int pauseTime = rng.nextInt(30) + 20;
    public static final int NUM_ITERATIONS = 100;
    private long fileSize;
    private String fileType;
    private File file;
    private UUID id;

    private SimpleStringProperty filename = new SimpleStringProperty();

    public String getFilename() {
        return filename.get();
    }

    public void setFilename(String filename) {
        this.filename.set(filename);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FilePanelEntry that = (FilePanelEntry) o;
        return Objects.equals(filename, that.filename);
    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileType() {
        return fileType;
    }

    public String getFileKind() {
        return fileType;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }

    @Override
    public int hashCode() {
        return Objects.hash(filename);
    }

    @Override
    protected Void call() throws Exception {
        this.updateProgress(ProgressIndicator.INDETERMINATE_PROGRESS, 1);
        this.updateMessage("Running...");
        for (int i = 0; i < NUM_ITERATIONS; i++) {
            updateProgress((1.0 * i) / NUM_ITERATIONS, 1);
            Thread.sleep(pauseTime);
        }
        this.updateMessage("Done");
        this.updateProgress(1, 1);
        return null;
    }

    public long getFileSize() {
        return fileSize;
    }

    public void setFileSize(long fileSize) {
        this.fileSize = fileSize;
    }
}
