const { createWriteStream, readdir, readFile } = require('fs')
const { join } = require('path')
const { promisify } = require('util')
const { parseString } = require('xml2js')

!async function (){
  const logs = createWriteStream(join(__dirname, 'logs.txt'))
  // console.log(logs.write)
  const files = await promisify(readdir)(join(__dirname, 'colexiu-xmls'))
  for (const file of files) {
    if (file.endsWith('.xml')) {
      try {
        console.log(file)
        const content = await promisify(readFile)(join(__dirname, 'colexiu-xmls', file))
        const json = await promisify(parseString)(content.toString())
        let times = []
        for (let i = 0; i < json['score-partwise'].part[0].measure.length; i++) {
          if (i > 1) {
            break
          }
          const measure = json['score-partwise'].part[0].measure[i]
          let time = 0
          for (let j = 0; j < measure.note.length; j++) {
            time = time + +measure.note[j].duration[0]
          }
          times.push(time)
        }
        if (times[0] !== times[1]) {
          logs.write(`${file} 是弱起\n`)
        }
        console.log(times)
      } catch (error) {
        logs.write(`${file} 解析失败\n`)
      }
    }
  }
}()
