package com.novoda.downloadmanager;

import android.support.annotation.Nullable;

class LiteDownloadFileStatus implements InternalDownloadFileStatus {

    private final DownloadBatchId downloadBatchId;
    private final DownloadFileId downloadFileId;

    private FileSize fileSize;
    private FilePath localFilePath;
    private Status status;
    private DownloadError downloadError;

    LiteDownloadFileStatus(DownloadBatchId downloadBatchId, DownloadFileId downloadFileId, Status status, FileSize fileSize, FilePath localFilePath) {
        this.downloadBatchId = downloadBatchId;
        this.downloadFileId = downloadFileId;
        this.status = status;
        this.fileSize = fileSize;
        this.localFilePath = localFilePath;
    }

    @Override
    public void update(FileSize fileSize, FilePath localFilePath) {
        this.fileSize = fileSize;
        this.localFilePath = localFilePath;

        if (fileSize.currentSize() == fileSize.totalSize()) {
            markAsDownloaded();
        }
    }

    private void markAsDownloaded() {
        status = Status.DOWNLOADED;
    }

    @Override
    public long bytesDownloaded() {
        return fileSize.currentSize();
    }

    @Override
    public long totalBytes() {
        return fileSize.totalSize();
    }

    @Override
    public FilePath localFilePath() {
        return localFilePath;
    }

    @Override
    public DownloadBatchId downloadBatchId() {
        return downloadBatchId;
    }

    @Override
    public DownloadFileId downloadFileId() {
        return downloadFileId;
    }

    @Override
    public boolean isMarkedAsDownloading() {
        return status == Status.DOWNLOADING;
    }

    @Override
    public boolean isMarkedAsQueued() {
        return status == Status.QUEUED;
    }

    @Override
    public boolean isMarkedForDeletion() {
        return status == Status.DELETION;
    }

    @Override
    public void markAsDownloading() {
        status = Status.DOWNLOADING;
    }

    @Override
    public void isMarkedAsPaused() {
        status = Status.PAUSED;
    }

    @Override
    public boolean isMarkedAsError() {
        return status == Status.ERROR && downloadError != null;
    }

    @Override
    public void markAsQueued() {
        status = Status.QUEUED;
    }

    @Override
    public void markForDeletion() {
        status = Status.DELETION;
    }

    @Override
    public void markAsError(DownloadError.Error error) {
        status = Status.ERROR;
        downloadError = new DownloadError(error);
    }

    @Override
    @Nullable
    public DownloadError error() {
        return downloadError;
    }

    @Override
    public Status status() {
        return status;
    }

    @Override
    public String toString() {
        return "LiteDownloadFileStatus{" +
                "downloadBatchId=" + downloadBatchId +
                ", downloadFileId=" + downloadFileId +
                ", fileSize=" + fileSize +
                ", localFilePath=" + localFilePath +
                ", status=" + status +
                ", downloadError=" + downloadError +
                '}';
    }
}

@@ -2788,18 +2788,16 @@
 Status%7B%22
- +
 %0A       
@@ -2804,16 +2804,18 @@
         
+ +
  %22downlo
@@ -2843,18 +2843,16 @@
 dBatchId
- +
 %0A       
@@ -2851,32 +2851,34 @@
 %0A               
+ +
  %22, downloadFile
@@ -2898,18 +2898,16 @@
 adFileId
- +
 %0A       
@@ -2910,24 +2910,26 @@
             
+ +
  %22, fileSize
@@ -2941,18 +2941,16 @@
 fileSize
- +
 %0A       
@@ -2953,24 +2953,26 @@
             
+ +
  %22, localFil
@@ -2994,19 +2994,18 @@
 FilePath
- +
 %0A
+ 
         
@@ -3007,24 +3007,25 @@
             
++
  %22, status=%22
@@ -3033,18 +3033,16 @@
 + status
- +
 %0A       
@@ -3049,16 +3049,18 @@
         
+ +
  %22, down
@@ -3090,11 +3090,10 @@
 rror
- +
 %0A
+ 
     
@@ -3103,16 +3103,17 @@
         
++
  '%7D';%0A  

Fix toString.