const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const fs = require('fs')
const path = require('path')
const util = require('util')
const streamPipeline = util.promisify(require('stream').pipeline)
const stat = util.promisify(fs.stat)

// const relationship = fs.createWriteStream('./scripts/colexiu-xmls/index.json')
const xmlResult = fs.createWriteStream('./scripts/xmlresult.txt')
const relationshipJSON = fs.readFileSync('./scripts/colexiu-xmls/index.json')

const Authorization = 'bearer 54fa202e-3b32-40e5-b463-3d277f3f82d0'
const LIST_URL = 'https://dev.colexiu.com/api-admin/music/sheet/list'

const FetchList = async (page = 1, pageSize = 100) => {
  const response = await fetch(LIST_URL, {
    method: 'POST',
    headers: {
      Authorization,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      page,
      limit: pageSize,
    })
  })
  const json = await response.json()
  console.log(json)
  return json
}

let page = 1

let errortimes = 0

/**
 * 从接口读取所有数据信息
 */
const start = async () => {
  console.log(page)
  try {
    const json = await FetchList(page)
    for (const item of json.data.rows) {
      relationship.write(JSON.stringify(item) + '\n')
    }
    if (json.data.totalPage > page) {
      page++
      await start()
    }
    errortimes = 0
  } catch (error) {
    if (errortimes < 3) {
      await start()
    }
    console.log(error)
  }
}

// start()

const getFilename = url => {
  return decodeURI(new URL(url).pathname).replace(/\//ig, ' ').trim().split(' ').join('-')
}

const FetchXml = async url => {
  const filename = getFilename(url)
  const response = await fetch(url, {
    method: 'GET',
  })
  console.log(url, response)
  if (response.status !== 200) throw new Error(`unexpected response ${response.statusText}`)
  await streamPipeline(response.body, fs.createWriteStream(path.join(__dirname, 'colexiu-xmls', filename)))
}

const ids = ['2366', '2365', '2364', '2363', '2353', '2279', '2223', '2178', '2162', '1822', '1127', '959', '942', '809', '798', '790', '726', '722', '21', '9', '6', '5', '4']


/**
 * 验证数据
 */
const errorlist = {}
const verification = async () => {
  const list = await relationshipJSON.toString()
  const items = list.split('\n')
  for (const item of items) {
    try {
      const json = JSON.parse(item)
      // if (!ids.includes(json.id + '')) {
      //   continue
      // }
      const txt = ['曲目id：' + json.id,'曲目名称：' + json.musicSheetName, '音频类型：' + json.audioType].join(' ')
      // xmlResult.write(txt + '\n')
      if (!json.xmlFileUrl) {
        errorlist[json.id] = '错误原因：没有xml文件'
        console.log(errorlist[json.id])
      }
      const subfixname = json.xmlFileUrl.split('.').pop()
      if (subfixname !== 'xml') {
        errorlist[json.id] = '错误原因：xml文件格式不对，文件是：' + subfixname
      }
      // console.log(json.xmlFileUrl)
      // try {
      //   const filestat = fs.statSync(path.join(__dirname, 'colexiu-xmls', getFilename(json.xmlFileUrl)))
      //   console.log(filestat)
      // } catch (error) {
      //   console.log(error)
      // }
      if (!errorlist[json.id]) {
        try {
          // await FetchXml(json.xmlFileUrl)
          fs.statSync(path.join(__dirname, 'colexiu-xmls', getFilename(json.xmlFileUrl)))
        } catch (error) {
          console.log(error)
          errorlist[json.id] = '错误原因：文件下载失败'
        }
      }
      if (errorlist[json.id]) {
        xmlResult.write([json.id, json.musicSheetName, errorlist[json.id]].join(' ') + '\n')
      }
    } catch (error) {
      console.log('解析错误：' + item)
    }
  }


  // xmlResult.write('\n\n\n')
  // xmlResult.write(JSON.stringify(errorlist) + '\n')
}

// verification()

const isXmlFile = async filepath => {
  try {
    const readerfile = util.promisify(fs.readFile)
    const xml = await readerfile(filepath)
    return xml.indexOf('<?xml ') === 0
  } catch (error) {
    return false
  }
}

/**
 * 验证是否是xml文件
 */
const xmlVerification = async () => {
  const xmls = fs.readdirSync(path.join(__dirname, 'colexiu-xmls'))

  for (const xml of xmls) {
    const filepath = path.join(__dirname, 'colexiu-xmls', xml)
    const fileStat = await stat(filepath)
    if (xml.split('.').pop() !== 'xml' || !fileStat.isFile()) {
      continue
    }
    const isxml = await isXmlFile(filepath)
    if(!isxml) {
      console.log(xml)
    }
  }
}

xmlVerification()

const overlay = async () => {
  const xmlinfo = {}
  const xmlstring = fs.readFileSync(path.join(__dirname, 'colexiu-xmls', 'index.json'))
  const list = xmlstring.toString().split('\n')

  for (const item of list) {
    try {
      const json = JSON.parse(item)
      if (!xmlinfo[json.xmlFileUrl]) {
        xmlinfo[json.xmlFileUrl] = 0
      }
      xmlinfo[json.xmlFileUrl] = xmlinfo[json.xmlFileUrl] + 1
    } catch (error) {}
  }
  console.log(xmlinfo)
}

// overlay()
