// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
#include <string>
#include <iostream>

using std::cout;
using std::endl;
using std::string;
using std::to_string;

#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>

using mongocxx::instance;
using mongocxx::uri;
using mongocxx::client;
using mongocxx::database;
using mongocxx::collection;
using mongocxx::cursor;

using bsoncxx::document::value;
using bsoncxx::document::view;
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
using bsoncxx::to_json;

struct Data
{
    string id;
    int iv;
    double xv;
    string sv;
};

static value serialize(Data *o)
{
    return make_document(kvp("id", o->id), kvp("iv", o->iv), kvp("xv", o->xv), kvp("sv", o->sv));
}

static Data deserialize(view d)
{
    Data o;
    o.id = d["id"].get_utf8().value.to_string();
    o.iv = d["iv"].get_int32();
    o.xv = d["xv"].get_double();
    o.sv = d["sv"].get_utf8().value.to_string();
    return o;
}

static void dump(collection col, view doc)
{
    cout << "dumping " << to_json(doc) << endl;
    bool foundsome = false;
    for(view d : col.find(doc))
    {
        //cout << to_json(d) << endl;
        Data o = deserialize(d);
        cout << "(" << o.iv << "," << o.xv << "," << o.sv << ")" << endl;
        foundsome = true;
    }
    if(!foundsome)
    {
        cout << "Not found" << endl;
    }
}

static const int NREC = 1000;
static const int CPP_OFFSET = 1 * NREC;

int main()
{
    instance instance{};
    // open
    uri uri("mongodb://localhost:27017");
    client client(uri);
    database db = client["TestDB"];
    collection col = db["data"];
    // put data
    for(int i = 0; i < NREC; i++)
    {
        Data o;
        o.id = "Doc#" + to_string(CPP_OFFSET + i + 1);
        o.iv = i + 1;
        o.xv = i + 1.0;
        o.sv = "This is value " + to_string(i + 1);
        value d = serialize(&o);
        col.insert_one(d.view());
    }
    //
    value doc77 = make_document(kvp("id", "Doc#" + to_string(CPP_OFFSET + 77)));
    // get
    dump(col, doc77.view());
    // delete
    col.delete_one(doc77.view());
    // get non-existing
    dump(col, doc77.view());
    // get and update
    value doc88 = make_document(kvp("id", "Doc#" + to_string(CPP_OFFSET + 88)));
    dump(col, doc88.view());
    Data o = deserialize(*col.find_one(doc88.view()));
    o.iv = o.iv + 1;
    o.xv = o.xv + 0.1;
    o.sv = o.sv + " updated";
    col.find_one_and_replace(doc88.view(), serialize(&o));
    dump(col, doc88.view());
    // list all
    cursor it = col.find(make_document());
    int n = 0;
    for(view itd : it)
    {
        Data ito = deserialize(itd);
        if(ito.id.rfind("Doc#", 0) != 0)
        {
            cout << "Unexpected document: " << ito.id << endl;
        }
        if(ito.iv < 1 || NREC < ito.iv)
        {
            cout << "Unexpected value: (" << ito.iv << "," << ito.xv << "," << ito.sv << ")" << endl;
        }
        n++;
    }
    cout << n << endl;
    cursor it2 = col.find(make_document(kvp("id", make_document(kvp("$gte", "Doc#" + to_string(CPP_OFFSET + 75)), kvp("$lt", "Doc#" + to_string(CPP_OFFSET + 85))))));
    int n2 = 0;
    for(view it2d : it2)
    {
        Data it2o = deserialize(it2d);
        if(it2o.id.rfind("Doc#", 0) != 0)
        {
            cout << "Unexpected document: " << it2o.id << endl;
        }
        n2++;
    }
    cout << n2 << endl;
    return 0;
}
