/**
 * 分类数据处理函数
 *
 * 从 u-charts 核心库搬迁的分类数据处理相关函数
 * 用于处理分类轴的数据计算
 */

// 类型定义
export interface ChartOptions {
  categories?: string[];
  xAxis?: XAxisOptions;
  width?: number;
  height?: number;
  pix?: number;
  area?: number[];
  [key: string]: any;
}

export interface XAxisOptions {
  lineHeight?: number;
  marginTop?: number;
  fontSize?: number;
  disabled?: boolean;
  rotateLabel?: boolean;
  rotateAngle?: number;
  scrollShow?: boolean;
  enableScroll?: boolean;
  formatter?: (item: any, index: number, opts: ChartOptions) => string;
  [key: string]: any;
}

export interface UChartsConfig {
  version: string;
  color: string[];
  linearColor: string[];
  yAxisWidth: number;
  xAxisHeight: number;
  padding: number[];
  rotate: boolean;
  fontSize: number;
  fontColor: string;
  dataPointShape: string[];
  pieChartLinePadding: number;
  pieChartTextPadding: number;
  titleFontSize: number;
  subtitleFontSize: number;
  radarLabelTextMargin: number;
}

export interface CategoriesDataResult {
  angle: number;
  xAxisHeight: number;
}

function measureText(text: string | number, fontSize: number, context?: any): number {
  let width = 0;
  text = String(text);
  if (context !== false && context !== undefined && context.setFontSize && context.measureText) {
    context.setFontSize(fontSize);
    return context.measureText(text).width;
  } else {
    const textArray = text.split('');
    for (let i = 0; i < textArray.length; i++) {
      const item = textArray[i];
      if (/[a-zA-Z]/.test(item)) {
        width += 7;
      } else if (/[0-9]/.test(item)) {
        width += 5.5;
      } else if (/\./.test(item)) {
        width += 2.7;
      } else if (/-/.test(item)) {
        width += 3.25;
      } else if (/:/.test(item)) {
        width += 2.5;
      } else if (/[\u4e00-\u9fa5]/.test(item)) {
        width += 10;
      } else if (/\(|\)/.test(item)) {
        width += 3.73;
      } else if (/\s/.test(item)) {
        width += 2.5;
      } else if (/%/.test(item)) {
        width += 8;
      } else {
        width += 10;
      }
    }
    return width * fontSize / 10;
  }
}

/**
 * 计算分类数据
 * 处理分类轴的数据
 *
 * @param categories - 分类数据数组
 * @param opts - 图表配置选项
 * @param config - uCharts配置对象
 * @param eachSpacing - 每个刻度的间距
 * @param context - Canvas上下文
 * @returns 分类数据结果
 */
export function calCategoriesData(
  categories: string[],
  opts: ChartOptions,
  config: UChartsConfig,
  eachSpacing: number,
  context?: any
): CategoriesDataResult {
  const result: CategoriesDataResult = {
    angle: 0,
    xAxisHeight: (opts.xAxis?.lineHeight || 0) * (opts.pix || 1) + (opts.xAxis?.marginTop || 0) * (opts.pix || 1)
  };
  const fontSize = (opts.xAxis?.fontSize || 13) * (opts.pix || 1);
  const categoriesTextLenth = categories.map(function(item, index) {
    const xitem = opts.xAxis?.formatter ? opts.xAxis.formatter(item, index, opts) : item;
    return measureText(String(xitem), fontSize, context);
  });
  const maxTextLength = Math.max(...categoriesTextLenth);
  if (opts.xAxis?.rotateLabel == true) {
    result.angle = (opts.xAxis.rotateAngle || 0) * Math.PI / 180;
    const tempHeight = (opts.xAxis.marginTop || 0) * (opts.pix || 1) * 2 + Math.abs(maxTextLength * Math.sin(result.angle));
    const minHeight = fontSize + (opts.xAxis.marginTop || 0) * (opts.pix || 1) * 2;
    result.xAxisHeight = tempHeight < minHeight ? minHeight : tempHeight;
  }
  if (opts.xAxis?.scrollShow && (opts as any).enableScroll) {
    result.xAxisHeight += 6 * (opts.pix || 1);
  }
  if (opts.xAxis?.disabled) {
    result.xAxisHeight = 0;
  }
  return result;
}
