/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
'use strict';

angular.module('dashboard', ["highcharts-ng"])

.controller('dashboardController', ['$scope', '$window', 'AppUtil', 'APIName', '$http', 'APIStatus', 'app', '$filter', 'util','session', function ($scope, $window, AppUtil, APIName, $http, APIStatus, app, $filter, util,session) {

    $scope.chartTypes = [
    {"id": "line", "title": "Line"},
    {"id": "spline", "title": "Smooth line"},
    {"id": "area", "title": "Area"},
    {"id": "areaspline", "title": "Smooth area"},
    {"id": "column", "title": "Column"},
    {"id": "bar", "title": "Bar"},
    {"id": "pie", "title": "Pie"},
    {"id": "scatter", "title": "Scatter"}
    ];

    $scope.date = new Date();
    $scope.getCurrentYear = $filter('date')(new Date(), 'yyyy');
    $scope.currentUser = session.getUser();
    $scope.hogoDashboard = {
        users: "",
        teams: "",
        files: "",
        storage: ""
    };

    $scope.pieChartData = [];

    AppUtil.createRequest(APIName.COMPANY_STATISTIC, {company_id: $scope.currentUser.company_id},function(res){
        var result = res.data;

        $scope.hogoDashboard = {
        users: result.users,
        teams: result.teams,
        files: result.files,
        storage: result.total_storage
        };

        var data = result.team_statistic;

        angular.forEach(data, function(value,key){
            $scope.pieChartData.push([value.name, value.number_of_team]);
        });


    // Pie chart
    $scope.chartConfig2 = {
        options: {
            chart: {
                type: 'pie'
            },
            plotOptions: {
                series: {
                    stacking: ''
                }
            }
        },
        series: [{"name": util.translate("dashboard.numbers"), "data": $scope.pieChartData}],
        title: {
            text: util.translate("dashboard.team_statistic")
        },
        credits: {
            enabled: false
        },
        loading: false,
        size: {}
    };

    }, function(error){

    });

    $scope.currentChartData=[];

    $scope.drawChart=function ()
    {
        $scope.result = $scope.currentChartData.data;
        var lineChartXAxis = [];
        var lineChartYAxis = [];
        angular.forEach($scope.result, function (value, key) {
            //lineChartData.push([value.month, value.number_of_teams_registration]);
            lineChartXAxis.push([value.month]);
            lineChartYAxis.push([value.number_of_teams_registration]);
        });
        $scope.chartConfig1 = {
            options: {
                chart: {
                    type: 'line'
                },
                plotOptions: {
                    series: {
                        stacking: ''
                    }
                }
            },
            series: [{"name": util.translate("dashboard.number_of_team_registration"), "data": lineChartYAxis}],
            xAxis: {
                title: {text: util.translate("dashboard.months")},
                categories: lineChartXAxis
            },
            yAxis: {
                title: {
                    text: util.translate("dashboard.numbers")
                }
            },
            title: {
                text: $scope.getCurrentYear
            },
            credits: {
                enabled: false
            },
            loading: false,
            size: {}
        };
    };

    $scope.loadLineChart = function (param) {

        AppUtil.createRequest(APIName.TEAM_REGISTRATION_STATISTIC, {year: param}, function (res) {

            $scope.currentChartData = res;
            $scope.drawChart();

        }, function (error) {

        });
    }
    ;
    $scope.loadLineChart($scope.getCurrentYear);

    $scope.viewPreviousYear = function(){

        var previousYear = new Date($scope.getCurrentYear);
        previousYear.setYear(previousYear.getFullYear()-1);
        $scope.getCurrentYear = $filter('date')(previousYear, 'yyyy');
        $scope.loadLineChart($scope.getCurrentYear);

    };

    $scope.viewNextYear = function(){

        var previousYear = new Date($scope.getCurrentYear);
        previousYear.setYear(previousYear.getFullYear()+1);
        $scope.getCurrentYear = $filter('date')(previousYear, 'yyyy');
        $scope.loadLineChart($scope.getCurrentYear);

    };

}]);
