
var Thrift = require('../nodejs/thrift-node');

// Define types and services
var Rpc = {};

Rpc.NwResource = Thrift.Struct.define('NwResource', {
    1: { alias: 'id', type: Thrift.Type.STRING },
    2: { alias: 'mimeType', type: Thrift.Type.STRING },
    3: { alias: 'filename', type: Thrift.Type.STRING },
    4: { alias: 'data', type: Thrift.Type.BINARY }
});

Rpc.NwResourceBase64 = Thrift.Struct.define('NwResourceBase64', {
    1: { alias: 'id', type: Thrift.Type.STRING },
    2: { alias: 'mimeType', type: Thrift.Type.STRING },
    3: { alias: 'filename', type: Thrift.Type.STRING },
    4: { alias: 'base64', type: Thrift.Type.STRING }
});

Rpc.NwNote = Thrift.Struct.define('NwNote', {
    1: { alias: 'id', type: Thrift.Type.I32 },
    2: { alias: 'title', type: Thrift.Type.STRING },
    3: { alias: 'source', type: Thrift.Type.STRING },
    4: { alias: 'html', type: Thrift.Type.STRING },
    5: { alias: 'contentClass', type: Thrift.Type.STRING },
    6: { alias: 'sourceURL', type: Thrift.Type.STRING },
    7: { alias: 'author', type: Thrift.Type.STRING }
});

Rpc.DesktopService = {};

Rpc.DesktopService.serverStarted = Thrift.Method.define({
    alias: 'serverStarted',
    args: Thrift.Struct.define('serverStartedArgs', {
        1: { alias: 'serverName', type: Thrift.Type.STRING }
    }),
    result: Thrift.Struct.define('serverStartedResult')
});

Rpc.DesktopService.logMessage = Thrift.Method.define({
    alias: 'logMessage',
    args: Thrift.Struct.define('logMessageArgs', {
        1: { alias: 'message', type: Thrift.Type.STRING }
    }),
    result: Thrift.Struct.define('logMessageResult')
});

Rpc.DesktopService.getLocalizedMessage = Thrift.Method.define({
    alias: 'getLocalizedMessage',
    args: Thrift.Struct.define('getLocalizedMessageArgs', {
        1: { alias: 'identifier', type: Thrift.Type.STRING }
    }),
    result: Thrift.Struct.define('getLocalizedMessageResult', {
        0: { alias: 'returnValue', type: Thrift.Type.STRING }
    })
});

Rpc.DesktopService.getLocalizedMessages = Thrift.Method.define({
    alias: 'getLocalizedMessages',
    args: Thrift.Struct.define('getLocalizedMessagesArgs'),
    result: Thrift.Struct.define('getLocalizedMessagesResult', {
        0: {
            alias: 'returnValue',
            type: Thrift.Type.MAP,
            def: Thrift.Map.define(Thrift.Type.STRING, Thrift.Type.STRING)
        }
    })
});

Rpc.DesktopService.getCurrentUserInfo = Thrift.Method.define({
    alias: 'getCurrentUserInfo',
    args: Thrift.Struct.define('getCurrentUserInfoArgs'),
    result: Thrift.Struct.define('getCurrentUserInfoResult', {
        0: {
            alias: 'returnValue',
            type: Thrift.Type.MAP,
            def: Thrift.Map.define(Thrift.Type.STRING, Thrift.Type.STRING)
        }
    })
});

Rpc.DesktopService.getAnalyticsInfo = Thrift.Method.define({
    alias: 'getAnalyticsInfo',
    args: Thrift.Struct.define('getAnalyticsInfoArgs'),
    result: Thrift.Struct.define('getAnalyticsInfoResult', {
        0: {
            alias: 'returnValue',
            type: Thrift.Type.MAP,
            def: Thrift.Map.define(Thrift.Type.STRING, Thrift.Type.STRING)
        }
    })
});

Rpc.DesktopService.getResource = Thrift.Method.define({
    alias: 'getResource',
    args: Thrift.Struct.define('getResourceArgs', {
        1: { alias: 'resourceUrl', type: Thrift.Type.STRING }
    }),
    result: Thrift.Struct.define('getResourceResult', {
        0: { alias: 'returnValue', type: Thrift.Type.STRUCT, def: Rpc.NwResource }
    })
});

Rpc.DesktopService.getResources = Thrift.Method.define({
    alias: 'getResources',
    args: Thrift.Struct.define('getResourcesArgs', {
        1: { alias: 'noteId', type: Thrift.Type.I32 }
    }),
    result: Thrift.Struct.define('getResourcesResult', {
        0: {
            alias: 'returnValue',
            type: Thrift.Type.LIST,
            def: Thrift.List.define(Thrift.Type.STRUCT, Rpc.NwResource)
        }
    })
});

Rpc.DesktopService.getResourceAsBase64 = Thrift.Method.define({
    alias: 'getResourceAsBase64',
    args: Thrift.Struct.define('getResourceAsBase64Args', {
        1: { alias: 'resourceUrl', type: Thrift.Type.STRING }
    }),
    result: Thrift.Struct.define('getResourceAsBase64Result', {
        0: { alias: 'returnValue', type: Thrift.Type.STRUCT, def: Rpc.NwResourceBase64 }
    })
});

Rpc.DesktopService.getResourcesAsBase64 = Thrift.Method.define({
    alias: 'getResourcesAsBase64',
    args: Thrift.Struct.define('getResourcesAsBase64Args', {
        1: { alias: 'noteId', type: Thrift.Type.I32 }
    }),
    result: Thrift.Struct.define('getResourcesAsBase64Result', {
        0: {
            alias: 'returnValue',
            type: Thrift.Type.LIST,
            def: Thrift.List.define(Thrift.Type.STRUCT, Rpc.NwResourceBase64)
        }
    })
});

Rpc.DesktopService.renderPage = Thrift.Method.define({
    alias: 'renderPage',
    args: Thrift.Struct.define('renderPageArgs', {
        1: { alias: 'resourceUrl', type: Thrift.Type.STRING },
        2: { alias: 'offset', type: Thrift.Type.I32 },
        3: { alias: 'width', type: Thrift.Type.I32 },
        4: { alias: 'height', type: Thrift.Type.I32 },
        5: { alias: 'pageNum', type: Thrift.Type.I32 }
    }),
    result: Thrift.Struct.define('renderPageResult', {
        0: { alias: 'returnValue', type: Thrift.Type.BINARY }
    })
});

Rpc.DesktopService.renderPages = Thrift.Method.define({
    alias: 'renderPages',
    args: Thrift.Struct.define('renderPagesArgs', {
        1: { alias: 'resourceUrl', type: Thrift.Type.STRING },
        2: { alias: 'offset', type: Thrift.Type.I32 },
        3: { alias: 'width', type: Thrift.Type.I32 },
        4: { alias: 'height', type: Thrift.Type.I32 },
        5: { alias: 'pageNum', type: Thrift.Type.LIST, def: Thrift.List.define(Thrift.Type.I32) }
    }),
    result: Thrift.Struct.define('renderPagesResult', {
        0: { alias: 'returnValue', type: Thrift.Type.LIST, def: Thrift.List.define(Thrift.Type.BINARY) }
    })
});

Rpc.DesktopService.updateResource = Thrift.Method.define({
    alias: 'updateResource',
    args: Thrift.Struct.define('updateResourceArgs', {
        1: { alias: 'resource', type: Thrift.Type.STRUCT, def: Rpc.NwResource },
        2: { alias: 'offset', type: Thrift.Type.I32 }
    }),
    result: Thrift.Struct.define('updateResourceResult')
});

Rpc.DesktopService.updateResourceAsBase64 = Thrift.Method.define({
    alias: 'updateResourceAsBase64',
    args: Thrift.Struct.define('updateResourceAsBase64Args', {
        1: { alias: 'resource', type: Thrift.Type.STRUCT, def: Rpc.NwResourceBase64 },
        2: { alias: 'offset', type: Thrift.Type.I32 }
    }),
    result: Thrift.Struct.define('updateResourceAsBase64Result')
});

Rpc.DesktopService.createNoteFromResource = Thrift.Method.define({
    alias: 'createNoteFromResource',
    args: Thrift.Struct.define('createNoteFromResourceArgs', {
        1: { alias: 'resource', type: Thrift.Type.STRUCT, def: Rpc.NwResource },
        2: { alias: 'offset', type: Thrift.Type.I32 }
    }),
    result: Thrift.Struct.define('createNoteFromResourceResult')
});

Rpc.DesktopService.createNoteFromResourceAsBase64 = Thrift.Method.define({
    alias: 'createNoteFromResourceAsBase64',
    args: Thrift.Struct.define('createNoteFromResourceAsBase64Args', {
        1: { alias: 'resource', type: Thrift.Type.STRUCT, def: Rpc.NwResourceBase64 },
        2: { alias: 'offset', type: Thrift.Type.I32 }
    }),
    result: Thrift.Struct.define('createNoteFromResourceResult')
});

Rpc.DesktopService.getNoteId = Thrift.Method.define({
    alias: 'getNoteId',
    args: Thrift.Struct.define('getNoteIdArgs', {
        1: { alias: 'noteLink', type: Thrift.Type.STRING }
    }),
    result: Thrift.Struct.define('getNoteIdResult', {
        0: { alias: 'returnValue', type: Thrift.Type.I32 }
    })
});

Rpc.DesktopService.getNoteTitle = Thrift.Method.define({
    alias: 'getNoteTitle',
    args: Thrift.Struct.define('getNoteTitleArgs', {
        1: { alias: 'noteId', type: Thrift.Type.I32 }
    }),
    result: Thrift.Struct.define('getNoteTitleResult', {
        0: { alias: 'returnValue', type: Thrift.Type.STRING }
    })
});

Rpc.DesktopService.getNoteAsHtml = Thrift.Method.define({
    alias: 'getNoteAsHtml',
    args: Thrift.Struct.define('getNoteAsHtmlArgs', {
        1: { alias: 'noteId', type: Thrift.Type.I32 }
    }),
    result: Thrift.Struct.define('getNoteAsHtmlResult', {
        0: { alias: 'returnValue', type: Thrift.Type.STRING }
    })
});

Rpc.DesktopService.getNote = Thrift.Method.define({
    alias: 'getNote',
    args: Thrift.Struct.define('getNoteArgs', {
        1: { alias: 'noteId', type: Thrift.Type.I32 }
    }),
    result: Thrift.Struct.define('getNoteResult', {
        0: { alias: 'returnValue', type: Thrift.Type.STRUCT, def: Rpc.NwNote }
    })
});

Rpc.DesktopService.markupWindowFocused = Thrift.Method.define({
    alias: 'markupWindowFocused',
    args: Thrift.Struct.define('markupWindowFocusedArgs', {
        1: { alias: 'resourceUrl', type: Thrift.Type.STRING }
    }),
    result: Thrift.Struct.define('markupWindowFocusedResult')
});

Rpc.DesktopService.markupWindowActive = Thrift.Method.define({
    alias: 'markupWindowActive',
    args: Thrift.Struct.define('markupWindowActiveArgs', {
        1: { alias: 'resourceUrl', type: Thrift.Type.STRING }
    }),
    result: Thrift.Struct.define('markupWindowActiveResult')
});

Rpc.DesktopService.markupWindowClosed = Thrift.Method.define({
    alias: 'markupWindowClosed',
    args: Thrift.Struct.define('markupWindowClosedArgs', {
        1: { alias: 'resourceUrl', type: Thrift.Type.STRING }
    }),
    result: Thrift.Struct.define('markupWindowClosedResult')
});

Rpc.DesktopService.presentationWindowClosed = Thrift.Method.define({
    alias: 'presentationWindowClosed',
    args: Thrift.Struct.define('presentationWindowClosedArgs'),
    result: Thrift.Struct.define('presentationWindowClosedResult')
});

Rpc.DesktopService.pdfTrialStarted = Thrift.Method.define({
    alias: 'pdfTrialStarted',
    args: Thrift.Struct.define('pdfTrialStartedArgs'),
    result: Thrift.Struct.define('pdfTrialStartedResult')
});

Rpc.DesktopService.presTrialStarted = Thrift.Method.define({
    alias: 'presTrialStarted',
    args: Thrift.Struct.define('presTrialStartedArgs'),
    result: Thrift.Struct.define('presTrialStartedResult')
});

Rpc.DesktopService.showPremiumPage = Thrift.Method.define({
    alias: 'showPremiumPage',
    args: Thrift.Struct.define('showPremiumPageArgs'),
    result: Thrift.Struct.define('showPremiumPageResult')
});

// Define DesktopService Client
(function (namespace) {
    var ServiceDef = namespace.DesktopService;

    function Client(output) {
        this.output = output;
        this.seqid = 0;
    }

    Client.prototype.serverStarted = function (serverName, onsuccess, onerror) {
        var mdef = ServiceDef.serverStarted;
        var args = new mdef.args();
        args.serverName = serverName;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.logMessage = function (message, onsuccess, onerror) {
        var mdef = ServiceDef.logMessage;
        var args = new mdef.args();
        args.message = message;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.getLocalizedMessage = function (identifier, onsuccess, onerror) {
        var mdef = ServiceDef.getLocalizedMessage;
        var args = new mdef.args();
        args.identifier = identifier;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.getLocalizedMessages = function (onsuccess, onerror) {
        var mdef = ServiceDef.getLocalizedMessages;
        var args = new mdef.args();
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.getCurrentUserInfo = function (onsuccess, onerror) {
        var mdef = ServiceDef.getCurrentUserInfo;
        var args = new mdef.args();
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.getAnalyticsInfo = function (onsuccess, onerror) {
        var mdef = ServiceDef.getAnalyticsInfo;
        var args = new mdef.args();
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.getResource = function (resourceUrl, onsuccess, onerror) {
        var mdef = ServiceDef.getResource;
        var args = new mdef.args();
        args.resourceUrl = resourceUrl;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.getResources = function (noteId, onsuccess, onerror) {
        var mdef = ServiceDef.getResources;
        var args = new mdef.args();
        args.noteId = noteId;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.getResourceAsBase64 = function (resourceUrl, onsuccess, onerror) {
        var mdef = ServiceDef.getResourceAsBase64;
        var args = new mdef.args();
        args.resourceUrl = resourceUrl;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.getResourcesAsBase64 = function (noteId, onsuccess, onerror) {
        var mdef = ServiceDef.getResourcesAsBase64;
        var args = new mdef.args();
        args.noteId = noteId;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.renderPage = function (resourceUrl, offset, width, height, pageNum, onsuccess, onerror) {
        var mdef = ServiceDef.renderPage;
        var args = new mdef.args();
        args.resourceUrl = resourceUrl;
        args.offset = offset;
        args.width = width;
        args.height = height;
        args.pageNum = pageNum;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.renderPages = function (resourceUrl, offset, width, height, pageNums, onsuccess, onerror) {
        var mdef = ServiceDef.renderPages;
        var args = new mdef.args();
        args.resourceUrl = resourceUrl;
        args.offset = offset;
        args.width = width;
        args.height = height;
        args.pageNums = pageNums;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.updateResource = function (resource, offset, onsuccess, onerror) {
        var mdef = ServiceDef.updateResource;
        var args = new mdef.args();
        args.resource = resource;
        args.offset = offset;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.updateResourceAsBase64 = function (resource, offset, onsuccess, onerror) {
        var mdef = ServiceDef.updateResourceAsBase64;
        var args = new mdef.args();
        args.resource = resource;
        args.offset = offset;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.createNoteFromResource = function (resource, offset, onsuccess, onerror) {
        var mdef = ServiceDef.createNoteFromResource;
        var args = new mdef.args();
        args.resource = resource;
        args.offset = offset;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.createNoteFromResourceAsBase64 = function (resource, offset, onsuccess, onerror) {
        var mdef = ServiceDef.createNoteFromResourceAsBase64;
        var args = new mdef.args();
        args.resource = resource;
        args.offset = offset;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.getNoteId = function (noteLink, onsuccess, onerror) {
        var mdef = ServiceDef.getNoteId;
        var args = new mdef.args();
        args.noteLink = noteLink;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.getNoteTitle = function (noteId, onsuccess, onerror) {
        var mdef = ServiceDef.getNoteTitle;
        var args = new mdef.args();
        args.noteId = noteId;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.getNoteAsHtml = function (noteId, onsuccess, onerror) {
        var mdef = ServiceDef.getNoteAsHtml;
        var args = new mdef.args();
        args.noteId = noteId;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.getNote = function (noteId, onsuccess, onerror) {
        var mdef = ServiceDef.getNote;
        var args = new mdef.args();
        args.noteId = noteId;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.markupWindowFocused = function (resourceUrl, onsuccess, onerror) {
        var mdef = ServiceDef.markupWindowFocused;
        var args = new mdef.args();
        args.resourceUrl = resourceUrl;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.markupWindowActive = function (resourceUrl, onsuccess, onerror) {
        var mdef = ServiceDef.markupWindowActive;
        var args = new mdef.args();
        args.resourceUrl = resourceUrl;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.markupWindowClosed = function (resourceUrl, onsuccess, onerror) {
        var mdef = ServiceDef.markupWindowClosed;
        var args = new mdef.args();
        args.resourceUrl = resourceUrl;
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.presentationWindowClosed = function (onsuccess, onerror) {
        var mdef = ServiceDef.presentationWindowClosed;
        var args = new mdef.args();
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.pdfTrialStarted = function (onsuccess, onerror) {
        var mdef = ServiceDef.pdfTrialStarted;
        var args = new mdef.args();
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.presTrialStarted = function (onsuccess, onerror) {
        var mdef = ServiceDef.presTrialStarted;
        var args = new mdef.args();
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    Client.prototype.showPremiumPage = function (onsuccess, onerror) {
        var mdef = ServiceDef.showPremiumPage;
        var args = new mdef.args();
        mdef.sendRequest(this.output, this.seqid++, args, onsuccess, onerror);
    };

    ServiceDef.Client = Client;
})(Rpc);

// Define DesktopService Server
(function (namespace) {
    var ServiceDef = namespace.DesktopService;

    function Server(service, stransport, Protocol, async) {
        var methodName;

        async = (async == null) ? true : async;
        this.service = service;
        this.stransport = stransport;
        this.processor = new Thrift.Processor();

        for (methodName in ServiceDef) {
            if (service[methodName]) {
                this.processor.addMethod(ServiceDef[methodName], service[methodName].bind(service), async);
            }
        }

        this.stransport.process = function (input, output, noop) {
            var inprot = new Protocol(input);
            var outprot = new Protocol(output);
            this.processor.process(inprot, outprot, noop);
        }.bind(this);
    }

    Server.prototype.start = function () {
        this.stransport.listen();
    };

    Server.prototype.stop = function () {
        this.stransport.close();
    };

    ServiceDef.Server = Server;
})(Rpc);


// Node exports
Object.keys(Rpc).forEach(function (key) {
    exports[key] = Rpc[key];
});
