/**
 * (c) by aheadware.com
 */

var app = app || {};
app.state = app.state || {};

app.state.Stats = function()
{
    var state = app.core.StateManager.createState( 'stats-overview' );

    state.onEnter = function( p )
    {
        let $content = app.core.View.getContent(),
            timespan = p.timespan || 7,
            groupId = p.groupId || null;

        app.gui.PageLoader.show();

        // Dummy code
        if ( null === groupId )
        {
            groupId = app.model.SessionUser.getGroups()[0].getId();
        }

        app.core.Rpc.call(
            'Stats',
            'getOverview',
            {
                    groupId : groupId
                },
                function( res )
                {



                    app.core.View.setContent(
                        app.core.View.getTemplate(
                            'stats-overview',
                            {
                                stats : res.stats,
                                groupId : groupId
                            }
                        )
                    );


                    var data = {
                        series: [ res.stats.count_accepted, res.stats.count_waiting ]
                    };
                    var sum = function(a, b) { return a + b };

                    if ( res.stats.count_accepted > 0  )
                    {
                        new Chartist.Pie( '.ct-chart', data, {
                            fullWidth: true,
                            labelInterpolationFnc: function(value) {
                                return Math.round(value / data.series.reduce(sum) * 100) + '%';
                            }
                        });
                    }

                    app.gui.PageLoader.hide();
                }
        );

        /*
        app.core.Rpc.call(
            'Stats',
            'get',
            {
                groupId : groupId,
                timespan : timespan
            },
            function( res )
            {
                console.log( res );
                app.core.View.setContent(
                    app.core.View.getTemplate(
                        'stats-overview',
                        {
                            kpis : res.kpis,
                            stats : res.stats,
                            timespan : timespan,
                            groupId : groupId
                        }
                    )
                );

                var xLabelLength = res.stats.labels.length;
                new Chartist.Line('.ct-chart', {
                    labels: res.stats.labels,
                    series: res.stats.series
                }, {
                    fullWidth: true,
                    showPoint: false,
                    axisY: {
                        labelInterpolationFnc: function( value,index ){
                            return ( value % 1 === 0 ) ? value : null;
                        }
                    },
                    axisX: {
                        labelInterpolationFnc: function( value, index ) {
                            var max = 1;
                            if ( xLabelLength === 30 )
                            {
                                max = 5;
                            }
                            else if ( xLabelLength === 90 )
                            {
                                max = 9;
                            }

                            return index % max  === 0 ? value.slice(0, value.length-5) : null;
                        }
                    },
                    chartPadding: {
                        right: 0
                    }
                });

                $content.find( '[data-id="btn-update"]' ).first().click( function()
                {
                    var uri = '#/stats/overview/';

                    uri += $content.find( '[data-id="select-team-id"]' ).first().val();
                    uri += "/";
                    uri += $content.find( '[data-id="select-timespan"]' ).first().val();

                    app.core.Controller.redirect( uri );
                });

                app.gui.PageLoader.hide();
            }
        );*/
    };

    return state;
};
app.state.Stats.prototype.getBeforeValue = function( nowBefore )
{
    return '----';
};
