import React, { useMemo } from 'react';
import { BaseChart, BaseChartProps } from './BaseChart';
import type { TouchEvent, ChartsConfig, LegendConfig, XAxisConfig, YAxisConfig } from '../lib/charts/index';

/**
 * 数据点形状
 */
export type DataPointShape = 'circle' | 'rect' | 'triangle' | 'diamond';

/**
 * 折线图组件的 Props 接口
 */
export interface LineChartProps extends Omit<BaseChartProps, 'type'> {
  /** 是否显示数据点 */
  dataPointShape?: DataPointShape | boolean;
  /** 数据点大小 */
  dataPointRadius?: number;
  /** 是否启用滚动 */
  enableScroll?: boolean;
  /** X 轴配置 */
  xAxis?: XAxisConfig;
  /** Y 轴配置 */
  yAxis?: YAxisConfig;
  /** 图例是否显示 */
  legend?: boolean;
  /** 字体大小 */
  fontSize?: number;
  /** 背景颜色 */
  background?: string;
  /** 是否开启动画 */
  animation?: boolean;
  /** 是否显示数据标签 */
  dataLabel?: boolean;
  /** 额外折线图配置 */
  extra?: ChartsConfig['extra'];
  /** tooltip 格式化函数 */
  tooltipFormatter?: (item: any, category: string) => string;
}

/**
 * LineChart 折线图组件
 *
 * 用于显示趋势数据的折线图，支持数据点形状、滚动和缩放
 */
export const LineChart: React.FC<LineChartProps> = (props) => {
  const {
    dataPointShape = true,
    dataPointRadius = 4,
    enableScroll = false,
    xAxis,
    yAxis,
    legend = true,
    fontSize = 11,
    background = '#FFFFFF',
    animation = true,
    dataLabel = false,
    extra = {},
    tooltipFormatter,
    categories = [],
    series = [],
    config = {},
    ...baseProps
  } = props;

  const chartRef = React.useRef<any>(null);

  /**
   * 默认配置
   */
  const defaultConfig = useMemo(() => {
    const legendConfig: LegendConfig = legend ? { show: true } : { show: false };
    return {
      legend: legendConfig,
      fontSize,
      background,
      animation,
      dataLabel,
      dataPointShape: typeof dataPointShape === 'boolean' ? (dataPointShape ? 'circle' : false) : dataPointShape,
      dataPointRadius,
      enableScroll,
      xAxis: xAxis ?? { disableGrid: false },
      yAxis: yAxis ?? {},
      extra: {
        line: {
          type: 'curve',
          width: 2
        },
        ...extra
      }
    };
  }, [legend, fontSize, background, animation, dataLabel, dataPointShape, dataPointRadius, enableScroll, xAxis, yAxis, extra]);

  /**
   * tooltip 事件处理
   */
  const handleTouchStart = (e: TouchEvent) => {
    if (chartRef.current) {
      chartRef.current.scrollStart(e);
      if (tooltipFormatter) {
        chartRef.current.showToolTip(e, {
          formatter: tooltipFormatter
        });
      } else {
        chartRef.current.showToolTip(e, {
          formatter: (item: any, category: string) => {
            return category + ' ' + item.name + ':' + item.data;
          }
        });
      }
    }
    baseProps.onTouchStart?.(e);
  };

  /**
   * 滚动事件处理
   */
  const handleTouchMove = (e: TouchEvent) => {
    if (chartRef.current && enableScroll) {
      chartRef.current.scroll(e);
    }
    baseProps.onTouchMove?.(e);
  };

  /**
   * 滚动结束事件处理
   */
  const handleTouchEnd = (e: TouchEvent) => {
    if (chartRef.current && enableScroll) {
      chartRef.current.scrollEnd(e);
    }
    baseProps.onTouchEnd?.(e);
  };

  return (
    <BaseChart
      {...baseProps}
      categories={categories}
      series={series}
      type="line"
      config={{ ...defaultConfig, ...config }}
      onTouchStart={handleTouchStart}
      onTouchMove={handleTouchMove}
      onTouchEnd={handleTouchEnd}
    />
  );
};

export default LineChart;
