/// <reference path="node.d.ts" />

module DB {
    var MongoClient = require('mongodb').MongoClient;
    export class DBProcessor {
        private static getUrl(): string {
            var url: string = 'mongodb://';
            var host = <string>process.env.MONGOHOST;
            var port = <string>process.env.MONGOPORT;
            var user = <string>process.env.MONGOUSER;
            var pass = <string>process.env.MONGOPASS;
            url += user + ':' + pass + '@' + host + ':' + port + '/a2m';
            return url;
        }
        public static getGeometry(address: string, callback: Function) 

        public static setAddressAndGeometry(address: string, geometry: string, callback: Function) {
            var obj = {
                address: address,
                geometry: geometry
            };
            MongoClient.connect(DBProcessor.getUrl(), (err, db) => {
                if (err) throw err;
                db.collection('geometry').insert(obj, {w:1}, (err) => {
                    if (err) {
                        db.close();
                        throw err;
                    }
                    db.close();
                    callback(null);
                });
            });
        }
    }
}
{
            MongoClient.connect(DBProcessor.getUrl(), (err, db) => {
                if (err) throw err;
                var collection = db.collection('geometry');
                collection.find({'address': address}).toArray((err, results) => {
                    if (err) {
                        db.close();
                        throw err;
                    }
                    db.close();
                    callback(null, results);
                });
            });
        }