// Copyright © 2014 Intel Corporation. All rights reserved.
// Use  of this  source  code is  governed by  an Apache v2
// license that can be found in the LICENSE-APACHE-V2 file.

var FS = require("fs");
var Http = require("http");
var Url = require("url");

var Config = require("./Config");



/**
 * Callback signature for {@link Downloader.get}
 * @memberOf Downloader
 * @inner
 */
function downloadFinishedCb(errormsg) {}



/**
 * Create Downloader object.
 * @param {String} toPath Path do download to.
 * @throws {Downloader~FileCreationFailedError} If file toPath could not be opened.
 * @constructor
 */
function Downloader(toPath) {

    var options = {
        flags: "wx",
        mode: 0600
    };
    this._fp = FS.createWriteStream(toPath, options);

    if (!this._fp) {
        throw new FileCreationFailedError("Could not open file " + toPath);
    }

    this._downloaded = 0;
    this._contentLength = 0;
}

/**
 * Download file.
 * @function get
 * @param {String} url Url to download
 * @param {Function} callback see {@link Downloader~downloadFinishedCb}
 * @memberOf Downloader
 */
Downloader.prototype.get =
function(url, callback) {

    // Object can only be used once.
    if (!this._fp) {
        callback("Downloader object can only be used once");
        return;
    }

    Http.get(url, function(res) {

        if (res.statusCode != 200) {
            callback("Download failed: HTTP Status " + res.statusCode);
            return;
        }

        this._contentLength = res.headers["content-length"];

        res.on("error", function(e) {

            this._fp.end();
            this._fp = null;
            callback("Download failed: " + e.message);

        }.bind(this));

        res.on('data', function(data) {

            this._fp.write(data);
            this._downloaded += data.length;
            this.progress(this._downloaded / this._contentLength);

        }.bind(this));

        res.on('end', function() {

            this._fp.end();
            this._fp = null;
            callback(null);

        }.bind(this))

    }.bind(this));
};

Downloader.prototype.progress =
function(progress) {

};



/**
 * Creates a new FileCreationFailedError.
 * @extends Error
 * @param {String} message Error message.
 * @constructor
 * @memberOf Downloader
 * @inner
 */
function FileCreationFailedError(message) {
    Error.call(this, message);
}
FileCreationFailedError.prototype = Error.prototype;

Downloader.FileCreationFailedError = FileCreationFailedError;



module.exports = Downloader;
@@ -458,16 +458,56 @@
 object.%0A
+ * @param %7BString%7D url URL to download.%0A
  * @para
@@ -545,16 +545,16 @@
 oad to.%0A
-
  * @thro
@@ -669,16 +669,21 @@
 nloader(
+url, 
 toPath) 
@@ -681,24 +681,46 @@
  toPath) %7B%0A%0A
+    this._url = url;%0A%0A
     var opti
@@ -1034,47 +1034,8 @@
 get%0A
- * @param %7BString%7D url Url to download%0A
  * @
@@ -1166,21 +1166,16 @@
 unction(
-url, 
 callback
@@ -1177,16 +1177,136 @@
 lback) %7B
+%0A%0A    // Object can only be used once.%0A    if (!this._url) %7B%0A        callback(%22Invalid URL null%22);%0A        return;%0A    %7D
 %0A%0A    //
@@ -1454,16 +1454,22 @@
 ttp.get(
+this._
 url, fun
@@ -1772,32 +1772,62 @@
 his._fp = null;%0A
+            this._url = null;%0A
             call
@@ -2158,32 +2158,32 @@
 this._fp.end();%0A
-
             this
@@ -2187,32 +2187,62 @@
 his._fp = null;%0A
+            this._url = null;%0A
             call

Refactor to encapsulate single download operation