/**
 * Base.Date 1.0
 *
 * Requiere
 *     Base 1.0
 */
$(function() {

    $.extend(Base, {
        Date : {

            create: function (selector, options) {

                var _i18n = {
                    language: {}
                };
                _i18n = $.extend(true, _i18n, Base.i18n.date);
                if(options != undefined && options._i18n !== undefined && options._i18n != null){
                    _i18n = $.extend(true, _i18n, options._i18n);
                }

                //Option Convert/Compatibility
                var convertOptions = {};
                if(options != undefined){
                    if(options.startDate != undefined){
                        convertOptions.minDate = options.startDate;
                    }
                    if(options.startDate != undefined){
                        convertOptions.maxDate = options.endDate;
                    }
                    if(options.datesDisabled != undefined){
                        convertOptions.disable = options.datesDisabled;
                    }
                }


                var defaultOptions = {
                    allowInput: true,
                    enableTime: false,
                    time_24hr: false,
                    disable: [],
                    disableMobile: false,
                    dateFormat: 'd/m/Y',
                    language: 'es',
                    mode: 'single', //"single", "multiple", or "range"
                    minDate : new Date('1900-01-01'),
                    maxDate : new Date('2050-12-31')
                };

                var finallyOptions = $.extend(true, defaultOptions, options, convertOptions);

                return $(selector).flatpickr(finallyOptions);
            },

            _qxDate : function (date) {
console.log("date ", date)
                return {

                    instance : (date[0]._flatpickr),

                    ver : function () {

                        console.log("Base.Date 1.0");
                        console.log("Requiere: Base 1.0");
                    },

                    destroy: function(value){
                        this.instance.destroy();

                        if(value != undefined){
                            $(date).val(value);
                        }
                    },

                    setOption: function(option, value){

                        this.instance.set(option, value);
                    },

                    clear: function(){
                        this.instance.clear();
                    },

                    getDay: function (format) {

                        if(this.instance.selectedDates[0] == undefined){
                            return "";
                        }

                        if(format == undefined) {
                           return moment(this.instance.selectedDates[0], "L").format('dddd');
                        }

                        return moment(this.instance.selectedDates[0], "L").format(format);
                    },

                    getMonth: function (format) {

                        if(this.instance.selectedDates[0] == undefined){
                            return "";
                        }

                        if(format == undefined) {
                            return moment(this.instance.selectedDates[0], "L").format('MMMM');
                        }

                        return moment(this.instance.selectedDates[0], "L").format(format);
                    },

                    getYear: function (format) {

                        if(this.instance.selectedDates[0] == undefined){
                            return "";
                        }

                        if(format == undefined) {
                            return moment(this.instance.selectedDates[0], "L").format('yyyy');
                        }

                        return moment(this.instance.selectedDates[0], "L").format(format);
                    }
                }
            }
        }


    });

    $.fn.QxDate = function () {

        var date = this;

        return Base.Date._qxDate(date);
    }
});