import type { ChartOption, SplitData } from '../../types/index';
// import type { ScatterSeriesOption } from 'echarts/types/src/chart/scatter/ScatterSeries';
// import type { LabelOption } from 'echarts/types/src/util/types';
import { CHART_COLORS } from '../constants/colors';

export class MarkerProcessor {
  private readonly operateColor = CHART_COLORS.OPERATE;
  private readonly middleLineColor = CHART_COLORS.MIDDLE_LINE;

  add2OnTopOfVolumeMarkers(option: ChartOption, data: SplitData): void {
    const markersData = data.volumes
      .map((item, index) => {
        const previousDayVolume = index > 0 ? Number(data.volumes[index - 1][1]) : 0;
        const todayVolume = Number(item[1]);
        
        if (previousDayVolume > 0 && (previousDayVolume * 1) / 2 > todayVolume) {
          return {
            value: [index, todayVolume],
            symbol: 'pin',
            symbolSize: 10,
            label: {
              show: true,
              formatter: '2',
              position: 'top',
              color: '#ffffff',
              textBorderColor: '#000',
              textBorderWidth: 2,
              fontSize: 14,
              fontWeight: 'bolder'
            },
            itemStyle: {
              color: 'transparent'
            }
          };
        }
        return null;
      })
      .filter(item => item !== null);

    option.series.push({
      name: 'SpecificMarkers2',
      type: 'scatter',
      xAxisIndex: 1,
      yAxisIndex: 1,
      data: markersData,
      tooltip: { show: false }
    } as any);
  }

  add3OnTopOfVolumeMarkers(option: ChartOption, data: SplitData): void {
    const markersData = data.volumes
      .map((item, index) => {
        const previousDayVolume = index > 0 ? Number(data.volumes[index - 1][1]) : 0;
        const todayVolume = Number(item[1]);
        const is3 = previousDayVolume > 0 && (previousDayVolume * 2) / 3 > todayVolume;
        const is2 = previousDayVolume > 0 && (previousDayVolume * 1) / 2 > todayVolume;

        if (is3 && !is2) {
          return {
            value: [index, todayVolume],
            symbol: 'pin',
            symbolSize: 10,
            label: {
              show: true,
              formatter: '3',
              position: 'top',
              color: '#ffffff',
              textBorderColor: '#000',
              textBorderWidth: 2,
              fontSize: 14,
              fontWeight: 'bolder'
            } as any,
            itemStyle: {
              color: 'transparent'
            }
          };
        }
        return null;
      })
      .filter(item => item !== null);

    option.series.push({
      name: 'SpecificMarkers3',
      type: 'scatter',
      xAxisIndex: 1,
      yAxisIndex: 1,
      data: markersData,
      tooltip: { show: false }
    } as any);
  }

  addMiddleLinesToChartOption(option: ChartOption, data: SplitData): void {
    const middleLinesData = data.values.map((item, index) => {
      const open = Number(item.value[0]);
      const close = Number(item.value[1]);
      const changePercent = Number(item.value[4]);
      const lineHeight = 0.01;
      const average = (open + close) / 2;

      if (changePercent > 3 || changePercent < -3) {
        return {
          coords: [
            [index, average - lineHeight],
            [index, average + lineHeight]
          ]
        };
      }
      return {
        coords: [
          [index, average],
          [index, average]
        ]
      };
    });

    option.series.push({
      name: 'MiddleLines',
      type: 'lines',
      coordinateSystem: 'cartesian2d',
      data: middleLinesData,
      lineStyle: {
        color: this.middleLineColor,
        width: 10,
        type: 'solid'
      },
      effect: { show: false },
      tooltip: { show: false },
      z: 3
    });
  }
} 