<template>
  <div :id="id" :class="className" :style="{height:height,minWidth:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    title: {
      type: String,
      default: 'line'
    },
    width: {
      type: String,
      default: '500px'
    },
    height: {
      type: String,
      default: '400px'
    },
    lineData: {
      type: Object,
      default: () => {
        return {
          xAxis: [],
          series: []
        }
      }
    },
    description: {
      type: String,
      default: ''
    },
    config: {
      type: Object,
      default: () => {
        return {}
      }
    },
    format: {
      type: String,
      default: 'HH:mm:ss'
    },
    step: {
      type: Boolean,
      default: false
    },
    functionList: {
      type: Array,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    lineData: function(val) {
      this.updateChart(val)
    },
    functionList: function(val) {
      const series = []
      this.lineData.xAxis.forEach(i => {
        let sum = 0
        val.forEach(v => {
          const { amplitude, frequency, phase } = v.func
          sum += this.func(i, amplitude, frequency, phase)
        })
        series.push(sum)
      })
      this.secondChart(series)
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    func(x, amplitude = 1, frequency = 1, phase = 0) {
      return amplitude * Math.cos(2 * Math.PI * frequency * x + phase * Math.PI)
    },
    dispatch(action) {
      this.chart.dispatchAction(action)
    },
    secondChart(chart) {
      this.chart.setOption({
        xAxis: {
          data: this.lineData.xAxis
        },
        legend: {
          data: ['时序数据', '拟合数据']
        },
        series: [
          {
            type: 'line',
            showSymbol: false,
            name: '时序数据',
            clip: true,
            data: this.lineData.series.map(v => this.strip(v))
          },
          {
            type: 'line',
            showSymbol: false,
            name: '拟合数据',
            clip: true,
            data: chart
          }
        ]
      })
    },
    updateChart(data) {
      this.chart.setOption({
        legend: {
          data: ['时序数据']
        },
        xAxis: {
          data: data.xAxis
        },
        series: [{
          type: 'line',
          showSymbol: false,
          name: '时序数据',
          clip: true,
          data: data.series.map(v => this.strip(v))
        }]
      })
    },
    strip(num, precision = 12) {
      return +parseFloat(num.toPrecision(precision))
    },
    initChart() {
      function strip(num, precision = 12) {
        return +parseFloat(num.toPrecision(precision))
      }
      const lineData = {
        xAxis: [],
        series: []
      }
      for (let i = 0; i < 1; i += 0.001) {
        const number = strip(i)
        lineData.xAxis.push(number)
        lineData.series.push(Math.sin(2 * Math.PI * number))
      }
      this.chart = echarts.init(document.getElementById(this.id))
      let config = {
        tooltip: {},
        legend: {
          data: ['时序数据']
        },
        xAxis: {
          data: []
        },
        yAxis: {},
        series: [{
          type: 'line',
          name: '时序数据',
          data: []
        }]
      }
      config = {
        ...config,
        ...this.config
      }

      this.chart.setOption(config)
      this.chart.on('datazoom', (val) => {
        const params = {
          type: 'timeDomainLine',
          val: val.batch[0]
        }
        this.$emit('datazoom', params)
      })
    }
  }
}
</script>
