'use strict';

angular.module('qusion.ui.datepicker', [])
    .directive('qnDatepicker', [
        function() {
            return {
                require: '?ngModel',
                link: function(scope, element, attr, controller) {

                    var settings = {
                        dateFormat: 'yy-mm-dd',
                        changeYear: true,
                        changeMonth: true,
                        altField: "#" + element.attr("id"),
                        altFormat: "yy-mm-dd"
                    };

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

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

                        if (controller) {
 
                                var updateModel = function () {
                                    scope.$apply(function () {
                                        var date = element.datepicker("getDate");
                                        element.datepicker("setDate", element.val());
                                        controller.$setViewValue(date);
                                        element.blur();
                                    });
                                };
                            
                                if (options.onSelect) {
                                    // Caller has specified onSelect, so call this as well as updating the model
                                    var userHandler = opts.onSelect;
                                    options.onSelect = function (value, picker) {
                                        updateModel();
                                        scope.$apply(function () {
                                            userHandler(value, picker);
                                        });
                                    };
                                } else {
                                    // No onSelect already specified so just update the model
                                    options.onSelect = updateModel;
                                }

                            options.onClose = updateModel;

                            // Update the date picker when the model changes
                            controller.$render = function () {
                                var date = controller.$viewValue;
                               
                                if (date && !angular.isDate(date) && date.length > 0)
                                    date = new Date(date);
                                if (angular.isDefined(date) && date !== null && !angular.isDate(date)) {
                                    throw new Error('ng-Model value must be a Date object - currently it is a ' + typeof date + ' - use ui-date-format to convert it from a string');
                                }

                                element.datepicker("setDate", date);
                            };
                        }
                        // If we don't destroy the old one it doesn't update properly when the config changes
                        element.datepicker('destroy');
                        // Create the new datepicker widget
                        element.datepicker(options);
                        if (controller) {
                            // Force a render to override whatever is in the input text box
                            controller.$render();
                        }
                    };
                    // Watch for changes to the directives options
                    scope.$watch(getOptions, init, true);
                }
            };
        }
    ]);