/*
 * KodeBlox Copyright 2017 Sayak Mukhopadhyay
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http: //www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

"use strict";

const request = require('request');
const progress = require('request-progress');
const fs = require('fs-extra');
const eventEmmiter = require('events').EventEmitter;
const inherits = require('util').inherits;
const zlib = require('zlib');

let fileSize = require('../utilities/file_size');
const ungzip = zlib.createGunzip();
module.exports = Download;

function Download(pathFrom, pathTo) {
    eventEmmiter.call(this);
    let progressPercent = 0.0;
    progress(request.get(pathFrom, {headers: {'Accept-Encoding': 'gzip, deflate, sdch'}}))
        .on('response', response => {
            response.statusCode = 200;
            this.emit('start', response);
        })
        .on('progress', status => {
            if (process.env.NODE_ENV === 'development') {
                console.log(`Downloading File of size ${fileSize.withValue(status.size.total)}\nTime Elapsed: ${status.time.elapsed}\t Time Remaining: ${status.time.remaining}\n${(status.percent * 100).toFixed(2)}% completed\t ${fileSize.withValue(status.size.transferred)} data transferred`);
            }
            progressPercent = status.percent * 100;
        })
        .on('error', err => {
            this.emit('error', {
                error: err,
                progress: progressPercent
            });
        })
        .pipe(ungzip
            .on('finish', () => {
                this.emit('end');
            })
            .on('error', err => {
                this.emit('error', {
                    error: err,
                    progress: progressPercent
                });
            })).pipe(fs.createWriteStream(pathTo))
}

inherits(Download, eventEmmiter);

@@ -1043,39 +1043,8 @@
 s);%0A
-    let progressPercent = 0.0;%0A
     
@@ -1126,16 +1126,28 @@
 , sdch'%7D
+, gzip: true
 %7D))%0A    
@@ -1915,19 +1915,37 @@
 ipe(
-ungzip%0A    
+fs.createWriteStream(pathTo)%0A
     
@@ -1974,36 +1974,32 @@
 %3E %7B%0A            
-   
- 
 this.emit('end')
@@ -2004,28 +2004,24 @@
 ');%0A        
-    
 %7D)%0A         
@@ -2015,28 +2015,24 @@
  %7D)%0A        
-  
-  
 .on('error',
@@ -2033,36 +2033,32 @@
 rror', err =%3E %7B%0A
-    
             this
@@ -2082,36 +2082,32 @@
                 
-    
 error: err,%0A    
@@ -2094,36 +2094,32 @@
     error: err,%0A
-    
                 
@@ -2148,36 +2148,32 @@
 ent%0A            
-    
 %7D);%0A            
@@ -2172,50 +2172,42 @@
     
-    %7D)).pipe(fs.createWriteStream(pathTo))
+%7D))%0A    let progressPercent = 0.0;
 %0A%7D%0A%0A

fix compressed downloads