package com.novoda.downloadmanager;

import java.io.IOException;

class NetworkFileSizeRequester implements FileSizeRequester {

    private static final String HEADER_CONTENT_LENGTH = "Content-Length";
    private static final int UNKNOWN_CONTENT_LENGTH = -1;
    private static final int ZERO_FILE_SIZE = 0;

    private final HttpClient httpClient;
    private final NetworkRequestCreator requestCreator;

    NetworkFileSizeRequester(HttpClient httpClient, NetworkRequestCreator requestCreator) {
        this.httpClient = httpClient;
        this.requestCreator = requestCreator;
    }

    @Override
    public FileSize requestFileSize(String url) {
        try {
            long fileSize = executeRequestFileSize(url);
            if (fileSize == 0) {
                return FileSizeCreator.unknownFileSize();
            } else {
                return FileSizeCreator.createFromTotalSize(fileSize);
            }
        } catch (IOException e) {
            Logger.e(e, "Error requesting file size for " + url);
        }

        return FileSizeCreator.unknownFileSize();
    }

    private long executeRequestFileSize(String url) throws IOException {
        long fileSize = requestFileSizeThroughHeaderRequest(url);
        if (fileSize == UNKNOWN_CONTENT_LENGTH || fileSize == ZERO_FILE_SIZE) {
            Logger.w("filesize request through header returned zero, we'll try again " + url);
            fileSize = requestFileSizeThroughBodyRequest(url);
            if (fileSize == 0) {
                Logger.w("filesize request through body returned zero");
            }
        }

        return fileSize;
    }

    private long requestFileSizeThroughHeaderRequest(String url) throws IOException {
        NetworkRequest fileSizeRequest = requestCreator.createFileSizeRequest(url);
        HttpClient.NetworkResponse response = httpClient.execute(fileSizeRequest);
        long fileSize = ZERO_FILE_SIZE;
        if (response.isSuccessful()) {
            fileSize = Long.parseLong(response.header(HEADER_CONTENT_LENGTH, String.valueOf(UNKNOWN_CONTENT_LENGTH)));
            response.closeByteStream();
        }
        return fileSize;
    }

    private long requestFileSizeThroughBodyRequest(String url) throws IOException {
        NetworkRequest downloadRequest = requestCreator.createDownloadRequest(url);
        HttpClient.NetworkResponse response = httpClient.execute(downloadRequest);
        long fileSize = ZERO_FILE_SIZE;
        if (response.isSuccessful()) {
            fileSize = response.bodyContentLength();
            response.closeByteStream();
        }

        return fileSize;
    }
}

@@ -740,33 +740,84 @@
 if (fileSize == 
-0
+UNKNOWN_CONTENT_LENGTH %7C%7C fileSize == ZERO_FILE_SIZE
 ) %7B%0A            
@@ -1375,34 +1375,56 @@
 ogger.w(
+String.format(
 %22file
+ 
 size 
+header 
 request 
 through 
@@ -1419,22 +1419,12 @@
 est 
-through header
+'%25s'
  ret
@@ -1429,20 +1429,18 @@
 eturned 
-zero
+%25s
 , we'll 
@@ -1447,21 +1447,44 @@
 try 
-again %22 + url
+with a body request%22, url, fileSize)
 );%0A 
@@ -1573,17 +1573,68 @@
 Size == 
-0
+UNKNOWN_CONTENT_LENGTH %7C%7C fileSize == ZERO_FILE_SIZE
 ) %7B%0A    
@@ -1658,26 +1658,46 @@
 r.w(
+String.format(
 %22file
+ 
 size 
+body 
 request 
 thro
@@ -1696,35 +1696,41 @@
 est 
-through body returned zero%22
+'%25s' returned %25s%22, url, fileSize)
 );%0A 

Improve messaging of NetworkFileSizeRequester.