/**
 * 上下文数据
 */
export interface FlowContextData {
  input: Map<string, any>;
  output: Map<string, any>;
}

/**
 * 节点执行信息
 */
export interface FlowNodeExec {
  // 当前节点
  current: string;
  // 上一个节点
  prev: string;
  // 下一个节点
  next: string;
  // 下一个节点列表
  nextList: string[];
}

/**
 * 节点信息
 */
export interface NodeInfo {
  enable?: boolean;
  id?: string;
  label?: string;
  type?: string;
  icon?: string;
  name?: `node-${string}`;
  position?: {
    x: number;
    y: number;
  };
  form?: {
    width?: string;
    focus?: string;
    items: any[];
  };
  handle?: {
    target?: boolean;
    source?: boolean;
    next?: { label: string; value: string; [key: string]: any }[];
  };
  data?: {
    inputParams?: any[];
    outputParams?: any[];
    options?: any;
  };
  [key: string]: any;
}

/**
 * 线信息
 */
export interface LineInfo {
  id: string;
  target: string;
  type?: string;
  source: string;
  targetHandle?: string | null;
  sourceHandle?: string | null;
  animated?: boolean;
  style?: any;
  [key: string]: any;
}

/**
 * 流程图
 */
export interface FlowGraph {
  nodes: NodeInfo[];
  edges: LineInfo[];
}

/**
 * 上下文
 */
export class FlowContext {
  // 存储输入输出数据
  private data: FlowContextData = {
    input: new Map(),
    output: new Map(),
  };

  // 请求参数
  private requestParams = {};

  // 调试单个节点
  private debugOne = false;

  // 统计
  private count = {
    // token使用量
    tokenUsage: 0,
  };

  // 执行信息
  private flowNodeExec: FlowNodeExec = {
    current: null,
    prev: null,
    next: null,
    nextList: [],
  };

  // 流程图信息
  private flowGraph: FlowGraph;

  // 是否流式调用
  private stream = false;

  /**
   * 设置调试单个节点
   * @param debugOne
   */
  setDebugOne(debugOne: boolean): void {
    this.debugOne = debugOne;
  }

  /**
   * 是否调试单个节点
   * @returns
   */
  isDebugOne(): boolean {
    return this.debugOne;
  }

  /**
   * 更新统计
   * @param key
   * @param increment
   */
  updateCount(key: string, increment: number) {
    this.count[key] += increment;
  }

  /**
   * 获取统计
   * @returns
   */
  getCount(): any {
    return this.count;
  }

  /**
   * 设置流式调用
   * @param stream
   */
  setStream(stream: boolean): void {
    this.stream = stream;
  }

  /**
   * 是否流式调用
   * @returns
   */
  isStream(): boolean {
    return this.stream;
  }

  /**
   * 设置请求参数
   * @param params
   */
  setRequestParams(params: any): void {
    this.requestParams = params;
  }

  /**
   * 获得请求参数
   * @returns
   */
  getRequestParams(): any {
    return this.requestParams;
  }

  /**
   * 设置执行信息
   * @param flowNodeExec
   */
  setFlowNodeExec(flowNodeExec: FlowNodeExec): void {
    this.flowNodeExec = flowNodeExec;
  }

  /**
   * 获取执行信息
   * @returns
   */
  getFlowNodeExec(): FlowNodeExec {
    return this.flowNodeExec;
  }

  /**
   * 设置流程图
   * @param flowGraph
   */
  setFlowGraph(flowGraph: FlowGraph): void {
    this.flowGraph = flowGraph;
  }

  /**
   * 获取流程图
   * @returns
   */
  getFlowGraph(): FlowGraph {
    return this.flowGraph;
  }

  /**
   * 设置数据
   * @param key 键
   * @param value 值
   * @param type 类型
   */
  set(key: string, value: any, type: 'input' | 'output' = 'input'): void {
    this.data[type].set(key, value);
  }

  /**
   * 获取数据
   * @param key
   * @returns
   */
  get(key: string, type: 'input' | 'output' = 'input'): any {
    return this.data[type].get(key);
  }

  /**
   * 获取所有输入数据
   * @returns
   */
  getData(type?: 'input' | 'output'): FlowContextData | Map<string, any> {
    return type ? this.data[type] : this.data;
  }
}
