/**
 * 音频可视化
 * @param audioDom
 * @param canvasDom
 * @param fftSize  2的幂数，最小为32
 * 注意   由于ios低版本必须在用户操作之后才能初始化 createMediaElementSource 所以必须在用户操作之后初始化
 */
export default function audioVisualDraw(audioDom, canvasDom, fftSize = 128) {
   // canvas
   const canvasCtx = canvasDom.getContext("2d")
   let { width, height } = canvasDom.getBoundingClientRect()
   // 向上取整，当with为小数或者小于当前dom时候，切换app之后 会出现黑边
   width = Math.ceil(width)
   height = Math.ceil(height)
   canvasDom.width = width
   canvasDom.height = height
   // audio
   //const audioCtx = new AudioContext()
   //const source = audioCtx.createMediaElementSource(audioDom)
   //const analyser = audioCtx.createAnalyser()
   //analyser.fftSize = fftSize
   //source?.connect(analyser)
   //analyser.connect(audioCtx.destination)
   //const dataArray = new Uint8Array(fftSize / 2)
   const draw = (data, ctx, { lineGap, canvWidth, canvHeight, canvFillColor, lineColor }) => {
      if (!ctx) return
      const w = canvWidth
      const h = canvHeight
      fillCanvasBackground(ctx, w, h, canvFillColor)
      // 可视化
      const dataLen = data.length
      let step = (w / 2 - lineGap * dataLen) / dataLen
      step < 1 && (step = 1)
      const midX = w / 2
      const midY = h / 2
      let xLeft = midX
      for (let i = 0; i < dataLen; i++) {
         const value = data[i]
         const percent = value / 255 // 最大值为255
         const barHeight = percent * midY
         canvasCtx.fillStyle = lineColor
         // 中间加间隙
         if (i === 0) {
            xLeft -= lineGap / 2
         }
         canvasCtx.fillRect(xLeft - step, midY - barHeight, step, barHeight)
         canvasCtx.fillRect(xLeft - step, midY, step, barHeight)
         xLeft -= step + lineGap
      }
      let xRight = midX
      for (let i = 0; i < dataLen; i++) {
         const value = data[i]
         const percent = value / 255 // 最大值为255
         const barHeight = percent * midY
         canvasCtx.fillStyle = lineColor
         if (i === 0) {
            xRight += lineGap / 2
         }
         canvasCtx.fillRect(xRight, midY - barHeight, step, barHeight)
         canvasCtx.fillRect(xRight, midY, step, barHeight)
         xRight += step + lineGap
      }
   }
   const fillCanvasBackground = (ctx, w, h, colors) => {
      ctx.clearRect(0, 0, w, h)
      ctx.fillStyle = colors
      ctx.fillRect(0, 0, w, h)
   }
   const requestAnimationFrameFun = () => {
      // requestAnimationFrame(() => {
      //   //analyser?.getByteFrequencyData(dataArray)
      //   draw(generateMixedData(48), canvasCtx, {
      //     lineGap: 2,
      //     canvWidth: width,
      //     canvHeight: height,
      //     canvFillColor: "transparent",
      //     lineColor: "rgba(255, 255, 255, 0.7)"
      //   })
      //   if (!isPause) {
      //     requestAnimationFrameFun()
      //   }
      // })
      const _time = setInterval(() => {
         if (isPause) {
            clearInterval(_time)
            return
         }
         //analyser?.getByteFrequencyData(dataArray)
         draw(generateMixedData(48), canvasCtx, {
            lineGap: 2,
            canvWidth: width,
            canvHeight: height,
            canvFillColor: "transparent",
            lineColor: "#ffffff"
         })
      }, 300)
   }
   let isPause = true
   const playVisualDraw = () => {
      //audioCtx.resume()  // 重新更新状态   加了暂停和恢复音频音质发生了变化  所以这里取消了
      isPause = false
      requestAnimationFrameFun()
   }
   const pauseVisualDraw = () => {
      isPause = true
      requestAnimationFrame(() => {
         canvasCtx.clearRect(0, 0, width, height)
      })
      //audioCtx?.suspend()  // 暂停   加了暂停和恢复音频音质发生了变化  所以这里取消了
      // source?.disconnect()
      // analyser?.disconnect()
   }
   return {
      playVisualDraw,
      pauseVisualDraw
   }
}

export function generateMixedData(size) {
   const dataArray = new Uint8Array(size)
   const baseNoiseAmplitude = 30
   const minFrequency = 0.01
   const maxFrequency = 0.2
   const minAmplitude = 50
   const maxAmplitude = 150

   let lastAmplitude = maxAmplitude // 初始振幅设置为最大值
   let lastFrequency = minFrequency + Math.random() * (maxFrequency - minFrequency)

   for (let i = 0; i < size; i++) {
      const decayFactor = 1 - i / size // 使振幅随时间递减
      const amplitude = lastAmplitude * decayFactor + (Math.random() - 0.5) * 10
      const frequency = lastFrequency + (Math.random() - 0.5) * 0.01
      const wave = amplitude * (0.5 + 0.5 * Math.sin(frequency * i))
      const noise = Math.floor(Math.random() * baseNoiseAmplitude) - baseNoiseAmplitude / 2
      dataArray[i] = Math.min(255, Math.max(0, Math.floor(wave + noise)))
      lastAmplitude += (amplitude - lastAmplitude) * 0.05
      lastFrequency += (frequency - lastFrequency) * 0.05
   }

   return dataArray
}

export function vaildMusicScoreUrl() {
   const url = window.location.hostname
   let returnUrl = ""
   if (/dev/.test(url) || /192.168/.test(url)) {
      returnUrl = "https://test.gym.lexiaoya.cn"
   } else if (/test/.test(url)) {
      // test 环境
      returnUrl = "https://test.gym.lexiaoya.cn"
   } else {
      returnUrl = "https://gym.lexiaoya.cn"
   }
   return returnUrl
}
