import { ChartBaseConfig } from './config/ChartBaseConfig';
import { DataProcessor } from './data/DataProcessor';
import { MarkerProcessor } from './markers/MarkerProcessor';
import type { StockData, DateMemo, ChartOption, SplitData } from '../types/index';
import { DrawingTools } from './drawing/DrawingTools';
import { DateMemoHandler } from './DateMemoHandler';

export class StockChart {
  private readonly dataProcessor: DataProcessor;
  private readonly markerProcessor: MarkerProcessor;
  private data: StockData[];
  private dateMemos: DateMemo[];
  private memoVisible: boolean = true;
  private readonly drawingTools: DrawingTools;
  private dateMemoHandler: DateMemoHandler;

  constructor(data: StockData[], dateMemos: DateMemo[] = [], chart: any) {
    this.data = data;
    this.dateMemos = dateMemos;
    this.dataProcessor = new DataProcessor();
    this.markerProcessor = new MarkerProcessor();
    this.drawingTools = new DrawingTools(chart);
    this.dateMemoHandler = new DateMemoHandler(dateMemos);
  }

  public createChartOption(): ChartOption {
    const processedData = this.dataProcessor.processData(this.data);
    const splitData = this.dataProcessor.splitData(processedData);
    
    const option = ChartBaseConfig.createBaseOption(splitData.categoryData);
    const chartOption = option as ChartOption;

    // 添加K线图系列
    chartOption.series.push({
      name: 'Values',
      type: 'candlestick',
      data: splitData.values
    });

    // 添加成交量系列
    chartOption.series.push({
      name: 'Volumes',
      type: 'bar',
      xAxisIndex: 1,
      yAxisIndex: 1,
      data: splitData.volumes,
      ...ChartBaseConfig.getVolumeBarStyle()
    });

    this.markerProcessor.add2OnTopOfVolumeMarkers(chartOption, splitData);
    this.markerProcessor.add3OnTopOfVolumeMarkers(chartOption, splitData);
    this.markerProcessor.addMiddleLinesToChartOption(chartOption, splitData);

    // 添加日期备注标记
    return this.dateMemoHandler.addDateMemoMarkers(chartOption, splitData);
  }

  // 切换备注显示状态
  public toggleMemoVisibility(visible: boolean): void {
    this.dateMemoHandler.toggleMemoVisibility(visible);
  }

  // 更新图表配置中的备注可见性
  public updateMemoVisibility(option: ChartOption): void {
    this.dateMemoHandler.updateMemoVisibility(option);
  }

  private tooltipFormatter(params: any[]): string {
    const param = params[0];
    if (param.seriesName === 'Values') {
      const value = param.value;
      return `${param.name}<br/>
        开: ${value[1]}<br/>
        收: ${value[2]}<br/>
        高: ${value[4]}<br/>
        低: ${value[3]}<br/>
        涨幅: ${value[5]}<br/>`;
    } else if (param.seriesName === 'Volumes') {
      return `${param.name}<br/>成交量: ${param.value[1]}`;
    }
    return '';
  }

  public getSplitData(): SplitData {
    const processedData = this.dataProcessor.processData(this.data);
    return this.dataProcessor.splitData(processedData);
  }

  public getMemoVisible(): boolean {
    return this.memoVisible;
  }

  // 添加绘图工具按钮
  public initDrawingTools(): void {
    this.drawingTools.initDrawingTools();
  }

  // 处理鼠标事件
  public handleMouseEvent(event: string, params: any): void {
    this.drawingTools.handleMouseEvent(event, params);
  }

  // 清除所有绘制的线条
  public clearDrawings(): void {
    this.drawingTools.clearMarkLine();
  }

  // 添加更新方法
  public updateData(data: StockData[], dateMemos: DateMemo[] = []): void {
    this.data = data;
    this.dateMemos = dateMemos;
    this.dateMemoHandler = new DateMemoHandler(dateMemos);
  }
} 