'use strict';
/**
 * Implements directive for google PieChart component. 
 * PieChart component: https://google-developers.appspot.com/chart/interactive/docs/gallery/linechart
 */
angular.module('qusion.charts.line', [
])
    .directive('qnLinechart', [
        function() {
            return {
                require: '?ngModel',
                link: function(scope, element, attr, controller) {
                    var settings = {
                        
                    };

                    var getOptions = function() {
                        return angular.extend({ }, settings, scope.$eval(attr.qnLinechart));
                    };

                    // creates instance of datatable and adds columns from settings
                    var getDataTable = function() {
                        var columns = scope.$eval(attr.qnColumns);
                        var data = new google.visualization.DataTable();
                        angular.forEach(columns, function(column) {
                            data.addColumn(column.type || 'string', column.name);
                        });
                        return data;
                    };

                    var init = function() {
                        var options = getOptions();
                        if (controller) {

                            var drawChart = function() {
                                var data = getDataTable();

                                // set model
                                data.addRows(controller.$viewValue);

                                // Instantiate and draw our chart, passing in some options.
                                var line = new google.visualization.LineChart(element[0]);
                                line.draw(data, options);
                            };

                            controller.$render = function() {
                                drawChart();
                            };
                        }

                        if (controller) {
                            // Force a render to override
                            controller.$render();
                        }
                    };

                    // Watch for changes to the directives options
                    scope.$watch(getOptions, init, true);
                    scope.$watch(getDataTable, init, true);
                }
            };
        }
    ]);