<template>
  <div class="player-dmg">
    <div id="dmg-chart-1"></div>
    <div id="dmg-chart-2"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts/core';
import {GridComponent, LegendComponent, TooltipComponent} from 'echarts/components';
import {BarChart} from 'echarts/charts';
import {CanvasRenderer} from 'echarts/renderers';
import {onMounted, onUnmounted, ref} from "vue";
import {checkStatEmpty, getPlayerArr} from "../utils";
import {useStore} from "vuex";

export default {
  name: "FlashChart",
  setup() {
    const store = useStore()

    let myChart1, myChart2
    const width = ref(window.innerWidth <= 1000 && window.innerWidth >= 751 ? window.innerWidth : 750)
    const height = ref(window.innerWidth >= 751 ? width.value * 3 / 7.5 : window.innerWidth >= 501 && window.innerWidth <= 751 ? width.value * 3 / 6.5 : width.value * 3 / 5.5)

    const dataArr = (stats, team, prop) => {
      if (['team', 'enemy', 'self'].indexOf(prop) > -1) {
        let arr = []
        for (let i = (team - 1) * 5; i < team * 5; i++) {
          arr.push({
            value: checkStatEmpty(Function('return(function(stats, i){ return stats[i].dmg.' + prop + '})')()(stats, i)) * (prop === 'enemy' ? 1 : -1),
            itemStyle: {
              color: prop === 'enemy' ? getComputedStyle(document.documentElement).getPropertyValue(`--csgo-${stats[i].color}`) : 'firebrick'
            }
          })
        }
        arr.reverse()
        return arr
      }
    }

    const optionGen = (team) => {
      return {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          show: false
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'value',
            min: -300
          }
        ],
        yAxis: [
          {
            type: 'category',
            axisTick: {
              show: false
            },
            data: getPlayerArr(store.state.matchDetails.stats, team)
          }
        ],
        series: [
          {
            name: 'Team',
            type: 'bar',
            stack: 'Total',
            label: {
              show: true,
            },
            emphasis: {
              focus: 'series'
            },
            data: dataArr(store.state.matchDetails.stats, team, 'team')
          },
          {
            name: 'Enemy',
            type: 'bar',
            stack: 'Total',
            label: {
              show: true,
              position: 'inside'
            },
            emphasis: {
              focus: 'series'
            },
            data: dataArr(store.state.matchDetails.stats, team, 'enemy')
          }
        ]
      }
    }

    const disposeCharts = () => {
      if (myChart1 != null && myChart1 !== '' && myChart1 !== undefined) {
        myChart1.dispose()
      }
      if (myChart2 != null && myChart2 !== '' && myChart2 !== undefined) {
        myChart2.dispose()
      }
    }

    const buildCharts = () => {
      disposeCharts()

      myChart1 = echarts.init(document.getElementById('dmg-chart-1'), {}, {width: width.value, height: height.value});
      myChart1.setOption(optionGen(1));

      myChart2 = echarts.init(document.getElementById('dmg-chart-2'), {}, {width: width.value, height: height.value});
      myChart2.setOption(optionGen(2));
    }

    onMounted(() => {
      if (store.state.matchDetails.stats) {
        echarts.use([
          TooltipComponent,
          GridComponent,
          LegendComponent,
          BarChart,
          CanvasRenderer
        ]);

        buildCharts()
      }
    })

    onUnmounted(() => {
      disposeCharts()
    })

    window.onresize = () => {
      if (window.innerWidth <= 750) {
        width.value = window.innerWidth - 20
        if (window.innerWidth <= 500) {
          height.value = width.value * 3 / 5.5
        }
        if (window.innerWidth <= 750 && window.innerWidth >= 501) {
          height.value = width.value * 3 / 6.5
        }
        if (window.innerWidth <= 1000 && window.innerWidth >= 751) {
          height.value = width.value * 3 / 7.5
        }
      }

      buildCharts()
    }
  }
}
</script>

<style lang="scss" scoped>
.player-dmg {
  display: flex;
  margin-bottom: 4rem;

  #dmg-chart-1,
  #dmg-chart-2 {
    flex-basis: 50%;
  }
}

@media (max-width: 1200px) {
  .player-dmg {
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
  }

}
</style>
