'use strict'

var co = require('co');
var util = require('util');
var logger = require('winston');
var thunkify = require('thunkify');
var request = require('request');
var get = thunkify(request.get); 
var fs = require('fs');
var JSZip = require("jszip");
var path= require('path');

var api = require('../api');

var app = require.main.exports;
var bot = app.bot;
var config = app.config;


function exec(cmd) {

    return co(function *() {
        var msg = cmd.msg;

        var busyMsg = yield bot.sendMessage(msg, "Pulling latest Destiny Manifest"+"** :mag:");
        var manifest = yield api.manifest();

        // manifest already contains what we want...
        //var worldContents = JSON.stringify(manifest);
        //var mobileWorldContents = JSON.parse(worldContents);
        var mwcUrl = 'http://www.bungie.net'+manifest.mobileWorldContentPaths.en;
        logger.debug("Retrieving Mobile World Contents from %s", mwcUrl);

        // request defaults to UTF-8 encoding
        // http://stackoverflow.com/questions/14855015/getting-binary-content-in-node-js-using-request
        // make sure encoding: null

        var res = (yield get({ url: mwcUrl, encoding: null}))[0];

        if (res.statusCode !== 200) {
            logger.error("failed to download mobileWorldContent: %s\n", res.statusCode);
            throw new Error("download failure: "+res.statusMessage);
        } else {
            logger.debug("Reading manifest file...");
            var zip = new JSZip()

            yield zip.loadAsync(res.body);

            // get a list of content files
            var files = zip.file(/.content$/);
            var dbfile;
            // loop around the old fashioned way because we are using yield...
            for(var f = 0; f < files.length; f++) {
                dbfile = path.join(config.appDir, 'persist', 'destiny', files[f].name);
                logger.debug("extracting file: %s, to: %s", files[f].name, dbfile);
                // TODO: make sure the directory exists - might want to think about
                // using this: https://github.com/jrajav/mkpath

                // wow, the docs for jszip are really terse...
                // https://stuk.github.io/jszip/documentation/api_zipobject/node_stream.html
                files[f].nodeStream().pipe(fs.createWriteStream(dbfile));
            }

        }

    });

}

module.exports = {
    desc: 'Get latest Destiny Manifest file',
    name: 'manifest',
    alias: ['man'],
    exec: exec
}
@@ -609,181 +609,8 @@
 t();
-%0A%0A        // manifest already contains what we want...%0A        //var worldContents = JSON.stringify(manifest);%0A        //var mobileWorldContents = JSON.parse(worldContents);
 %0A   
@@ -688,16 +688,25 @@
 ths.en;%0A
+        %0A
         
@@ -776,194 +776,8 @@
 );%0A%0A
-        // request defaults to UTF-8 encoding%0A        // http://stackoverflow.com/questions/14855015/getting-binary-content-in-node-js-using-request%0A        // make sure encoding: null%0A%0A
     
@@ -1622,368 +1622,452 @@
     
-// TODO: make sure the directory exists - might want to think about%0A                // using this: https://github.com/jrajav/mkpath%0A%0A                // wow, the docs for jszip are really terse...%0A                // https://stuk.github.io/jszip/documentation/api_zipobject/node_stream.html%0A                files%5Bf%5D.nodeStream().pipe(fs.createWriteStream(dbfile)
+%0A                files%5Bf%5D.nodeStream().pipe(fs.createWriteStream(dbfile, %7B%22flags%22: %22w+%22%7D));%0A%0A                fs.stat(dbfile, function(err, stats) %7B%0A                    if (err) %7B%0A                        logger.error(%22There is a problem with the manifest file: %22 + err);%0A                    %7D%0A%0A                    return bot.updateMessage(busyMsg, %22New Destiny Manifest file has been successfully downloaded!%22+%22** :thumbsup:%22);%0A                %7D
 );%0A 

verify manifest and update msg to chat