package com.annimon.tsvc.controllers;

import com.annimon.tsvc.Util;
import com.annimon.tsvc.model.TwitchVideo;
import com.annimon.tsvc.tasks.PlaylistTask;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXProgressBar;
import com.jfoenix.controls.JFXSlider;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;

/**
 * FXML Controller class
 *
 * @author aNNiMON
 */
public class DownloadController implements Initializable {
    
    @FXML
    private VBox root;
    
    @FXML
    private Label lblResultInfo;
    
    @FXML
    private JFXSlider slSpeed;
    
    @FXML
    private JFXButton btnSaveTo;
    
    @FXML
    private JFXComboBox<Label> cbFormats;
    
    @FXML
    private JFXCheckBox cbAudio;
    
    @FXML
    private JFXButton btnDownload;
    
    @FXML
    private TextArea taStatus;
    
    @FXML
    private JFXProgressBar progressBar;
    
    private TwitchVideo video;

    public TwitchVideo getVideo() {
        return video;
    }

    public void setVideo(TwitchVideo video) {
        this.video = video;
        updateResultLength();
    }
    
    @FXML
    private void handleDownload(ActionEvent event) {
        final String vodId = Integer.toString(video.getId());
        final Path playlistPath = Paths.get(vodId + ".m3u8");

        final PlaylistTask task = new PlaylistTask(vodId, playlistPath);
        progressBar.setProgress(-1);
        progressBar.visibleProperty().bind(task.runningProperty());
        progressBar.lookup(".bar").setStyle("-fx-stroke: #FFEB3B;");
        new Thread(task).start();
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        cbFormats.getItems().addAll(new Label("mp4 (best)"), new Label("avi"), new Label("ts"));
        slSpeed.valueProperty().addListener(e ->updateResultLength());
    }
    
    private void updateResultLength() {
        final int value = (int) slSpeed.getValue();
        lblResultInfo.setText(calculateResultLength(value));
    }

    private String calculateResultLength(int speedFactor) {
        if (video == null) return String.format("%dx", (int) speedFactor);
        
        final int resultDuration = (int)(video.getLength() / speedFactor);
        return String.format("%dx %s", (int) speedFactor, Util.duration(resultDuration));
    }
}

@@ -1689,16 +1689,84 @@
 tPath);%0A
+        progressBar.visibleProperty().bind(task.runningProperty());%0A
         
@@ -1806,39 +1806,40 @@
     progressBar.
-visible
+progress
 Property().bind(
@@ -1835,39 +1835,40 @@
 rty().bind(task.
-running
+progress
 Property());%0A   
@@ -1926,14 +1926,14 @@
 e: #
-FFEB3B
+6441A5
 ;%22);
@@ -2142,24 +2142,129 @@
 bel(%22ts%22));%0A
+        progressBar.setProgress(-1);%0A        progressBar.prefWidthProperty().bind(root.widthProperty());%0A
         slSp

Fix progressbar issue