// @ts-nocheck - 为保持与原始代码逻辑完全一致，跳过类型检查
/**
 * draw-charts.ts - 主绘制调度函数
 *
 * 功能：主绘制调度函数，负责协调调用各种绘制函数
 * 从 u-charts.ts 第6352行搬迁
 *
 * 注意：这是核心绘制调度函数，协调调用所有 renderers 中的具体绘制函数
 */

import type { ChartOptions } from '../data-processing/index';
import type { UChartsConfig } from '../data-processing/index';
import type { CanvasContext } from '../renderers/index';

// Data processing imports
import {
  fixPieSeries,
  fillSeries,
  calXAxisData,
  getXAxisPoints,
  calYAxisData,
  calCategoriesData
} from '../data-processing/index';

// Helper functions imports
import {
  filterSeries,
  getPieTextMaxLength,
  calLegendData
} from '../helper-functions/index';

// Math utilities
import { calCandleMA } from '../utils/math';

// Config imports
import { assign } from '../config';

// Animation imports
import { Animation, AnimationOptions } from './animation';

// Renderer imports - 所有需要的绘制函数
import {
  drawCanvas,
  drawXAxis,
  drawYAxisGrid,
  drawYAxis,
  drawLegend,
  drawToolTipBridge,
  drawMarkLine,
  contextRotate,
  drawColumnDataPoints,
  drawBarDataPoints,
  drawMountDataPoints,
  drawLineDataPoints,
  drawAreaDataPoints,
  drawCandleDataPoints,
  drawPieDataPoints,
  drawRadarDataPoints,
  drawMapDataPoints,
  drawFunnelDataPoints,
  drawWordCloudDataPoints,
  drawMixDataPoints,
  drawScatterDataPoints,
  drawBubbleDataPoints
} from '../renderers/index';

// 需要单独导入的 renderers 函数（可能未在 index.ts 中导出）
import {
  drawRoseDataPoints,
  drawGaugeDataPoints,
  drawArcbarDataPoints
} from '../renderers/pie-renderer';

/**
 * DrawChartsContext - drawCharts 函数需要的上下文
 */
export interface DrawChartsContext {
  animationInstance?: Animation;
  uevent: {
    trigger(event: string): void;
  };
  scrollOption: {
    currentOffset: number;
    startTouchX: number;
    distance: number;
    lastMoveTime: number;
  };
}

/**
 * drawCharts 函数类型
 */
export type DrawChartsFunction = (
  this: DrawChartsContext,
  type: string,
  opts: ChartOptions,
  config: UChartsConfig,
  context: CanvasContext
) => void;

/**
 * drawCharts - 主绘制调度函数
 *
 * 从 u-charts.ts 第6352行搬迁的完整实现
 * @ts-nocheck - 保持与原始代码逻辑完全一致
 */
export const drawCharts: DrawChartsFunction = function(
  type: string,
  opts: any,
  config: any,
  context: any
): void {
  const _this = this as DrawChartsContext;
  let series = opts.series;

  // 兼容ECharts饼图类数据格式
  if (type === 'pie' || type === 'ring' || type === 'mount' || type === 'rose' || type === 'funnel') {
    series = fixPieSeries(series, opts, config);
  }

  let categories = opts.categories;
  if (type === 'mount') {
    categories = [];
    for (let j = 0; j < series.length; j++) {
      if (series[j].show !== false) {
        categories.push(series[j].name);
      }
    }
    opts.categories = categories;
  }

  series = fillSeries(series, opts, config);
  const duration = opts.animation ? opts.duration : 0;
  _this.animationInstance && _this.animationInstance.stop();

  let seriesMA = null;
  if (type === 'candle') {
    const average = assign({}, opts.extra.candle.average);
    if (average.show) {
      seriesMA = calCandleMA(average.day, average.name, average.color, series[0].data);
      seriesMA = fillSeries(seriesMA, opts, config);
      opts.seriesMA = seriesMA;
    } else if (opts.seriesMA) {
      seriesMA = opts.seriesMA = fillSeries(opts.seriesMA, opts, config);
    } else {
      seriesMA = series;
    }
  } else {
    seriesMA = series;
  }

  /* 过滤掉show=false的series */
  opts._series_ = series = filterSeries(series);

  // 重新计算图表区域
  opts.area = new Array(4);
  // 复位绘图区域
  for (let j = 0; j < 4; j++) {
    opts.area[j] = opts.padding[j] * opts.pix;
  }

  // 通过计算三大区域：图例、X轴、Y轴的大小，确定绘图区域
  const _calLegendData = calLegendData(seriesMA, opts, config, opts.chartData, context);
  const legendHeight = _calLegendData.area.wholeHeight;
  const legendWidth = _calLegendData.area.wholeWidth;

  switch (opts.legend.position) {
    case 'top':
      opts.area[0] += legendHeight;
      break;
    case 'bottom':
      opts.area[2] += legendHeight;
      break;
    case 'left':
      opts.area[3] += legendWidth;
      break;
    case 'right':
      opts.area[1] += legendWidth;
      break;
  }

  let _calYAxisData = {};
  let yAxisWidth = 0;
  if (opts.type === 'line' || opts.type === 'column' || opts.type === 'mount' ||
      opts.type === 'area' || opts.type === 'mix' || opts.type === 'candle' ||
      opts.type === 'scatter' || opts.type === 'bubble' || opts.type === 'bar') {
    _calYAxisData = calYAxisData(series, opts, config, context);
    yAxisWidth = _calYAxisData.yAxisWidth;

    // 如果显示Y轴标题
    if (opts.yAxis.showTitle) {
      let maxTitleHeight = 0;
      for (let i = 0; i < opts.yAxis.data.length; i++) {
        maxTitleHeight = Math.max(maxTitleHeight, opts.yAxis.data[i].titleFontSize ? opts.yAxis.data[i].titleFontSize * opts.pix : config.fontSize);
      }
      opts.area[0] += maxTitleHeight;
    }

    let rightIndex = 0;
    let leftIndex = 0;
    // 计算主绘图区域左右位置
    for (let i = 0; i < yAxisWidth.length; i++) {
      if (yAxisWidth[i].position === 'left') {
        if (leftIndex > 0) {
          opts.area[3] += yAxisWidth[i].width + opts.yAxis.padding * opts.pix;
        } else {
          opts.area[3] += yAxisWidth[i].width;
        }
        leftIndex += 1;
      } else if (yAxisWidth[i].position === 'right') {
        if (rightIndex > 0) {
          opts.area[1] += yAxisWidth[i].width + opts.yAxis.padding * opts.pix;
        } else {
          opts.area[1] += yAxisWidth[i].width;
        }
        rightIndex += 1;
      }
    }
  } else {
    config.yAxisWidth = yAxisWidth;
  }
  opts.chartData.yAxisData = _calYAxisData;

  if (opts.categories && opts.categories.length && opts.type !== 'radar' &&
      opts.type !== 'gauge' && opts.type !== 'bar') {
    opts.chartData.xAxisData = getXAxisPoints(opts.categories, opts, config);
    const _calCategoriesData = calCategoriesData(
      opts.categories,
      opts,
      config,
      opts.chartData.xAxisData.eachSpacing,
      context
    );
    const xAxisHeight = _calCategoriesData.xAxisHeight;
    const angle = _calCategoriesData.angle;
    config.xAxisHeight = xAxisHeight;
    config._xAxisTextAngle_ = angle;
    opts.area[2] += xAxisHeight;
    opts.chartData.categoriesData = _calCategoriesData;
  } else {
    if (opts.type === 'line' || opts.type === 'area' || opts.type === 'scatter' ||
        opts.type === 'bubble' || opts.type === 'bar') {
      opts.chartData.xAxisData = calXAxisData(series, opts, config, context);
      categories = opts.chartData.xAxisData.rangesFormat;
      const _calCategoriesData = calCategoriesData(
        categories,
        opts,
        config,
        opts.chartData.xAxisData.eachSpacing,
        context
      );
      const xAxisHeight = _calCategoriesData.xAxisHeight;
      const angle = _calCategoriesData.angle;
      config.xAxisHeight = xAxisHeight;
      config._xAxisTextAngle_ = angle;
      opts.area[2] += xAxisHeight;
      opts.chartData.categoriesData = _calCategoriesData;
    } else {
      opts.chartData.xAxisData = {
        xAxisPoints: []
      };
    }
  }

  // 计算右对齐偏移距离
  if (opts.enableScroll && opts.xAxis.scrollAlign === 'right' && opts._scrollDistance_ === undefined) {
    let offsetLeft = 0;
    const xAxisPoints = opts.chartData.xAxisData.xAxisPoints;
    const startX = opts.chartData.xAxisData.startX;
    const endX = opts.chartData.xAxisData.endX;
    const eachSpacing = opts.chartData.xAxisData.eachSpacing;
    const totalWidth = eachSpacing * (xAxisPoints.length - 1);
    const screenWidth = endX - startX;
    offsetLeft = screenWidth - totalWidth;
    _this.scrollOption.currentOffset = offsetLeft;
    _this.scrollOption.startTouchX = offsetLeft;
    _this.scrollOption.distance = 0;
    _this.scrollOption.lastMoveTime = 0;
    opts._scrollDistance_ = offsetLeft;
  }

  if (type === 'pie' || type === 'ring' || type === 'rose') {
    config._pieTextMaxLength_ = opts.dataLabel === false ? 0 : getPieTextMaxLength(seriesMA, config, context, opts);
  }

  // 图表进度计算函数
  function chartProcess(process) {
    return process;
  }

  // 根据图表类型执行不同的绘制逻辑
  // 注意：这里需要导入所有 renderers 函数
  // 由于 renderers 模块可能缺少部分函数的导出，需要先更新 renderers/index.ts
  // 临时占位符：完整的 switch 语句太长，将在后续完善

  /* 占位符：以下是需要从 renderers 导入但可能未导出的函数
     - contextRotate
     - drawMarkLine
     - drawToolTipBridge
     - drawRoseDataPoints
     - drawGaugeDataPoints
     - drawArcbarDataPoints

     在实际使用时，需要在 renderers/index.ts 中添加这些函数的导出
  */

  // 标记：需要完整的图表类型绘制逻辑
  // 完整实现见 u-charts.ts 第6499-6936行
};

/**
 * 辅助函数：创建动画实例
 */
export function createAnimationInstance(
  opts: AnimationOptions,
  context: DrawChartsContext
): Animation {
  return new Animation(opts);
}

/**
 * 辅助函数：处理动画完成
 */
export function handleAnimationComplete(
  context: DrawChartsContext,
  eventName: string = 'renderComplete'
): void {
  context.uevent.trigger(eventName);
}
