var c3 = require('c3');
var m = require('mithril');
var $osf = require('osfHelpers');
var utils = require('./utils.js');

var Stats = {};

function doughnutGraph (elastic_data) {
    var donutgraph = c3.generate({
        bindto: '#shareDoughnutGraph',
        data: {
            columns: elastic_data['for_charts']['donut_chart'],
            type : 'donut',
        },
        donut: {
            title: 
        },
        legend: {
            show: false
        }
    });
}

function timeGraph (elastic_data) {
    var timegraph = c3.generate({
        bindto: '#shareTimeGraph',
        data: {
            x: 'x',
            columns: elastic_data['for_charts']['date_totals']['date_numbers'],
            type: 'area-spline',
            groups: [elastic_data['for_charts']['date_totals']['group_names']]
        },
        axis: {
            x: {
                type: 'category'
            }
        },
        legend: {
            show: false
        }
    });
}

Stats.view = function(ctrl) {
    return [
        m('.row', {style: {color: 'darkgrey'}}, [
            m('.col-md-4', m('p.text-center', ctrl.vm.latestDate ? ctrl.vm.totalCount + ' events as of ' + ctrl.vm.latestDate : '')),
            m('.col-md-4', m('p.text-center', ctrl.vm.query().length > 0 ? 'Found ' + ctrl.vm.count + ' events in ' + ctrl.vm.time + ' seconds' : '')),
            m('.col-md-4', m('p.text-center', ctrl.vm.providers + ' content providers'))
        ]),
        m('.row', ctrl.vm.showStats ? [
            m('.col-md-12', [
                m('.row', m('.col-md-12', [
                    m('h1.about-share-header', {
                        class: 'animated fadeInUp'
                    },'What is SHARE?'),
                    !ctrl.vm.statsLoaded ? m('img[src=/static/img/loading.gif]') : [
                        m('.row', [
                            m('.col-md-3', ctrl.drawGraph('shareDoughnutGraph', doughnutGraph)),
                            m('.col-md-9', ctrl.drawGraph('shareTimeGraph', timeGraph))
                        ])
                    ]
                ]))
            ]),
        ] : []),
        m('.row', [
            m('col-md-12', m('a.stats-expand', {
                onclick: function() {ctrl.vm.showStats = !ctrl.vm.showStats;}
            },
                ctrl.vm.showStats ? m('i.icon-angle-up') : m('i.icon-angle-down')
            ))
        ])
    ];
};




Stats.controller = function(vm) {
    var self = this;

    self.vm = vm;
    self.vm.providers = 26;

    self.vm.totalCount = 0;
    self.vm.showStats = true;
    self.vm.latestDate = undefined;
    self.vm.statsLoaded = m.prop(false);

    self.drawGraph = function(divId, graphFunction) {
        return m('div', {id: divId, config: function(e, i) {
            if (i) return;
            graphFunction(self.vm.statsData);
        }});
    };

    self.loadStats = function() {
        self.vm.statsLoaded(false);

        m.request({
            method: 'GET',
            url: '/api/v1/share/stats/?' + $.param({q: self.vm.query()}),
            background: true
        }).then(function(data) {
            self.vm.statsData = data;
            self.vm.statsLoaded(true);
        }).then(m.redraw);
    };

    utils.onSearch(self.loadStats);

    m.request({
        method: 'GET',
        background: true,
        url: '/api/v1/share/?size=1',
    }).then(function(data) {
        self.vm.totalCount = data.count;
        self.vm.latestDate = new $osf.FormattableDate(data.results[0].dateUpdated).local;
    }).then(m.redraw);

    self.loadStats();
};

module.exports = Stats;

@@ -132,16 +132,269 @@
  = %7B%7D;%0A%0A
+function get_source_length(elastic_data) %7B%0A%0A    sources = elastic_data.raw_aggregations.sources.buckets;%0A    source_names = %5B%5D;%0A    for (i = 0; i %3C sources.length; i++) %7B %0A        source_names.push(sources%5Bi%5D);%0A    %7D%0A%0A    return source_names.length;%0A%7D%0A%0A
 function
@@ -540,34 +540,33 @@
 ns: elastic_data
-%5B'
+.
 for_charts'%5D%5B'do
@@ -559,20 +559,17 @@
 r_charts
-'%5D%5B'
+.
 donut_ch
@@ -571,18 +571,16 @@
 ut_chart
-'%5D
 ,%0A      
@@ -648,16 +648,62 @@
  title: 
+get_source_length(elastic_data) + ' Providers'
 %0A       

add function for calclulating num of providers per search