package com.novoda.downloadmanager;

class VersionOneToVersionTwoMigrationStatus implements InternalMigrationStatus {

    private static final int TOTAL_PERCENTAGE = 100;

    private final String migrationId;

    private Status status;
    private int numberOfBatches;
    private int totalNumberOfBatches;
    private int percentageMigrated;

    VersionOneToVersionTwoMigrationStatus(String migrationId, Status status, int numberOfBatches, int totalNumberOfBatches, int percentageMigrated) {
        this.migrationId = migrationId;
        this.status = status;
        this.numberOfBatches = numberOfBatches;
        this.totalNumberOfBatches = totalNumberOfBatches;
        this.percentageMigrated = percentageMigrated;
    }

    @Override
    public void update(int currentBatch, int numberOfBatches) {
        this.numberOfBatches = currentBatch;
        this.totalNumberOfBatches = numberOfBatches;
        this.percentageMigrated = getPercentageFrom(currentBatch, numberOfBatches);
    }

    private int getPercentageFrom(int numberOfBatches, int totalNumberOfBatches) {
        return (int) ((((float) numberOfBatches) / ((float) totalNumberOfBatches)) * TOTAL_PERCENTAGE);
    }

    @Override
    public void markAsExtracting() {
        status = Status.EXTRACTING;
    }

    @Override
    public void markAsMigrating() {
        status = Status.MIGRATING_FILES;
    }

    @Override
    public void markAsDeleting() {
        status = Status.DELETING_V1_DATABASE;
    }

    @Override
    public void markAsComplete() {
        status = Status.COMPLETE;
    }

    @Override
    public String migrationId() {
        return migrationId;
    }

    @Override
    public int numberOfMigratedBatches() {
        return numberOfBatches;
    }

    @Override
    public int totalNumberOfBatchesToMigrate() {
        return totalNumberOfBatches;
    }

    @Override
    public int percentageMigrated() {
        return percentageMigrated;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        VersionOneToVersionTwoMigrationStatus that = (VersionOneToVersionTwoMigrationStatus) o;

        if (numberOfBatches != that.numberOfBatches) {
            return false;
        }
        if (totalNumberOfBatches != that.totalNumberOfBatches) {
            return false;
        }
        if (percentageMigrated != that.percentageMigrated) {
            return false;
        }
        if (migrationId != null ? !migrationId.equals(that.migrationId) : that.migrationId != null) {
            return false;
        }
        return status == that.status;
    }

    @Override
    public int hashCode() {
        int result = migrationId != null ? migrationId.hashCode() : 0;
        result = 31 * result + (status != null ? status.hashCode() : 0);
        result = 31 * result + numberOfBatches;
        result = 31 * result + totalNumberOfBatches;
        result = 31 * result + percentageMigrated;
        return result;
    }

    @Override
    public String toString() {
        return "VersionOneToVersionTwoMigrationStatus{" +
                "migrationId='" + migrationId + '\'' +
                ", status=" + status +
                ", numberOfBatches=" + numberOfBatches +
                ", totalNumberOfBatches=" + totalNumberOfBatches +
                ", percentageMigrated=" + percentageMigrated +
                '}';
    }
}

@@ -252,31 +252,43 @@
 int numberOf
-Batches
+MigrationsCompleted
 ;%0A    privat
@@ -302,30 +302,33 @@
 otalNumberOf
-Batche
+Migration
 s;%0A    priva
@@ -420,70 +420,253 @@
 nId,
- Status status, int numberOfBatches, int totalNumberOfBatches,
+%0A                                          Status status,%0A                                          int numberOfMigrationsCompleted,%0A                                          int totalNumberOfMigrations,%0A                                         
  int
@@ -783,33 +783,57 @@
 erOf
-Batches = numberOfBatches
+MigrationsCompleted = numberOfMigrationsCompleted
 ;%0A  
@@ -852,30 +852,33 @@
 otalNumberOf
-Batche
+Migration
 s = totalNum
@@ -874,38 +874,41 @@
  = totalNumberOf
-Batche
+Migration
 s;%0A        this.
@@ -1054,23 +1054,35 @@
 numberOf
-Batches
+MigrationsCompleted
  = curre
@@ -1116,22 +1116,25 @@
 NumberOf
-Batche
+Migration
 s = numb
@@ -1974,31 +1974,43 @@
 urn numberOf
-Batches
+MigrationsCompleted
 ;%0A    %7D%0A%0A   
@@ -2093,30 +2093,33 @@
 otalNumberOf
-Batche
+Migration
 s;%0A    %7D%0A%0A  
@@ -2608,39 +2608,63 @@
 erOf
-Batches != that.numberOfBatches
+MigrationsCompleted != that.numberOfMigrationsCompleted
 ) %7B%0A
@@ -2724,22 +2724,25 @@
 NumberOf
-Batche
+Migration
 s != tha
@@ -2748,38 +2748,41 @@
 at.totalNumberOf
-Batche
+Migration
 s) %7B%0A           
@@ -3303,39 +3303,51 @@
 esult + numberOf
-Batches
+MigrationsCompleted
 ;%0A        result
@@ -3372,30 +3372,33 @@
 otalNumberOf
-Batche
+Migration
 s;%0A        r
@@ -3697,35 +3697,59 @@
 erOf
-Batches=%22 + numberOfBatches
+MigrationsCompleted=%22 + numberOfMigrationsCompleted
  +%0A 
@@ -3779,22 +3779,25 @@
 NumberOf
-Batche
+Migration
 s=%22 + to
@@ -3807,22 +3807,25 @@
 NumberOf
-Batche
+Migration
 s +%0A    

Rename Migration variables for explicitness.