/**
 * Created by Cyan on 3/28/2016.
 */

$(document).ready(function() {
    loadStatisticEvents();

    // Select first sprint
    var firstSprint = $('.selectpicker').val();
    if (firstSprint != '') {
        getSprint(firstSprint);
    }
});

function loadStatisticEvents() {
    // Select tab
    $('.nav li.active').removeClass('selected');
    $('#statistics-tab').addClass('selected');

    // Select picker
    $('.selectpicker').selectpicker({
        style: 'btn-info',
        size: 4
    });

    // Selector event
    $('.selectpicker').change(function() {
        var sprintId = $(this).find('option:selected').val();

        getSprint(sprintId);
    });
}

function getSprint(id) {
    var token = $("input[name='_csrf']").val();

    $.ajax({
        type: "GET",
        headers: {
            'X-CSRF-TOKEN': token
        },
        url: "/sprint/getSprint/",
        data: "id=" + id,
        dataType: "json",
        async: true,
        success: function(data){
            if (data['id'] == id) {
                loadBurndownChart(data);
                loadSprintInfo(data);
            }
        },

        error: function(e){
            showErrorMessage('Error in getting the sprint.');
        }
    });
}

function loadBurndownChart(sprint) {
    $(function() {
        $('#container').highcharts({
            title: {
                text: 'Burndown Chart',
                x: -20 //center
            },
            colors: ['blue', 'red'],
            plotOptions: {
                line: {
                    lineWidth: 3
                },
                tooltip: {
                    hideDelay: 200
                }
            },
            subtitle: {
                text: 'Sprint ' + sprint['id'],
                x: -20
            },
            xAxis: {
                categories: sprint['statistic']['labels']
            },
            yAxis: {
                title: {
                    text: 'Points'
                },
                plotLines: [{
                    value: 0,
                    width: 1
                }]
            },
            tooltip: {
                valueSuffix: ' p',
                crosshairs: true,
                shared: true
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            series: [{
                name: 'Ideal Burn',
                color: 'rgba(255,0,0,0.25)',
                lineWidth: 2,
                data: sprint['statistic']['idealPoints']
            }, {
                name: 'Actual Burn',
                color: 'rgba(0,120,200,0.75)',
                marker: {
                    radius: 6
                },
                data: sprint['statistic']['burnedPoints']
            }]
        });
    });
}

function loadSprintInfo(sprint) {
    $('.spring-info .panel-heading').text('Spring ' + sprint['id'] + ' results');

    $('.sprint-info-table .duration .text').text(sprint['statistic']['duration'] + ' days');
    $('.sprint-info-table .capacity .text').text(sprint['statistic']['capacity'] + ' points');
    $('.sprint-info-table .members .text').text(sprint['statistic']['members'] + ' members');
    $('.sprint-info-table .planned .text').text(sprint['statistic']['planned'] + ' points');
    $('.sprint-info-table .burned .text').text(sprint['statistic']['burned'] + ' points');
    $('.sprint-info-table .remaining .text').text(sprint['statistic']['remaining'] + ' points');
    $('.sprint-info-table .ideal-velocity .text').text(sprint['statistic']['idealVelocity'] + ' points/day');
    $('.sprint-info-table .real-velocity .text').text(sprint['statistic']['realVelocity'] + ' points/day');
}
