<template>
  <div
    class="el-slider"
    :class="{ 'is-vertical': vertical, 'el-slider--with-input': showInput }"
    role="slider"
    :aria-valuemin="min"
    :aria-valuemax="max"
    :aria-orientation="vertical ? 'vertical': 'horizontal'"
    :aria-disabled="sliderDisabled"
  >
    <el-input-number
      v-model="firstValue"
      v-if="showInput && !range"
      class="el-slider__input"
      ref="input"
      @change="$nextTick(emitChange)"
      :step="step"
      :disabled="sliderDisabled"
      :controls="enumVals.length > 0 ? false : showInputControls"
      :min="minLegal"
      :max="maxLegal"
      :debounce="debounce"
      :size="inputSize">
    </el-input-number>
    <div
      class="el-slider__runway"
      :class="{ 'show-input': showInput, 'disabled': sliderDisabled }"
      :style="runwayStyle"
      @click="onSliderClick"
      ref="slider">
      <div
        class="el-slider__bar"
        :style="barStyle">
      </div>
      <slider-button
        :vertical="vertical"
        v-model="firstValue"
        :tooltip-class="tooltipClass"
        ref="button1">
      </slider-button>
      <slider-button
        :vertical="vertical"
        v-model="secondValue"
        :tooltip-class="tooltipClass"
        ref="button2"
        v-if="range">
      </slider-button>
      <div
        class="el-slider__stop"
        v-for="(item, key) in stops"
        :key="key"
        :style="vertical ? { 'bottom': item + '%' } : { 'left': item + '%' }"
        v-if="showStops">
      </div>
      <div
        class="el-slider__ruler"
        v-for="item in ruler"
        :style="vertical ? { 'bottom': item.pos + '%' } : { 'left': item.pos + '%' }"
        :class="{ 'leftside': firstValue >= item.value }"
        @click.stop="onRulerClick(item)"
        v-if="ruler.length > 0">
        <span>{{ item.value + ' ' + item.unit }}</span>
      </div>
    </div>
  </div>
</template>

<script type="text/babel">
  import ElInputNumber from 'element-ui/packages/input-number';
  import SliderButton from './button.vue';
  import Emitter from 'element-ui/src/mixins/emitter';

  export default {
    name: 'ElSlider',

    mixins: [Emitter],

    inject: {
      elForm: {
        default: ''
      }
    },

    props: {
      min: {
        type: Number,
        default: 0
      },
      max: {
        type: Number,
        default: 100
      },
      step: {
        type: Number,
        default: 1
      },
      value: {
        type: [Number, Array],
        default: 0
      },
      showInput: {
        type: Boolean,
        default: false
      },
      showInputControls: {
        type: Boolean,
        default: true
      },
      inputSize: {
        type: String,
        default: 'small'
      },
      showStops: {
        type: Boolean,
        default: false
      },
      showTooltip: {
        type: Boolean,
        default: true
      },
      formatTooltip: Function,
      disabled: {
        type: Boolean,
        default: false
      },
      range: {
        type: Boolean,
        default: false
      },
      vertical: {
        type: Boolean,
        default: false
      },
      height: {
        type: String
      },
      debounce: {
        type: Number,
        default: 300
      },
      ruler: {
        type: Array,
        default: function _default() {
          return [];
        }
      },
      enumVals: {
        type: Array,
        default: function _default() {
          return [];
        }
      },
      label: {
        type: String
      },
      tooltipClass: String
    },

    components: {
      ElInputNumber,
      SliderButton
    },

    data() {
      return {
        firstValue: null,
        secondValue: null,
        oldValue: null,
        dragging: false,
        sliderSize: 1,
        button1Percent: '0%',
        enumMin: null,
        enumMax: null,
        enumCalcStep: null
      };
    },

    watch: {
      value(val, oldVal) {
        if (this.dragging ||
          Array.isArray(val) &&
          Array.isArray(oldVal) &&
          val.every((item, index) => item === oldVal[index])) {
          return;
        }
        this.setValues();
      },

      dragging(val) {
        if (!val) {
          this.setValues();
        }
      },

      firstValue(val) {
        if (this.range) {
          this.$emit('input', [this.minValue, this.maxValue]);
        } else if (this.enumVals.length > 1) {
          var idx = this.enumVals.indexOf(val);
          if (idx >= 0) {
            this.$emit('input', val);
          } else {
            for (var i = 0; i < this.enumVals.length - 1; i++) {
              if (val > this.enumVals[i] && val < this.enumVals[i + 1]) {
                var leftDist = val - this.enumVals[i];
                var rightDist = this.enumVals[i + 1] - val;
                idx = leftDist > rightDist ? i + 1 : i;
                this.$refs.input.currentValue = this.enumVals[idx];
                this.$emit('input', this.enumVals[idx]);
                break;
              }
            }
          }
        } else {
          this.$emit('input', val);
        }
      },

      secondValue() {
        if (this.range) {
          this.$emit('input', [this.minValue, this.maxValue]);
        }
      },

      minLegal() {
        this.setValues();
      },
      maxLegal() {
        this.setValues();
      }
    },

    methods: {
      valueChanged() {
        if (this.range) {
          return ![this.minValue, this.maxValue]
            .every((item, index) => item === this.oldValue[index]);
        } else {
          return this.value !== this.oldValue;
        }
      },
      setValues() {
        if (this.min > this.max) {
          console.error('[Element Error][Slider]min should not be greater than max.');
          return;
        }
        const val = this.value;
        if (this.enumVals && this.enumVals.length > 1) {
          this.enumMin = this.enumVals[0];
          this.enumMax = this.enumVals[this.enumVals.length - 1];
          this.enumCalcStep = (this.maxLegal - this.minLegal) / (this.enumVals.length - 1);
        }
        if (this.range && Array.isArray(val)) {
          if (val[1] < this.minLegal) {
            this.$emit('input', [this.minLegal, this.minLegal]);
          } else if (val[0] > this.maxLegal) {
            this.$emit('input', [this.maxLegal, this.maxLegal]);
          } else if (val[0] < this.minLegal) {
            this.$emit('input', [this.minLegal, val[1]]);
          } else if (val[1] > this.maxLegal) {
            this.$emit('input', [val[0], this.maxLegal]);
          } else {
            this.firstValue = val[0];
            this.secondValue = val[1];
            if (this.valueChanged()) {
              this.dispatch('ElFormItem', 'el.form.change', [this.minValue, this.maxValue]);
              this.oldValue = val.slice();
            }
          }
        } else if (!this.range && typeof val === 'number' && !isNaN(val)) {
          if (val < this.minLegal) {
            this.$emit('input', this.minLegal);
          } else if (val > this.maxLegal) {
            this.$emit('input', this.maxLegal);
          } else {
            this.firstValue = val;
            if (this.valueChanged()) {
              this.dispatch('ElFormItem', 'el.form.change', val);
              this.oldValue = val;
            }
          }
        }
      },

      setPosition(percent) {
        const targetValue = this.minLegal + percent * (this.maxLegal - this.minLegal) / 100;
        if (!this.range) {
          this.$refs.button1.setPosition(percent);
          return;
        }
        let button;
        if (Math.abs(this.minValue - targetValue) < Math.abs(this.maxValue - targetValue)) {
          button = this.firstValue < this.secondValue ? 'button1' : 'button2';
        } else {
          button = this.firstValue > this.secondValue ? 'button1' : 'button2';
        }
        this.$refs[button].setPosition(percent);
      },

      onSliderClick(event) {
        if (this.sliderDisabled || this.dragging) return;
        this.resetSize();
        if (this.vertical) {
          const sliderOffsetBottom = this.$refs.slider.getBoundingClientRect().bottom;
          this.setPosition((sliderOffsetBottom - event.clientY) / this.sliderSize * 100);
        } else {
          const sliderOffsetLeft = this.$refs.slider.getBoundingClientRect().left;
          this.setPosition((event.clientX - sliderOffsetLeft) / this.sliderSize * 100);
        }
        this.emitChange();
      },
      onRulerClick(clickRuler) {
        if (this.disabled || this.dragging) return;
        this.resetSize();
        this.setPosition(clickRuler.pos);
      },
      resetSize() {
        if (this.$refs.slider) {
          this.sliderSize = this.$refs.slider[`client${ this.vertical ? 'Height' : 'Width' }`];
        }
      },

      emitChange() {
        this.$nextTick(() => {
          this.$emit('change', this.range ? [this.minValue, this.maxValue] : this.value);
        });
      }
    },

    computed: {
      stops() {
        if (!this.showStops || this.min > this.max) return [];
        if (this.stepFinal === 0) {
          process.env.NODE_ENV !== 'production' &&
          console.warn('[Element Warn][Slider]step should not be 0.');
          return [];
        }
        const stopCount = (this.maxLegal - this.minLegal) / this.stepFinal;
        const stepWidth = 100 * this.stepFinal / (this.maxLegal - this.minLegal);
        const result = [];
        for (let i = 1; i < stopCount; i++) {
          result.push(i * stepWidth);
        }
        if (this.range) {
          return result.filter(step => {
            return step < 100 * (this.minValue - this.minLegal) / (this.maxLegal - this.minLegal) ||
                    step > 100 * (this.maxValue - this.minLegal) / (this.maxLegal - this.minLegal);
            // return step < 100 * (this.minValue - this.min) / (this.max - this.min) ||
            //   step > 100 * (this.maxValue - this.min) / (this.max - this.min);
          });
        } else {
          return result.filter(step => {
            return step > 100 * (this.firstValue - this.minLegal) / (this.maxLegal - this.minLegal);
          });
        }
      },

      minValue() {
        return Math.min(this.firstValue, this.secondValue);
      },

      maxValue() {
        return Math.max(this.firstValue, this.secondValue);
      },
      minLegal: function minLegal() {
        return this.enumMin !== null && this.enumMin !== undefined && !isNaN(this.enumMin) ? this.enumMin : this.min;
      },
      maxLegal: function maxLegal() {
        return this.enumMax !== null && this.enumMax !== undefined && !isNaN(this.enumMax) ? this.enumMax : this.max;
      },
      stepFinal: function stepFinal() {
        return this.enumCalcStep !== null && this.enumCalcStep !== undefined && !isNaN(this.enumCalcStep) ? this.enumCalcStep : this.step;
      },
      barSize() {
        // return this.range
        //   ? `${ 100 * (this.maxValue - this.minValue) / (this.max - this.min) }%`
        //   : `${ 100 * (this.firstValue - this.min) / (this.max - this.min) }%`;
        if (this.enumVals.length > 1) {
          const idx = this.enumVals.indexOf(this.firstValue);
          return 100 / (this.enumVals.length - 1) * idx + '%';
        }
        if (this.ruler.length > 0) {
          if (!this.firstValue || this.firstValue < 0) return '0%';
          if (this.firstValue <= this.minLegal) return '0%';
          if (this.firstValue >= this.maxLegal) return '100%';
          let curValue = void 0;
          let nextValue = void 0;
          for (let i = 0, len = this.ruler.length; i < len; i++) {
            curValue = this.ruler[i].value;
            nextValue = this.ruler[i + 1].value;
            if (this.firstValue >= curValue && this.firstValue < nextValue) {
              const pert = (i + (this.firstValue - curValue) / (nextValue - curValue)) * (100 / (this.ruler.length - 1));
              return pert + '%';
            }
          }
          return this.range ? 100 * (this.maxValue - this.minValue) / (this.maxLegal - this.minLegal) + '%' : 100 * (this.firstValue - this.minLegal) / (this.maxLegal - this.minLegal) + '%';
        }
        return this.range ? 100 * (this.maxValue - this.minValue) / (this.maxLegal - this.minLegal) + '%' : 100 * (this.firstValue - this.minLegal) / (this.maxLegal - this.minLegal) + '%';
      },

      barStart() {
        // return this.range
        //   ? `${ 100 * (this.minValue - this.min) / (this.max - this.min) }%`
        //   : '0%';
        return this.range ? 100 * (this.minValue - this.minLegal) / (this.maxLegal - this.minLegal) + '%' : '0%';
      },

      precision() {
        let precisions = [this.minLegal, this.maxLegal, this.stepFinal].map(item => {
          let decimal = ('' + item).split('.')[1];
          return decimal ? decimal.length : 0;
        });
        return Math.max.apply(null, precisions);
      },

      runwayStyle() {
        return this.vertical ? { height: this.height } : {};
      },

      barStyle() {
        return this.vertical
          ? {
            height: this.barSize,
            bottom: this.barStart
          } : {
            width: this.barSize,
            left: this.barStart
          };
      },

      sliderDisabled() {
        return this.disabled || (this.elForm || {}).disabled;
      }
    },

    mounted() {
      let valuetext;
      if (this.enumVals && this.enumVals.length > 1) {
        this.enumMin = this.enumVals[0];
        this.enumMax = this.enumVals[this.enumVals.length - 1];
        this.enumCalcStep = (this.maxLegal - this.minLegal) / (this.enumVals.length - 1);
      }
      if (this.range) {
        if (Array.isArray(this.value)) {
          this.firstValue = Math.max(this.minLegal, this.value[0]);
          this.secondValue = Math.min(this.maxLegal, this.value[1]);
        } else {
          this.firstValue = this.minLegal;
          this.secondValue = this.maxLegal;
        }
        this.oldValue = [this.firstValue, this.secondValue];
        valuetext = `${this.firstValue}-${this.secondValue}`;
      } else {
        if (typeof this.value !== 'number' || isNaN(this.value)) {
          this.firstValue = this.minLegal;
        } else {
          this.firstValue = Math.min(this.maxLegal, Math.max(this.minLegal, this.value));
        }
        this.oldValue = this.firstValue;
        valuetext = this.firstValue;
      }
      this.$el.setAttribute('aria-valuetext', valuetext);

      // label screen reader
      this.$el.setAttribute('aria-label', this.label ? this.label : `slider between ${this.min} and ${this.max}`);

      this.resetSize();
      window.addEventListener('resize', this.resetSize);
    },

    beforeDestroy() {
      window.removeEventListener('resize', this.resetSize);
    }
  };
</script>
