/**
 * 仪表盘数据项接口
 */
export interface GaugeDataItem {
  data: number | null;
  _proportion_?: number;
  color?: string;
  _endAngle_?: number;
  _oldAngle_?: number;
  [key: string]: any;
}

/**
 * 仪表盘分类项
 */
export interface GaugeCategoryItem {
  value: number;
  color: string;
  _startAngle_?: number;
  _endAngle_?: number;
  [key: string]: any;
}

/**
 * 仪表盘配置选项
 */
export interface GaugeOption {
  type?: string;
  startAngle?: number;
  endAngle?: number;
  direction?: string;
  oldAngle?: number;
  oldData?: number;
  pointer?: {
    color?: string;
  };
  [key: string]: any;
}

/**
 * 环形条配置选项
 */
export interface ArcbarOption {
  type?: string;
  startAngle?: number;
  endAngle?: number;
  direction?: string;
  [key: string]: any;
}

/**
 * 获取仪表盘数据点
 * 计算仪表盘显示的角度和颜色
 * @param series - 仪表盘数据项数组
 * @param categories - 颜色范围分类项数组
 * @param gaugeOption - 仪表盘配置选项
 * @param process - 动画进度（0-1），默认为1（无动画）
 * @returns 包含计算后角度的仪表盘数据项数组
 */
export function getGaugeDataPoints(
  series: GaugeDataItem[],
  categories: GaugeCategoryItem[],
  gaugeOption: GaugeOption,
  process: number = 1
): GaugeDataItem[] {
  for (let i = 0; i < series.length; i++) {
    let item = series[i];
    item.data = item.data === null ? 0 : item.data;
    if (gaugeOption.pointer?.color == 'auto') {
      for (let j = 0; j < categories.length; j++) {
        if (item.data <= categories[j].value) {
          item.color = categories[j].color;
          break;
        }
      }
    } else {
      item.color = gaugeOption.pointer?.color;
    }
    let totalAngle: number;
    if (gaugeOption.endAngle! < gaugeOption.startAngle!) {
      totalAngle = 2 + gaugeOption.endAngle! - gaugeOption.startAngle!;
    } else {
      totalAngle = gaugeOption.startAngle! - gaugeOption.endAngle!;
    }
    item._endAngle_ = totalAngle * item.data + gaugeOption.startAngle!;
    item._oldAngle_ = gaugeOption.oldAngle || 0;
    if (gaugeOption.oldAngle! < gaugeOption.endAngle!) {
      item._oldAngle_! += 2;
    }
    if (item.data >= (gaugeOption.oldData || 0)) {
      item._proportion_ = (item._endAngle_! - item._oldAngle_!) * process + (gaugeOption.oldAngle || 0);
    } else {
      item._proportion_ = item._oldAngle_! - (item._oldAngle_! - item._endAngle_!) * process;
    }
    if (item._proportion_! >= 2) {
      item._proportion_ = item._proportion_! % 2;
    }
  }
  return series;
}

/**
 * 获取仪表盘环形条数据点
 * 计算仪表盘环形条显示的比例
 * @param series - 仪表盘数据项数组
 * @param arcbarOption - 环形条配置选项
 * @param process - 动画进度（0-1），默认为1（无动画）
 * @returns 包含计算后比例的仪表盘数据项数组
 */
export function getGaugeArcbarDataPoints(
  series: GaugeDataItem[],
  arcbarOption: ArcbarOption,
  process: number = 1
): GaugeDataItem[] {
  let currentProcess = process;
  if (currentProcess == 1) {
    currentProcess = 0.999999;
  }
  for (let i = 0; i < series.length; i++) {
    let item = series[i];
    item.data = item.data === null ? 0 : item.data;
    let totalAngle: number;
    if (arcbarOption.type == 'circle') {
      totalAngle = 2;
    } else {
      if (arcbarOption.endAngle! < arcbarOption.startAngle!) {
        totalAngle = 2 + arcbarOption.endAngle! - arcbarOption.startAngle!;
      } else {
        totalAngle = arcbarOption.startAngle! - arcbarOption.endAngle!;
      }
    }
    item._proportion_ = totalAngle * item.data * currentProcess + arcbarOption.startAngle!;
    if (item._proportion_! >= 2) {
      item._proportion_ = item._proportion_! % 2;
    }
  }
  return series;
}

/**
 * 获取环形条数据点
 * 计算环形条（圆形进度条）图表显示的比例
 * @param series - 仪表盘数据项数组
 * @param arcbarOption - 环形条配置选项
 * @param process - 动画进度（0-1），默认为1（无动画）
 * @returns 包含计算后比例的仪表盘数据项数组
 */
export function getArcbarDataPoints(
  series: GaugeDataItem[],
  arcbarOption: ArcbarOption,
  process: number = 1
): GaugeDataItem[] {
  let currentProcess = process;
  if (currentProcess == 1) {
    currentProcess = 0.999999;
  }
  for (let i = 0; i < series.length; i++) {
    let item = series[i];
    item.data = item.data === null ? 0 : item.data;
    let totalAngle: number;
    if (arcbarOption.type == 'circle') {
      totalAngle = 2;
    } else {
      if(arcbarOption.direction == 'ccw'){
        if (arcbarOption.startAngle! < arcbarOption.endAngle!) {
          totalAngle = 2 + arcbarOption.startAngle! - arcbarOption.endAngle!;
        } else {
          totalAngle = arcbarOption.startAngle! - arcbarOption.endAngle!;
        }
      }else{
        if (arcbarOption.endAngle! < arcbarOption.startAngle!) {
          totalAngle = 2 + arcbarOption.endAngle! - arcbarOption.startAngle!;
        } else {
          totalAngle = arcbarOption.startAngle! - arcbarOption.endAngle!;
        }
      }
    }
    item._proportion_ = totalAngle * item.data * currentProcess + arcbarOption.startAngle!;
    if(arcbarOption.direction == 'ccw'){
      item._proportion_ = arcbarOption.startAngle! - totalAngle * item.data * currentProcess;
    }
    if (item._proportion_! >= 2) {
      item._proportion_ = item._proportion_! % 2;
    }
  }
  return series;
}

/**
 * 获取仪表盘轴线点
 * 计算仪表盘的轴线点
 * @param categories - 分类项数组
 * @param startAngle - 仪表盘起始角度
 * @param endAngle - 仪表盘结束角度
 * @returns 包含计算后角度的分类项数组
 */
export function getGaugeAxisPoints(
  categories: GaugeCategoryItem[],
  startAngle: number,
  endAngle: number
): GaugeCategoryItem[] {
  let totalAngle: number;
  if (endAngle < startAngle) {
    totalAngle = 2 + endAngle - startAngle;
  } else {
    totalAngle = startAngle - endAngle;
  }
  let tempStartAngle = startAngle;
  for (let i = 0; i < categories.length; i++) {
    categories[i].value = categories[i].value === null ? 0 : categories[i].value;
    categories[i]._startAngle_ = tempStartAngle;
    categories[i]._endAngle_ = totalAngle * categories[i].value + startAngle;
    if (categories[i]._endAngle_! >= 2) {
      categories[i]._endAngle_ = categories[i]._endAngle_! % 2;
    }
    tempStartAngle = categories[i]._endAngle_!;
  }
  return categories;
}
