<template>
  <div class="system-login-wave-chart" :class="{ 'is-compact': compact }">
    <div ref="chartElement" class="chart-body" role="img" :aria-label="ariaLabel"></div>
  </div>
</template>

<script setup>
import { computed, shallowRef } from 'vue'
import { LineChart as EChartsLineChart } from 'echarts/charts'
import { GridComponent, LegendComponent, TooltipComponent } from 'echarts/components'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'

import { useEcharts } from '../../composables/useEcharts.js'
import { useThemeColors } from '../../composables/useThemeColors.js'

use([GridComponent, LegendComponent, TooltipComponent, EChartsLineChart, CanvasRenderer])

const props = defineProps({
  labels: { type: Array, required: true },
  loginUsers: { type: Array, required: true },
  interactions: { type: Array, required: true },
  compact: { type: Boolean, default: false }
})

const chartElement = shallowRef(null)
const themeColors = useThemeColors()
const chartColors = computed(() => ({
  primary: themeColors.value.chartPrimary,
  blue: themeColors.value.chartBlue
}))

const ariaLabel = computed(() =>
  props.labels.map((label, index) => (
    `${label}在线${props.loginUsers[index] || 0}人，互动${props.interactions[index] || 0}次`
  )).join('；')
)

const chartOptions = computed(() => ({
  backgroundColor: 'transparent',
  animation: true,
  animationDuration: 950,
  animationEasing: 'cubicOut',
  legend: {
    top: 0,
    left: 0,
    itemWidth: 18,
    itemHeight: 8,
    itemGap: 16,
    textStyle: {
      color: '#475569',
      fontSize: 12,
      fontWeight: 700
    }
  },
  grid: {
    top: 38,
    right: 36,
    bottom: 24,
    left: 32,
    containLabel: true
  },
  tooltip: {
    trigger: 'axis',
    confine: true,
    appendToBody: true,
    backgroundColor: 'rgba(255, 255, 255, 0.98)',
    borderColor: 'rgba(148, 163, 184, 0.24)',
    borderWidth: 1,
    padding: [9, 10],
    textStyle: {
      color: '#334155',
      fontSize: 12,
      fontWeight: 700
    },
    extraCssText: 'border-radius:4px;box-shadow:0 12px 28px rgba(15,23,42,.12);'
  },
  xAxis: {
    type: 'category',
    data: props.labels,
    boundaryGap: false,
    axisTick: { show: false },
    axisLine: { lineStyle: { color: 'rgba(148, 163, 184, 0.28)' } },
    axisLabel: {
      color: '#64748b',
      fontSize: 11,
      fontWeight: 700,
      interval: props.compact ? 2 : 1,
      hideOverlap: true
    }
  },
  yAxis: [
    {
      type: 'value',
      name: '在线',
      min: 0,
      axisLabel: {
        color: '#64748b',
        fontSize: 11,
        fontWeight: 700
      },
      nameTextStyle: { color: '#94a3b8', fontSize: 11, fontWeight: 700 },
      splitLine: { lineStyle: { color: 'rgba(226, 232, 240, 0.72)' } }
    },
    {
      type: 'value',
      name: '互动',
      min: 0,
      axisLabel: {
        color: '#94a3b8',
        fontSize: 11,
        fontWeight: 700
      },
      nameTextStyle: { color: '#94a3b8', fontSize: 11, fontWeight: 700 },
      splitLine: { show: false }
    }
  ],
  series: [
    {
      name: '在线人数',
      type: 'line',
      smooth: 0.42,
      symbol: 'circle',
      symbolSize: 7,
      data: props.loginUsers,
      lineStyle: {
        width: 3,
        color: chartColors.value.primary
      },
      itemStyle: {
        color: '#ffffff',
        borderColor: chartColors.value.primary,
        borderWidth: 2.5
      },
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            { offset: 0, color: toRgba(chartColors.value.primary, 0.18) },
            { offset: 1, color: toRgba(chartColors.value.primary, 0.02) }
          ]
        }
      }
    },
    {
      name: '互动次数',
      type: 'line',
      yAxisIndex: 1,
      smooth: 0.5,
      symbol: 'emptyCircle',
      symbolSize: 6,
      data: props.interactions,
      lineStyle: {
        width: 2.5,
        color: chartColors.value.blue,
        type: 'dashed'
      },
      itemStyle: {
        color: '#ffffff',
        borderColor: chartColors.value.blue,
        borderWidth: 2
      }
    }
  ]
}))

useEcharts(chartElement, chartOptions)

function toRgba(color, alpha) {
  const normalized = String(color || '').trim()
  const hex = normalized.replace('#', '')
  if (/^[\da-f]{6}$/i.test(hex)) {
    const r = parseInt(hex.slice(0, 2), 16)
    const g = parseInt(hex.slice(2, 4), 16)
    const b = parseInt(hex.slice(4, 6), 16)
    return `rgba(${r}, ${g}, ${b}, ${alpha})`
  }
  return `rgba(58, 124, 165, ${alpha})`
}
</script>

<style scoped>
.system-login-wave-chart {
  height: 292px;
}

.system-login-wave-chart.is-compact {
  height: 188px;
}

.chart-body {
  width: 100%;
  height: 100%;
}
</style>
