#include "trygvis/sensor.h"

#include "json.hpp"
#include <set>
#include <boost/regex.hpp>
#include <boost/lexical_cast.hpp>
#include <chrono>

namespace trygvis {
namespace sensor {

using namespace std;

string to_string(const sample_format_type &arg) {
    if (arg == sample_format_type::AUTO)
        return "auto";
    else if (arg == sample_format_type::CSV)
        return "csv";
    else if (arg == sample_format_type::JSON)
        return "json";
    else if (arg == sample_format_type::KEY_VALUE)
        return "key-value";
    else if (arg == sample_format_type::SQL)
        return "sql";
    else if (arg == sample_format_type::RRD)
        return "rrd";
    else
        return "unknown";
}

std::ostream& operator<<(std::ostream& os, sample_format_type const& type) {
    return os << to_string(type);
}

std::istream& operator>>(std::istream& is, sample_format_type& type) {
    string s;
    is >> s;

    if (s == "auto") {
        type = sample_format_type::AUTO;
    } else if (s == "csv") {
        type = sample_format_type::CSV;
    } else if (s == "key-value") {
        type = sample_format_type::KEY_VALUE;
    } else if (s == "json") {
        type = sample_format_type::JSON;
    } else if (s == "sql") {
        type = sample_format_type::SQL;
    } else if (s == "rrd") {
        type = sample_format_type::RRD;
    }

    return is;
}

template<>
const o<long> SampleRecord::lexical_at(const SampleKey *key) const {
    auto value = at(key);

    if (!value) {
        return o<long>();
    }

    return o<long>(boost::lexical_cast<long>(value.get()));
}
//
//template<class A>
//const o<A> SampleRecord::lexical_at(const SampleKey *key) const {
//    auto value = at(key);
//
//    if (!value) {
//        return o<A>();
//    }
//
//    return o<A>(boost::lexical_cast<A>(value.get()));
//}

}
}