/* globals Pikaday */
import Ember from 'ember';
import moment from 'moment';

const {
  isPresent,
  run,
  getProperties
} = Ember;

const assign = Ember.assign || Ember.merge;

export default Ember.Mixin.create({

  _options: Ember.computed('options', 'i18n', {
    get() {
      let options = this._defaultOptions();

      if (isPresent(this.get('i18n'))) {
        if(isPresent(this.get('i18n').t)) {
          options.i18n = {
            previousMonth : this.get('i18n').t('previousMonth').toString(),
            nextMonth     : this.get('i18n').t('nextMonth').toString(),
            months        : this.get('i18n').t('months').toString().split(','),
            weekdays      : this.get('i18n').t('weekdays').toString().split(','),
            weekdaysShort : this.get('i18n').t('weekdaysShort').toString().split(',')
          };
        } else {
          options.i18n = this.get('i18n');
        }
      }
      if (isPresent(this.get('position'))) {
        options.position = this.get('position');
      }
      if (isPresent(this.get('reposition'))) {
        options.reposition = this.get('reposition');
      }

      assign(options, this.get('options') || {});
      return options;
    }
  }),

  _defaultOptions() {
    const firstDay = this.get('firstDay');

    return {
      field: this.get('field'),
      container: this.get('pikadayContainer'),
      bound: this.get('pikadayContainer') ? false : true,
      onOpen: run.bind(this, this.onPikadayOpen),
      onClose: run.bind(this, this.onPikadayClose),
      onSelect: run.bind(this, this.onPikadaySelect),
      onDraw: run.bind(this, this.onPikadayRedraw),
      firstDay: (typeof firstDay !== 'undefined') ? parseInt(firstDay, 10) : 1,
      format: this.get('format') || 'DD.MM.YYYY',
      yearRange: this.determineYearRange(),
      minDate: this.get('minDate') || null,
      maxDate: this.get('maxDate') || null,
      theme: this.get('theme') || null
    };
  },

	/**
	 * When updating attrs, we need to reset some things in case they've changed.
	 * @public
	 * @memberOf {Mixins.Pikaday}
	 * @return {undefined}
	 */
  didUpdateAttrs() {
    this.setMinDate();
    this.setMaxDate();
    this.setPikadayDate();

    if (this.get('options')) {
      this._updateOptions();
    }
  },

  didRender() {
    this._super(...arguments);
    this.autoHideOnDisabled();
  },

  setupPikaday() {
    let pikaday = new Pikaday(this.get('_options'));

    this.set('pikaday', pikaday);
    this.setPikadayDate();
  },

  willDestroyElement() {
    this.get('pikaday').destroy();
  },

  setPikadayDate: function() {
    const format = 'YYYY-MM-DD';
    const value = this.get('value');

    if (!value) {
      this.get('pikaday').setDate(value, true);
    } else {
      const date = this.get('useUTC') ? moment(moment.utc(value).format(format), format).toDate() : value;

      this.get('pikaday').setDate(date, true);
    }
  },

  setMinDate: function() {
    const { pikaday, minDate, value } = getProperties(this, [ 'pikaday', 'minDate', 'value' ]);

    if (minDate) {
      run.later(() => {
        const _minDate = new Date(minDate.getTime());
        pikaday.setMinDate(_minDate);
      });

      // If the current date is lower than minDate we set date to minDate
      run.schedule('sync', () => {
        if (value && moment(value).isBefore(minDate, 'day')) {
          pikaday.setDate(minDate);
        }
      });
    }
  },

  setMaxDate: function() {
    const { pikaday, maxDate, value }  = getProperties(this, [ 'pikaday', 'maxDate', 'value' ]);

    if (maxDate) {
      run.later(() => {
        const _maxDate = new Date(maxDate.getTime());
        pikaday.setMaxDate(_maxDate);
      });

      // If the current date is greater than maxDate we set date to maxDate
      run.schedule('sync', () => {
        if (value > maxDate) {
          pikaday.setDate(maxDate);
        }
      });
    }
  },

  onOpen() {},
  onClose() {},
  onSelection() {},
  onDraw() {},

  onPikadaySelect: function() {
    this.userSelectedDate();
  },

  onPikadayRedraw: function() {
    this.get('onDraw')();
  },

  userSelectedDate: function() {
    var selectedDate = this.get('pikaday').getDate();

    if (this.get('useUTC')) {
      selectedDate = moment.utc([selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate()]).toDate();
    }

    this.get('onSelection')(selectedDate);
  },

  determineYearRange: function() {
    var yearRange = this.get('yearRange');

    if (yearRange) {
      if (yearRange.indexOf(',') > -1) {
        var yearArray = yearRange.split(',');

        if (yearArray[1] === 'currentYear') {
          yearArray[1] = new Date().getFullYear();
        }

        return yearArray;
      } else {
        return yearRange;
      }
    } else {
      return 10;
    }
  },

  autoHideOnDisabled() {
    if (this.get('disabled') && this.get('pikaday')) {
      this.get('pikaday').hide();
    }
  },

  _updateOptions() {
    this.get('pikaday').config(this.get('_options'));
  }
});

@@ -2121,24 +2121,48 @@
 teAttrs() %7B%0A
+    run.later(() =%3E %7B%0A  
     this.set
@@ -2168,32 +2168,34 @@
 tMinDate();%0A    
+  
 this.setMaxDate(
@@ -2197,24 +2197,26 @@
 Date();%0A    
+ 
+ 
 this.setPika
@@ -2219,32 +2219,34 @@
 PikadayDate();%0A%0A
+  
     if (this.get
@@ -2256,24 +2256,26 @@
 ptions')) %7B%0A
+  
       this._
@@ -2287,33 +2287,43 @@
 eOptions();%0A    
-%7D
+  %7D%0A    %7D);
 %0A  %7D,%0A%0A  didRend
@@ -3098,34 +3098,8 @@
 ) %7B%0A
-      run.later(() =%3E %7B%0A  
     
@@ -3144,34 +3144,32 @@
 tTime());%0A      
-  
 pikaday.setMinDa
@@ -3177,34 +3177,24 @@
 e(_minDate);
-%0A      %7D);
 %0A%0A      // I
@@ -3571,34 +3571,8 @@
 ) %7B%0A
-      run.later(() =%3E %7B%0A  
     
@@ -3621,26 +3621,24 @@
 e());%0A      
-  
 pikaday.setM
@@ -3654,26 +3654,16 @@
 axDate);
-%0A      %7D);
 %0A%0A      

Make setDate asychronous. (#149)