/**
 * Upgrade Module to manage store upgrades
 */
'use strict';

define(['util/btc'], function(BtcUtils) {
// DarkWallet namespace for the local storage.
var DW_NS = 'dw:identity:';

/**
 * Upgrade version 1 to version 2
 */

function Upgrade1To2(store) {
    // If no scankeys need to regenerate
    if (store.scankeys.length == 0) {
        console.log('You need to reseed the wallet to generate stealth scanning keys!');
        store.reseed = true;
        return false;
    }
    if (!store.mpk) {
        console.log("Wallet without mpk!", this.mpk);
        // throw Error("No mpk!");
    }
 
    // Upgrade Public Keys
    var pubKeys = store.pubkeys;

    var toRemove = [];
    Object.keys(pubKeys).forEach(function(index) {
        var walletAddress = pubKeys[index];
        // Check for malformed address
        if (walletAddress == null || walletAddress.index == null) {
            toRemove.push(index)
            return;
        }
        // Start mpk for pocket addresses
        if (walletAddress.index.length == 1) {
            walletAddress.mpk = BtcUtils.deriveMpk(store.mpk, walletAddress.index[0]);
        }
    });
    // Cleanup malformed addresses
    toRemove.forEach(function(index) {
        console.log("[model] Deleting", pubKeys[index]);
        delete pubKeys[index];
    });

    // Upgrade Public Keys
    var contacts = store.contacts;

    if (!Array.isArray(contacts)) {
        store.contacts = [];
        contacts = store.contacts;
    }
    // Cleanup empty keys
    contacts.forEach(function(contact) {
        if (!contact) {
            console.log("[upgrade] Delete contact", contact);
            contacts.splice(contacts.indexOf(contact), 1);
            return;
        }
    });
    // Upgrade pocket store to new format
    if (typeof store.pockets[0] == 'string') {
        for(var i=0; i< store.pockets.length; i++) {
            store.pockets[i] = {'name': store.pockets[i]};
        };
    }
    return true;
}

/**
 * Upgrade a given store
 */
function Upgrade(store) {
    if (store.version == 2) {
        return false;
    }

    console.log("[upgrade] Upgrading to version 2")

    if (Upgrade1To2(store)) {
        store.version = 2;
        console.log("[upgrade] Upgraded to version 2")
        return true;
    }
}

return Upgrade;
});

@@ -254,233 +254,8 @@
 ) %7B%0A
-    // If no scankeys need to regenerate%0A    if (store.scankeys.length == 0) %7B%0A        console.log('You need to reseed the wallet to generate stealth scanning keys!');%0A        store.reseed = true;%0A        return false;%0A    %7D%0A
     
@@ -1728,52 +1728,423 @@
     
-return true;%0A%7D%0A%0A/**%0A * Upgrade a given store
+// If no scankeys need to regenerate%0A    if (store.scankeys.length == 0) %7B%0A        // Can't finish the upgrade, need user to regenerate keys%0A        console.log('%5Bupgrade%5D You need to reseed the wallet to generate stealth scanning keys!');%0A        store.reseed = true;%0A        return false;%0A    %7D%0A    return true;%0A%7D%0A%0A/**%0A * Upgrade a given store%0A * @return true of false if the store was changed and should be saved
 %0A */
@@ -2394,24 +2394,26 @@
 ion 2%22)%0A    
+%7D%0A
     return t
@@ -2417,22 +2417,16 @@
 n true;%0A
-    %7D%0A
 %7D%0A%0Aretur

move check of scankeys to the end of the upgrade so it doesn't abort other operations, also made the upgrade signal save even if we need user intervention.