/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package yafft.FFModel.ModelItems;

import java.net.URL;
import java.net.URI;

import java.sql.Timestamp;

import java.time.LocalDateTime;

import javafx.beans.property.*;

/**
 *
 * @author michal
 */
public class DownloadsItem {

    private final StringProperty fileName;

    private final ObjectProperty<URL> source;
    private final ObjectProperty<URI> destination;
    private final ObjectProperty<Timestamp> endTime;
    private final LongProperty fileSize;

    public DownloadsItem(StringProperty fileName, ObjectProperty<URL> source, ObjectProperty<URI> destination, ObjectProperty<Timestamp> endTime, LongProperty fileSize) {
        this.fileName = fileName;
        this.source = source;
        this.destination = destination;
        this.endTime = endTime;
        this.fileSize = fileSize;
    }

    public StringProperty getFileName() {
        return fileName;
    }

    public ObjectProperty<URL> getSource() {
        return source;
    }

    public ObjectProperty<URI> getDestination() {
        return destination;
    }

    public ObjectProperty<Timestamp> getEndTime() {
        return endTime;
    }

    public LongProperty getFileSize() {
        return fileSize;
    }

    public StringProperty getFileSizeMB() {
        //fileName=poznamky-mpr.txt,  fileSize=LongProperty [value: 573]}
        Double d = 0.0;
        String r = "-";
        String unit = "-";

        if (fileSize.getValue() < 10) {
            return new SimpleStringProperty(r);
        }

        double l = 1024.0;
        if (fileSize.getValue() < l) {
            d = fileSize.getValue().doubleValue(); unit = " B";
        } else {

           double ll = l * l;
            if (fileSize.getValue() < ll) {
                d = fileSize.getValue() / l; unit = " KB";
            } else {
                
                double lll = ll * l;
                if (fileSize.getValue() < lll) {
                    d = fileSize.getValue() / ll ;unit = " MB";
                } else {

                    double llll = lll * l;
                    if (fileSize.getValue() < llll) {
                        d = fileSize.getValue() / lll;unit = " GB";
                    }
                }
            }
        }
        r = String.valueOf(d);
        int index = r.indexOf(".");
        int ff = r.length() - index;
        if (ff > 3) { ff = 3;}
        r = r.substring(0, index + ff);
        r += unit;

        return new SimpleStringProperty(r);
    }

    @Override
    public String toString() {
        return "DownloadsItem{" + "fileName=" + fileName.getValue() + ", source=" + source.getName() + ", destination=" + destination + ", endTime=" + endTime + ", fileSize=" + fileSize + '}';
    }

}
