<template>
  <div class="obow" @click="focus">
    <span class="word-box"
      :class="{
        'right-focus': focusing && focusingIndex === index,
        'select-word': focusing && focusingIndex === index && item
      }"
      v-for="(item, index) in chartArray"
      :key="index"
      @click="clickFocus(index)">{{item}}</span>
    <input class="hidden-input"
      v-model="inputTemp"
      @input="setInputIntoArray"
      @keydown="onKeyDown($event)"
      @blur="focusing = false" ref="hInput">
  </div>
</template>
<script>

export default {
  name: 'InputOneByOne',
  props: {
    boxNum: Number,
    value: String
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  data () {
    return {
      inputTemp: '',
      chartArray: [],
      focusing: false,
      focusingIndex: 0
    }
  },
  watch: {
    'chartArray': function () {
      const charts = this.chartArray.join('')
      const reg = /^[0-9]+(.[0-9]{1,2})?$/g
      const numCharts = charts.match(reg)
      if (numCharts) {
        this.$emit('change', numCharts.join(''))
      } else {
        this.$emit('change', '')
      }
    }
  },
  mounted () {
    this.chartArray = new Array(this.boxNum)
  },
  methods: {
    setInputIntoArray () {
      if (!this.inputTemp) return
      const reg = /[0-9]|\./g
      const numCharts = this.inputTemp.match(reg)
      if (numCharts) {
        const replaceCounts = numCharts.length < this.boxNum - this.focusingIndex ? numCharts.length : this.boxNum - this.focusingIndex
        this.chartArray.splice(this.focusingIndex, replaceCounts, ...numCharts.slice(0, replaceCounts))
        this.focusingIndex += replaceCounts
      }
      this.inputTemp = ''
    },
    focus () {
      let input = this.$refs.hInput
      input.focus()
      this.focusing = true
    },
    clickFocus (index) {
      this.focusingIndex = index
      let input = this.$refs.hInput
      input.focus()
      this.focusing = true
    },
    onKeyDown (event) {
      let keycode = event.keyCode || event.which
      switch (keycode) {
        // end
        case 35:
          this.focusingIndex = this.boxNum - 1
          break
        case 36:
          this.focusingIndex = 0
          break
        // left
        case 37:
          this.focusingIndex--
          break
        // right
        case 39:
          this.focusingIndex++
          break
        // backspace
        case 8:
          this.backspaceHandle()
          break
      }
      if (this.focusingIndex < 0) {
        this.focusingIndex = 0
      }
      if (this.focusingIndex >= this.boxNum) {
        this.focusingIndex = this.boxNum - 1
      }
    },
    backspaceHandle () {
      let i = this.focusingIndex
      if (i < 0) {
        this.focusingIndex = 0
        return
      }
      if (this.chartArray[i]) {
        // this.chartArray.replaceElem(i, undefined)
        this.chartArray[i] = undefined
      } else {
        this.focusingIndex--
        // this.chartArray.replaceElem(--i, undefined)
        this.chartArray[--i] = undefined
      }
    },
    clearCharts () {
      this.chartArray = new Array(this.boxNum)
    }
  }
}
</script>
<style lang="less">
.obow {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  user-select: none;
  .word-box {
    display: inline-block;
    font-size: 32px;
    text-align: center;
    background: transparent;
    border-top: none;
    border-left: none;
    border-right: none;
    border-bottom: solid 1px #CDDBEE;
    outline: none;
    margin-left: 10px;
    width: 48px;
    height: 48px;
    cursor: text;
    font-weight: bold;
  }
  .word-box:nth-child(3n+1) {
    margin-left: 23px;
  }
  .word-box:nth-of-type(1) {
    margin-left: 0;
  }
  .select-word {
    color: white;
    background: rgb(49, 142, 254)
  }
  .right-focus {
    box-shadow: 0px 0px 16px 0px #cddbee;
  }
  .right-focus::after {
    content: '';
    border-left: 1px solid black;
    margin-left: 2px;
    animation: 1s blink step-end infinite;
  }
  .hidden-input {
    opacity: 0;
    width: 0;
    height: 0;
  }
  .debug-input {
    width: 400px;
  }
}
@keyframes blink {
  from, to {
    border-left: none;
  }
  50% {
    border-left: 1px solid black;
  }
}
</style>
