import { web3 } from "hardhat";
import swapPath from "./interface/swapPath";
import logger from "../utils/logger";

function filterPool(pool: any) {
  return true
}

function handleSamePools(poolList: any[], pathList: any[]) {
  for (let poolIndexI = 0; poolIndexI < poolList.length; poolIndexI++) {
    const x = poolList[poolIndexI]
    if (!filterPool(x)) continue

    for (let poolIndexJ = poolIndexI + 1; poolIndexJ < poolList.length; poolIndexJ++) {
      const y = poolList[poolIndexJ]
      if (!filterPool(y)) continue

      pathList.push([x, y])
    }
  }
}

async function main() {
  logger.debug('start loading...')
  const v2PoolList = require('../config/v2PoolList.json')
  const v3PoolList = require('../config/v3PoolList.json')

  logger.debug('start filter...')
  const checkedPath: any = {}
  for (let v2PoolIndex = 0; v2PoolIndex < v2PoolList.length; v2PoolIndex++) {
    const v2Pool = v2PoolList[v2PoolIndex]
    v2Pool.type = 'v2'

    // 已经查过的sum不再查询
    if (checkedPath[v2Pool.sum2] != null) continue
    checkedPath[v2Pool.sum2] = []

    // 开始排查
    const sameSumPoolList = [v2Pool]
    // 排查v2的，从当前位置开始，以免重复path
    for (let findSameSumV2Index = v2PoolIndex + 1; findSameSumV2Index < v2PoolList.length; findSameSumV2Index++) {
      const v2PoolUnChecked = v2PoolList[findSameSumV2Index]
      // 同sumValue
      if (v2PoolUnChecked.sum2 == v2Pool.sum2) {
        v2PoolUnChecked.type = 'v2'
        sameSumPoolList.push(v2PoolUnChecked)
      }
    }
    // 排查v3的，从0开始
    for (let findSameSumV3Index = 0; findSameSumV3Index < v3PoolList.length; findSameSumV3Index++) {
      const v3PoolUnChecked = v3PoolList[findSameSumV3Index]
      // 同sumValue
      if (v3PoolUnChecked.sum2 == v2Pool.sum2) {
        v3PoolUnChecked.type = 'v3'
        sameSumPoolList.push(v3PoolUnChecked)
      }
    }

    if (sameSumPoolList.length > 1) {
      handleSamePools(sameSumPoolList, checkedPath[v2Pool.sum2])
      await swapPath.appendOrUpdate(v2Pool.sum2, '2', checkedPath[v2Pool.sum2])
    }
  }
  // 处理v3的
  for (let v3PoolIndex = 0; v3PoolIndex < v3PoolList.length; v3PoolIndex++) {
    const v3Pool = v3PoolList[v3PoolIndex]
    v3Pool.type = 'v3'

    // 已经查过的sum不再查询
    if (checkedPath[v3Pool.sum2] != null) continue
    checkedPath[v3Pool.sum2] = []

    // 开始排查
    const sameSumPoolList = [v3Pool]
    // 排查v3的，从v3PoolIndex开始
    for (let findSameSumV3Index = v3PoolIndex + 1; findSameSumV3Index < v3PoolList.length; findSameSumV3Index++) {
      const v3PoolUnChecked = v3PoolList[findSameSumV3Index]
      // 同sumValue
      if (v3PoolUnChecked.sum2 == v3Pool.sum2) {
        v3PoolUnChecked.type = 'v3'
        sameSumPoolList.push(v3PoolUnChecked)
      }
    }

    if (sameSumPoolList.length > 1) {
      handleSamePools(sameSumPoolList, checkedPath[v3Pool.sum2])
      await swapPath.appendOrUpdate(v3Pool.sum2, '2', checkedPath[v3Pool.sum2])
    }
  }
  logger.debug('query ok.')
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
})
