package tdl.s3.sync.progress;

import java.io.File;
import java.util.Date;
import java.util.Optional;

public class UploadStatsProgressListener implements ProgressListener {


    public static class FileUploadStat {

        private final double BYTE_PER_MILLISECOND_TO_MEGABYTES_PER_SECOND = 0.001;

        private int totalSize = 0;

        private long uploadedSize = 0;

        private long startTimestamp = 0;

        FileUploadStat(int totalSize) {
            this.totalSize = totalSize;
            this.startTimestamp = new Date().getTime();
        }

        public int getTotalSize() {
            return totalSize;
        }

        public long getUploadedSize() {
            return uploadedSize;
        }

        void incrementUploadedSize(long size) {
            this.uploadedSize += size;
        }

        public double getMBps() {
            double elapsedMilliseconds = (new Date().getTime() - this.startTimestamp);
            if (elapsedMilliseconds == 0) {
                return 0;
            }
            double bytesUploaded = (double) this.uploadedSize;
            double bytePerMillisecond = bytesUploaded / elapsedMilliseconds;
            return bytePerMillisecond * BYTE_PER_MILLISECOND_TO_MEGABYTES_PER_SECOND;
        }

        public double getUploadRatio() {
            return (double) uploadedSize / (double) totalSize;
        }
    }

    private FileUploadStat fileUploadStat = null;

    @Override
    public void uploadFileStarted(File file, String uploadId) {
        fileUploadStat = new FileUploadStat((int) file.length());
    }

    @Override
    public void uploadFileProgress(String uploadId, long uploadedByte) {
        fileUploadStat.incrementUploadedSize(uploadedByte);
    }

    @Override
    public void uploadFileFinished(File file) {
        fileUploadStat = null;
    }


    //~~~~ Getters


    public Optional<FileUploadStat> getCurrentStats() {
        //TODO Improve this class so that it can handle multiple uploads simultaneously
        return Optional.ofNullable(fileUploadStat);
    }

}

@@ -2063,10 +2063,100 @@
 %0A    %7D%0A%0A
+%0A    public boolean isCurrentlyUploading() %7B%0A        return fileUploadStat != null;%0A    %7D%0A
 %7D%0A

TIDY Improve reporting [skip ci]