<template>
  <div class="kline-wrapper" :style="{ height: height }">
    <div ref="chartContainer" class="kline-chart"></div>
  </div>
</template>

<script setup>
import { onMounted, onBeforeUnmount, ref, watch, nextTick, toRaw, defineExpose } from 'vue'
import * as klinecharts from 'klinecharts'

const props = defineProps({
  data: { type: Array, default: () => [] },
  height: { type: String, default: '100%' },
  precision: { type: Object, default: () => ({ price: 2, volume: 2 }) },
  colors: {
    type: Object,
    default: () => ({
      up: '#2EBD85',
      down: '#F6465D',
      grid: '#F2F4F6',
      text: '#929AA5',
      targetLine: '#4A6EF5'
    })
  }
})

const chartContainer = ref(null)
let chartInstance = null

const initChart = () => {
  if (!chartContainer.value) return
  if (chartInstance) klinecharts.dispose(chartContainer.value)

  chartInstance = klinecharts.init(chartContainer.value)

  // 设置精度
  const { price, volume } = props.precision
  if (chartInstance.setPriceVolumePrecision) {
    chartInstance.setPriceVolumePrecision(price, volume)
  } else if (chartInstance.setPrecision) {
    chartInstance.setPrecision(price, volume)
  }

  // 样式配置
  const { up, down, grid, text, targetLine } = props.colors
  chartInstance.setStyleOptions({
    grid: { show: true, horizontal: { show: true, size: 1, color: grid, style: 'dash', dashValue: [5, 5] }, vertical: { show: false } },
    candle: {
      type: 'candle_solid',
      bar: { upColor: up, downColor: down, noChangeColor: up },
      priceMark: {
        show: true,
        last: { show: true, upColor: up, downColor: down, line: { show: true, style: 'dash' }, text: { show: true, color: '#FFF', paddingLeft: 4, paddingRight: 4, borderRadius: 2 } }
      },
      tooltip: { showRule: 'follow_cross', showType: 'rect', dataSource: 'none' }
    },
    xAxis: { axisLine: { show: false }, tickLine: { show: false }, tickText: { color: text, size: 10, paddingTop: 8 } },
    yAxis: { inside: true, axisLine: { show: false }, tickLine: { show: false }, tickText: { color: text, size: 10, paddingLeft: 8 } },
  })

  // 默认加载 MA 和 VOL
  chartInstance.createTechnicalIndicator('MA', false, { id: 'candle_pane' })
  chartInstance.createTechnicalIndicator('VOL', false, { id: 'pane_1', heightRatio: 0.2 })

  // 初始加载
  if (props.data && props.data.length > 0) {
    chartInstance.applyNewData(toRaw(props.data))
  }
}

// --- 🎯 新增：暴露给父组件的方法 ---

// 1. 设置主图指标
const setMainIndicator = (name) => {
  if (!chartInstance) return
  // 移除常见主图指标
  chartInstance.removeTechnicalIndicator('candle_pane', 'MA')
  chartInstance.removeTechnicalIndicator('candle_pane', 'BOLL')

  if (name && name !== 'Hide') {
    chartInstance.createTechnicalIndicator(name, false, { id: 'candle_pane' })
  }
}

// 2. 设置副图指标
const setSubIndicator = (name) => {
  if (!chartInstance) return
  // 移除常见副图指标
  const subs = ['VOL', 'MACD', 'KDJ', 'RSI', 'WR']
  subs.forEach(s => chartInstance.removeTechnicalIndicator('pane_1', s))

  if (name && name !== 'Hide') {
    chartInstance.createTechnicalIndicator(name, false, { id: 'pane_1', heightRatio: 0.2 })
  }
}

defineExpose({
  setMainIndicator,
  setSubIndicator
})

// --- 原有逻辑保持不变 ---

watch(() => props.data, (newData) => {
  if (!chartInstance) return

  const rawData = toRaw(newData)
  const currentList = chartInstance.getDataList()

  if (rawData.length === 0) {
    chartInstance.clearData()
    return
  }

  if (currentList.length === 0) {
    chartInstance.applyNewData(rawData)
    return
  }

  const firstOld = currentList[0]
  const firstNew = rawData[0]
  if (firstOld.timestamp !== firstNew.timestamp) {
    chartInstance.applyNewData(rawData)
    return
  }

  if (rawData.length > 0) {
    const lastData = rawData[rawData.length - 1]
    chartInstance.updateData(lastData)
  }
}, { deep: true })

watch(() => props.precision, (val) => {
  if (chartInstance) {
    if (chartInstance.setPriceVolumePrecision) chartInstance.setPriceVolumePrecision(val.price, val.volume)
    else if (chartInstance.setPrecision) chartInstance.setPrecision(val.price, val.volume)
  }
}, { deep: true })

onMounted(() => {
  nextTick(() => initChart())
  window.addEventListener('resize', handleResize)
})

onBeforeUnmount(() => {
  window.removeEventListener('resize', handleResize)
  if (chartInstance) {
    klinecharts.dispose(chartContainer.value)
    chartInstance = null
  }
})

const handleResize = () => {
  if (chartInstance) chartInstance.resize()
}
</script>

<style scoped>
.kline-wrapper { width: 100%; position: relative; }
.kline-chart { width: 100%; height: 100%; }
</style>