// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package io.gitea.sdk.models;

import java.time.Duration;

/**
 * Represents upload progress.
 */
public class Progress {
    private long bytesDone;
    private long bytesTotal;
    private long chunksDone;
    private long chunksTotal;
    private double percent;
    private double speed;
    private Duration eta;

    public Progress() {
    }

    public Progress(long bytesDone, long bytesTotal, long chunksDone, long chunksTotal, double percent, double speed, Duration eta) {
        this.bytesDone = bytesDone;
        this.bytesTotal = bytesTotal;
        this.chunksDone = chunksDone;
        this.chunksTotal = chunksTotal;
        this.percent = percent;
        this.speed = speed;
        this.eta = eta;
    }

    public long getBytesDone() {
        return bytesDone;
    }

    public void setBytesDone(long bytesDone) {
        this.bytesDone = bytesDone;
    }

    public long getBytesTotal() {
        return bytesTotal;
    }

    public void setBytesTotal(long bytesTotal) {
        this.bytesTotal = bytesTotal;
    }

    public long getChunksDone() {
        return chunksDone;
    }

    public void setChunksDone(long chunksDone) {
        this.chunksDone = chunksDone;
    }

    public long getChunksTotal() {
        return chunksTotal;
    }

    public void setChunksTotal(long chunksTotal) {
        this.chunksTotal = chunksTotal;
    }

    public double getPercent() {
        return percent;
    }

    public void setPercent(double percent) {
        this.percent = percent;
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public Duration getEta() {
        return eta;
    }

    public void setEta(Duration eta) {
        this.eta = eta;
    }

    /**
     * Formats the speed as a human-readable string.
     */
    public String getSpeedFormatted() {
        return formatBytes((long) speed) + "/s";
    }

    /**
     * Formats the ETA as a human-readable string.
     */
    public String getEtaFormatted() {
        if (eta == null) {
            return "unknown";
        }
        long seconds = eta.getSeconds();
        if (seconds < 60) {
            return seconds + "s";
        } else if (seconds < 3600) {
            return (seconds / 60) + "m" + (seconds % 60) + "s";
        } else {
            return (seconds / 3600) + "h" + ((seconds % 3600) / 60) + "m";
        }
    }

    private static String formatBytes(long bytes) {
        if (bytes < 1024) {
            return bytes + " B";
        } else if (bytes < 1024 * 1024) {
            return String.format("%.1f KB", bytes / 1024.0);
        } else if (bytes < 1024 * 1024 * 1024) {
            return String.format("%.1f MB", bytes / 1024.0 / 1024.0);
        } else {
            return String.format("%.1f GB", bytes / 1024.0 / 1024.0 / 1024.0);
        }
    }
}
