<template>
  <el-input v-if="!isInline"
    class="el-date-editor"
    :role="type==='daterange' || type==='date'?'button': undefined"
    :id="id"
    :aria-expanded="pickerVisible"
    :aria-haspopup="true"
    :aria-label="ariaLabel || t('el.datepicker.selectDateTime') "
    :class="'el-date-editor--' + type"
    :readonly="!editable || readonly"
    :disabled="disabled"
    :size="size"
    v-clickoutside="handleClose"
    :placeholder="placeholder"
    @focus="handleFocus"
    @blur="handleBlur"
    @keydown.native="handleKeydown"
    :value="displayValue"
    @change.native="displayValue = $event.target.value"
    :validateEvent="false"
    @click.native="pickerVisible = true"
    ref="reference">
    <i slot="icon"
      class="el-input__icon"
      @click="handleClickIcon"
      :class="[showClose ? 'icon-exit' : triggerClass]"
      @mouseenter="handleMouseEnterIcon"
      @mouseleave="showClose = false"
      v-if="haveTrigger">
    </i>
  </el-input>
  <div ref="reference" class="el-date-editor el-date-editor--inline" v-else>
  </div>
</template>



<script>
import Vue from 'vue';
import Clickoutside from '../../../src/utils/clickoutside';
import { formatDate, parseDate, getWeekNumber, equalDate, isDate, KEY_CODES } from './util';
import Popper from '../../../src/utils/vue-popper';
import Emitter from '../../../src/mixins/emitter';
import ElInput from '../../../packages/input';
import Locale from '../../../src/mixins/locale';

const NewPopper = {
  props: {
    appendToBody: Popper.props.appendToBody,
    offset: Popper.props.offset,
    boundariesPadding: Popper.props.boundariesPadding
  },
  methods: Popper.methods,
  data: Popper.data,
  beforeDestroy: Popper.beforeDestroy
};

const DEFAULT_FORMATS = {
  date: 'yyyy-MM-dd',
  month: 'yyyy-MM',
  datetime: 'yyyy-MM-dd HH:mm:ss',
  time: 'HH:mm:ss',
  week: 'yyyywWW',
  timerange: 'HH:mm:ss',
  daterange: 'yyyy-MM-dd',
  datetimerange: 'yyyy-MM-dd HH:mm:ss',
  year: 'yyyy'
};
const HAVE_TRIGGER_TYPES = [
  'date',
  'datetime',
  'time',
  'time-select',
  'week',
  'month',
  'year',
  'daterange',
  'timerange',
  'datetimerange'
];
const DATE_FORMATTER = function(value, format) {
  return formatDate(value, format);
};
const DATE_PARSER = function(text, format) {
  return parseDate(text, format);
};
const RANGE_FORMATTER = function(value, format, separator, isRtl) {
  if (Array.isArray(value) && value.length === 2) {
    const start = value[0];
    const end = value[1];

    if (start && end) {
      if (isRtl) {
        return formatDate(end, format) + separator + formatDate(start, format);
      } else {
        return formatDate(start, format) + separator + formatDate(end, format);
      }
    }
  }
  return '';
};
const RANGE_PARSER = function(text, format, separator) {
  const array = text.split(separator);
  if (array.length === 2) {
    const range1 = array[0];
    const range2 = array[1];

    return [parseDate(range1, format), parseDate(range2, format)];
  }
  return [];
};
const TYPE_VALUE_RESOLVER_MAP = {
  default: {
    formatter(value) {
      if (!value) return '';
      return '' + value;
    },
    parser(text) {
      if (text === undefined || text === '') return null;
      return text;
    }
  },
  week: {
    formatter(value, format) {
      let date = formatDate(value, format);
      const week = getWeekNumber(value);

      date = /WW/.test(date)
            ? date.replace(/WW/, week < 10 ? '0' + week : week)
            : date.replace(/W/, week);
      return date;
    },
    parser(text) {
      const array = (text || '').split('w');
      if (array.length === 2) {
        const year = Number(array[0]);
        const month = Number(array[1]);

        if (!isNaN(year) && !isNaN(month) && month < 54) {
          return text;
        }
      }
      return null;
    }
  },
  date: {
    formatter: DATE_FORMATTER,
    parser: DATE_PARSER
  },
  datetime: {
    formatter: DATE_FORMATTER,
    parser: DATE_PARSER
  },
  daterange: {
    formatter: RANGE_FORMATTER,
    parser: RANGE_PARSER
  },
  datetimerange: {
    formatter: RANGE_FORMATTER,
    parser: RANGE_PARSER
  },
  timerange: {
    formatter: RANGE_FORMATTER,
    parser: RANGE_PARSER
  },
  time: {
    formatter: DATE_FORMATTER,
    parser: DATE_PARSER
  },
  month: {
    formatter: DATE_FORMATTER,
    parser: DATE_PARSER
  },
  year: {
    formatter: DATE_FORMATTER,
    parser: DATE_PARSER
  },
  number: {
    formatter(value) {
      if (!value) return '';
      return '' + value;
    },
    parser(text) {
      let result = Number(text);

      if (!isNaN(text)) {
        return result;
      } else {
        return null;
      }
    }
  }
};
const PLACEMENT_MAP = {
  left: 'bottom-start',
  center: 'bottom-center',
  right: 'bottom-end'
};
const parseAsFormatAndType = (value, customFormat, type, rangeSeparator = '-') => {
  if (!value) return null;
  const parser = (
    TYPE_VALUE_RESOLVER_MAP[type] ||
    TYPE_VALUE_RESOLVER_MAP['default']
  ).parser;
  const format = customFormat || DEFAULT_FORMATS[type];
  return parser(value, format, rangeSeparator);
};
// only considers date-picker's value: Date or [Date, Date]
const valueEquals = function(a, b) {
  const aIsArray = a instanceof Array;
  const bIsArray = b instanceof Array;
  if (aIsArray && bIsArray) {
    return new Date(a[0]).getTime() === new Date(b[0]).getTime() &&
           new Date(a[1]).getTime() === new Date(b[1]).getTime();
  }
  if (!aIsArray && !bIsArray) {
    return new Date(a).getTime() === new Date(b).getTime();
  }
  return false;
};

export default {
  mixins: [Emitter, NewPopper, Locale],

  props: {
    id: String,
    size: String,
    format: String,
    readonly: Boolean,
    placeholder: String,
    disabled: Boolean,
    clearable: {
      type: Boolean,
      default: true
    },
    popperClass: String,
    editable: {
      type: Boolean,
      default: true
    },
    align: {
      type: String,
      default: 'left'
    },
    value: {},
    defaultValue: {},
    rangeSeparator: {
      default: ' - '
    },
    pickerOptions: {},
    today: String,
    ariaLabel: {
      type: String
    }
  },

  components: { ElInput },

  directives: { Clickoutside },

  data() {
    return {
      pickerVisible: false,
      showClose: false,
      currentValue: '',
      unwatchPickerOptions: null,
      localeString: ''
    };
  },

  watch: {
    pickerVisible(val) {
      if (!val) this.dispatch('ElFormItem', 'el.form.blur');
      if (this.readonly || this.disabled) return;
      val ? this.showPicker() : this.hidePicker();
    },
    currentValue(val) {
      if (val) return;
      if (this.picker && typeof this.picker.handleClear === 'function') {
        this.picker.handleClear();
      } else {
        this.$emit('input');
      }
    },
    value: {
      immediate: true,
      handler(val) {
        const localeOption = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
        if (typeof (val) === 'string' && val.indexOf('-') > 0) {
          var tmp = val.split(' ')[0];
          tmp = tmp.split('-');
          if (tmp.length === 3) {
            this.currentValue = new Date(tmp[0], tmp[1] - 1, tmp[2]);
          } else {
            this.currentValue = isDate(val) ? new Date(val) : val;
          }
        } else {
          this.currentValue = isDate(val) ? new Date(val) : val;
        }
        if (this.type === 'daterange' && val.length === 2) {
          this.localeString = `${val[0].toLocaleDateString(this.locale, localeOption)} ~ ${val[1].toLocaleDateString(this.locale, localeOption)}`;
        } else if (this.type === 'date' && val) {
          this.localeString = `${val.toLocaleDateString(this.locale, localeOption)}`;
        }
      }
    },
    displayValue(val) {
      this.$emit('change', val);
      this.dispatch('ElFormItem', 'el.form.change');
    }
  },

  computed: {
    rtl() {
      return document.dir === 'rtl';
    },
    reference() {
      if (this.isInline) {
        return this.$refs.reference;
      } else {
        return this.$refs.reference.$el;
      }
    },

    refInput() {
      if (this.reference) return this.reference.querySelector('input');
      return {};
    },

    valueIsEmpty() {
      const val = this.currentValue;
      if (Array.isArray(val)) {
        for (let i = 0, len = val.length; i < len; i++) {
          if (val[i]) {
            return false;
          }
        }
      } else {
        if (val) {
          return false;
        }
      }
      return true;
    },

    triggerClass() {
      return this.type.indexOf('time') !== -1 ? 'icon-mds-arrow-down_bold' : 'icon-arrow-down';
    },

    selectionMode() {
      if (this.type === 'week') {
        return 'week';
      } else if (this.type === 'month') {
        return 'month';
      } else if (this.type === 'year') {
        return 'year';
      }

      return 'day';
    },

    haveTrigger() {
      if (typeof this.showTrigger !== 'undefined') {
        return this.showTrigger;
      }
      return HAVE_TRIGGER_TYPES.indexOf(this.type) !== -1;
    },

    displayValue: {
      get() {
        const value = this.currentValue;
        if (!value) return;
        const formatter = (
          TYPE_VALUE_RESOLVER_MAP[this.type] ||
          TYPE_VALUE_RESOLVER_MAP['default']
        ).formatter;
        const format = DEFAULT_FORMATS[this.type];

        return formatter(value, this.format || format, this.rangeSeparator, this.rtl);
      },

      set(value) {
        if (value) {
          const type = this.type;
          const parser = (
            TYPE_VALUE_RESOLVER_MAP[type] ||
            TYPE_VALUE_RESOLVER_MAP['default']
          ).parser;
          const parsedValue = parser(value, this.format || DEFAULT_FORMATS[type], this.rangeSeparator);

          if (parsedValue && this.picker) {
            this.picker.value = parsedValue;
          }
        } else {
          this.$emit('input', value);
          this.picker.value = value;
        }
        this.$forceUpdate();
      }
    },

    isInline() {
      return this.type === 'inline-date';
    }
  },

  created() {
    // vue-popper
    this.popperOptions = {
      boundariesPadding: 0,
      gpuAcceleration: false
    };
    this.placement = PLACEMENT_MAP[this.align] || PLACEMENT_MAP.left;
  },

  mounted() {
    if (this.isInline) {
      this.showPicker();
    }
  },

  methods: {
    handleMouseEnterIcon() {
      if (this.readonly || this.disabled) return;
      if (!this.valueIsEmpty && this.clearable) {
        this.showClose = true;
      }
    },

    handleClickIcon() {
      if (this.readonly || this.disabled) return;
      if (this.showClose) {
        this.currentValue = this.$options.defaultValue || '';
        this.showClose = false;
      } else {
        this.pickerVisible = !this.pickerVisible;
      }
    },

    dateChanged(dateA, dateB) {
      if (Array.isArray(dateA)) {
        let len = dateA.length;
        if (!dateB) return true;
        while (len--) {
          if (!equalDate(dateA[len], dateB[len])) return true;
        }
      } else {
        if (!equalDate(dateA, dateB)) return true;
      }

      return false;
    },

    handleClose() {
      this.pickerVisible = false;
    },

    handleFocus() {
      if (this._inhidden) {
        this._inhidden = false;
        return;
      }
      this.$emit('focus', this);
    },

    handleBlur() {
      this.$emit('blur', this);
    },

    handleKeydown(event) {
      const keyCode = event.keyCode;

      // ESC
      if (keyCode === 27) {
        this.pickerVisible = false;
        event.stopPropagation();
        return;
      }

      // // Tab
      // if (keyCode === 9) {
      //   if (this.pickerVisible) {
      //     this.picker.focus(event);
      //   }
      //   return;
      // }

      // Enter
      if (keyCode === KEY_CODES.ENTER || keyCode === KEY_CODES.SPACE) {
        event.preventDefault();
        if (!this.pickerVisible) {
          this.pickerVisible = true;
          event.stopPropagation();
          return;
        }
        if (this.userInput === '' || this.isValidValue(this.parseString(this.displayValue))) {
          this.handleChange();
          this.pickerVisible = this.picker.visible = false;
          this.handleBlur();
        }
        event.stopPropagation();
        return;
      }

      // if user is typing, do not let picker handle key input
      if (this.userInput) {
        event.stopPropagation();
        return;
      }
      if (!this.pickerVisible) {
        if (+keyCode === 40) {
          this.pickerVisible = true;
          this.picker.visible = true;
          event.stopPropagation();
        }
      }
      // delegate other keys to panel
      if (this.picker && this.picker.handleKeydown) {
        // this.picker.handleKeydown(event);
      }

    },
    handleChange() {
      if (this.userInput) {
        const value = this.parseString(this.displayValue);
        if (value) {
          this.picker.value = value;
          if (this.isValidValue(value)) {
            this.emitInput(value);
            this.userInput = null;
          }
        }
      }
      if (this.userInput === '') {
        this.emitInput(null);
        this.emitChange(null);
        this.userInput = null;
      }
    },
    focusPicker(e) {
      if (this.picker.focus) {
        this.picker.focus(e);
      }
    },
    hidePicker() {
      // console.log('enter hide picker proc!');
      if (this.picker) {
        this.picker.resetView && this.picker.resetView();
        this.picker.resetDate && this.picker.resetDate();
        this.pickerVisible = this.picker.visible = false;
        this.destroyPopper();
      }
    },

    showPicker() {
      const picker = this.picker;
      if (this.$isServer) return;
      if (!picker) {
        this.mountPicker();
      }
      this.pickerVisible = this.picker.visible = true;
      if (!this.isInline) {
        this.updatePopper();
      }
      if (this.currentValue instanceof Date) {
        this.picker.date = new Date(this.currentValue.getTime());
      } else {
        this.picker.value = this.currentValue;
      }
      this.picker.resetView && this.picker.resetView();
      this.referenceElm && this.referenceElm.querySelector('input').blur();
      this.$nextTick(() => {
        this.picker.$el.focus();
      });
    },

    mountPicker() {
      this.panel.defaultValue = this.defaultValue || this.currentValue;
      this.panel.isInline = this.isInline;
      this.picker = new Vue(this.panel).$mount();
      this.picker.locale = this.locale;
      this.picker.popperClass = this.popperClass;
      this.popperElm = this.picker.$el;
      this.picker.isInline = this.isInline;
      if (!this.isInline) {
        this.picker.width = this.reference.getBoundingClientRect().width;
      }
      this.picker.showTime = this.type === 'datetime' || this.type === 'datetimerange';
      this.picker.selectionMode = this.selectionMode;
      if (this.format) {
        this.picker.format = this.format;
      }
      if (this.today) {
        this.picker.today = this.today;
      }

      const updateOptions = () => {
        const options = this.pickerOptions;

        if (options && options.selectableRange) {
          let ranges = options.selectableRange;
          const parser = TYPE_VALUE_RESOLVER_MAP.datetimerange.parser;
          const format = DEFAULT_FORMATS.timerange;

          ranges = Array.isArray(ranges) ? ranges : [ranges];
          this.picker.selectableRange = ranges.map(range => parser(range, format, this.rangeSeparator));
        }

        for (const option in options) {
          if (options.hasOwnProperty(option) &&
              // 忽略 time-picker 的该配置项
              option !== 'selectableRange') {
            this.picker[option] = options[option];
          }
        }
      };
      updateOptions();
      this.unwatchPickerOptions = this.$watch('pickerOptions', () => updateOptions(), { deep: true });

      this.$el.appendChild(this.picker.$el);
      this.picker.resetView && this.picker.resetView();
      let me = this;
      this.picker.$on('dodestroy', this.doDestroy);
      this.picker.$on('pick', (date = '', visible = false, isClick) => {
        // do not emit if values are same
        // console.log('has pick a date:' + date);
        if (!valueEquals(this.value, date)) {
          this.$emit('input', date);
        }
        if (!this.isInline) {
          if (this.type !== 'daterange') {
            this.picker.date = date;
            if (isClick) {
              me.pickerVisible = false;
            }
          } else {
            me.pickerVisible = me.picker.visible = visible;
            me.picker.resetView && me.picker.resetView();
          }
        }
        if (!valueEquals(this.value, date)) {
          this.$emit('input', date);
        }
        let input = this.reference.querySelector('input');
        if (!me.pickerVisible) {
          input.focus();
        }
        this.$nextTick(()=>{
          if (this.isInline) {
            this.picker.date = date;
          }
        });
      });
      this.picker.$on('blur', () => {
        setTimeout(()=>{
          if (!this.isInline) {
            this.pickerVisible = me.picker.visible = false;
            this._inhidden = true;
            this.reference.querySelector('input').focus();
          } else {
            if (this.$parent.showPopper) {
              this.$parent.showPopper = false;
            }
          }
        }, 100);
      });
      this.picker.$on('select-range', (start, end) => {
        this.refInput.setSelectionRange(start, end);
        this.refInput.focus();
      });
    },

    unmountPicker() {
      if (this.picker) {
        this.picker.$destroy();
        this.picker.$off();
        if (typeof this.unwatchPickerOptions === 'function') {
          this.unwatchPickerOptions();
        }
        this.picker.$el.parentNode.removeChild(this.picker.$el);
      }
    },

    isValidValue(value) {
      if (!this.picker) {
        this.mountPicker();
      }
      if (this.picker.isValidValue) {
        return value && this.picker.isValidValue(value);
      } else {
        return true;
      }
    },
    emitChange(val) {
      // determine user real change only
      if (!valueEquals(val, this.valueOnOpen)) {
        this.$emit('change', val);
        this.dispatch('ElFormItem', 'el.form.change', val);
        this.valueOnOpen = val;
      }
    },

    emitInput(val) {
      const formatted = this.formatToValue(val);
      if (!valueEquals(this.value, formatted)) {
        this.$emit('input', formatted);
      }
    },
    parseString(value) {
      const type = Array.isArray(value) ? this.type : this.type.replace('range', '');
      return parseAsFormatAndType(value, this.format, type);
    },
    getLocaleString() {
      return this.localeString;
    }
  }
};
</script>
