/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/SplitFilesFX.java
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.calebfontenot.mp5.files_calebfontenot;

import java.io.DataInput;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
 *
 * @author caleb
 */
public class SplitFilesFX extends Application {
    static File file = null;
    final  FileChooser fileChooser = new FileChooser();
    static ProgressBar pb = new ProgressBar();
    static Alert alert = new Alert(Alert.AlertType.INFORMATION);
    @Override
    public void start(final Stage stage) throws Exception
    {
        fileChooser.setTitle("Open File to Split...");
        BorderPane primaryBorderPane = new BorderPane();
        BorderPane secondaryBoarderPane = new BorderPane();
        GridPane textFieldGridPane = new GridPane();
        
        pb.prefWidthProperty().bind(stage.widthProperty());

        Label infoLabel = new Label("If you split a file named tmp into 3 smaller files,\n the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3. ");
        Label chooseFile = new Label("Choose a file to split: ");
        Button openFilePicker = new Button("Choose...");
        Label splitCountLabel = new Label("Enter the amount of files to split into: ");
        TextField splitCount = new TextField();
        Button run = new Button("Start");
        textFieldGridPane.add(chooseFile, 0, 0);
        textFieldGridPane.add(openFilePicker, 1, 0);
        textFieldGridPane.add(splitCountLabel, 0, 1);
        textFieldGridPane.add(splitCount, 1, 1);
        secondaryBoarderPane.setAlignment(run, Pos.CENTER);

        primaryBorderPane.setTop(infoLabel);
        primaryBorderPane.setCenter(textFieldGridPane);
        
        secondaryBoarderPane.setBottom(run);
        secondaryBoarderPane.setTop(pb);
        
        primaryBorderPane.setBottom(secondaryBoarderPane);

        openFilePicker.setOnAction(e -> {
            file = fileChooser.showOpenDialog(openFilePicker.getScene().getWindow());
        });

        run.setOnAction(e -> {
            if (file == null) {
                e.consume();
            }
            splitFile(file, Integer.parseInt(splitCount.getText()));
        });

        Scene scene = new Scene(primaryBorderPane);
        stage.setScene(scene);
        stage.show();
    }
    
    public static void splitFile(File fileToSplit, int splitCount) {
        int outputFileSize = (int) fileToSplit.length() / splitCount;
        System.out.println("output file size will be: " + outputFileSize);
        if (outputFileSize < 0) {
                    alert.setTitle("Output files too large");
                    alert.setHeaderText("Output files too large");
                    alert.setContentText("Please increase the amount of files to split!");

                    alert.showAndWait();
                    return;
        }
        
        double progress = 0;
        String outputPath = fileToSplit.getParent();
        // Open the original file
        try (FileInputStream dataIn = new FileInputStream(fileToSplit)) {
            System.out.println("Opening the source file!");
            for (int i = 0; i < splitCount; ++i) {
                progress = (i * 100) / splitCount;
                System.out.println(progress);
                pb.setProgress(progress);
                dataIn.mark(outputFileSize * (i + 1));
                try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/"  + fileToSplit.getName() + "." + (i + 1))) {
                    dataOut.write(dataIn.readNBytes(outputFileSize), 0, outputFileSize);
                    System.out.println("Writing to " + outputPath + "/" + fileToSplit.getName() + "." + (i + 1));
                }
            }
            
        } catch (IOException ex) {
            System.out.println(ex);
        }
        
    }
    

    public static void main(String[] args)
    {
        launch();
    }
}
/*

 */