require.config({ paths: { 'json.sortify': '/bower_components/json.sortify/dist/JSON.sortify' } });
define([
    '/common/cryptpad-common.js',
    '/common/pinpad.js',
    '/bower_components/jquery/dist/jquery.min.js',
], function (Cryptpad, Pinpad) {
    var $ = window.jQuery;
    var APP = window.APP = {
        Cryptpad: Cryptpad,
    };

    var then = function (call) {
        call.getFileSize('26f014b2ab959418605ea37a6785f317', function (e, msg) {
            if (e) {
                if (e === 'ENOENT') { return; }
                return void console.error(e);
            }
            console.error("EXPECTED ENOENT");
            console.log(msg);
        });

        call.getFileSize('pewpew', function (e, msg) {
            if (e) {
                if (e === 'INVALID_CHAN') { return; }
                return void console.error(e);
            }
            console.log(msg);
        });

        var list = Cryptpad.getUserChannelList();
        if (list.length) {
            call.getFileSize(list[0], function (e, msg) {
                if (e) {
                    return void console.error(e);
                }
                console.log(msg);
            });
        }
        call.getFileListSize(list, function (e, bytes) {
            if (e) { return void console.error(e); }
            console.log("%s total bytes used", bytes);
        });

        call.getServerHash(function (e, hash) {
            if (e) { return void console.error(e); }
            console.log("the server believes your user hash is [%s]", hash);
        });
    };

    var synchronize = function (call) {
        var localHash = call.localChannelsHash();
        var serverHash;

        call.getServerHash(function (e, hash) {
            if (e) { return void console.error(e); }
            serverHash = hash;

            if (serverHash === localHash) {
                return console.log("all your pads are pinned. There is nothing to do");
            }

            call.reset(function (e, response) {
                if (e) { return console.error(e); }

                var list = call.uniqueChannelList();

                // now start pinning...
                list.forEach(function (channel) {
                    call.pin(channel, function (e, out) {
                        if (e) { return console.error(e); }
                    });
                });
            });

/*
            console.log(JSON.stringify({
                local: localHash,
                remote: serverHash,
            }, null, 2));*/
        });
    };

    $(function () {
        Cryptpad.ready(function (err, env) {
            Pinpad.create(function (e, call) {
                if (e) { return void console.error(e); }
                // then(call);
                synchronize(call);
            });
        });
    });
});

@@ -1193,186 +1193,8 @@
   %7D%0A
-        call.getFileListSize(list, function (e, bytes) %7B%0A            if (e) %7B return void console.error(e); %7D%0A            console.log(%22%25s total bytes used%22, bytes);%0A        %7D);%0A%0A
     
@@ -1229,32 +1229,32 @@
 ion (e, hash) %7B%0A
+
             if (
@@ -1498,24 +1498,196 @@
 erverHash;%0A%0A
+        call.getFileListSize(function (e, bytes) %7B%0A            if (e) %7B return void console.error(e); %7D%0A            console.log(%22%25s total bytes used%22, bytes);%0A        %7D);%0A%0A
         call
@@ -2105,24 +2105,65 @@
 nelList();%0A%0A
+                var left = list.length;%0A%0A
             
@@ -2295,15 +2295,49 @@
 (e, 
-out
+hash
 ) %7B%0A
+                        left--;%0A%0A
     
@@ -2364,37 +2364,338 @@
 if (
+!
 e) %7B
- return console.error(e);
+%0A                            console.log('pinned channel %5B%25s%5D. new hash is %5B%25s%5D', channel, hash);%0A                        %7D else %7B%0A                            console.error(e);%0A                        %7D%0A                        if (left === 0) %7B%0A                            console.log('done pinning');%0A                       
  %7D%0A 

test new rpc apis