const ThreadDragImage = document.createElement("img")
ThreadDragImage.src = `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADimHc4AAAJc0lEQVR42u2dSW8URxTHsY0XtgQTspySKIryBRCgALZIIPkA4RL5kkMuufAVcs2VIxKCAycuCBIBYjE7GGOx72bfwg628bAYA536VfpFL+Xume6ebnvkqZb+IswMXfX+v6rXr6pnOlOCIJjiNXHyJngAHoCXB+ABeHkAHoCXB+ABeHkAHoCXB+ABeHkAHoCXB+ABeHkAdQQg5dHg9T8lPrICKNd4Yx0rNZC0AMqZ3WQ0tc7VVAFIVQDGGN/e3v7lvHnzlnZ2di6LUkdHx/LJrLi458+fv3Tu3LlfxYDIBGCM+Q0NDQtWrVr167Nnz3rM518F/pBjZHBwsG/NmjW/NTY2LqwEIQkA13ym2WddXV0/PX/+fMD7HX2USqXhlStXdhmvPlepaQyENADEfLTgxo0bf718+TJ48eJF8P79e++4OvAEb+7du9eNV8q3xrQA3IutXGgXmgbuvXv3LhgeHg6GhoY8BHPggckKVnjz5s2bIbyKuED/ByENADG/2ejb0dHREo28ffs2GBgYCMy1wDZarwex4wFe4Al/BwheGbU4EFIBEGpifksIYJiGzJ/ByMhI8Pjx4+Dhw4f27/V2EDOxP3r0yHrB3wVCCKA19M6FkBiAjP6W8GSLzPSyM0AAvH79Orh//35w9+5dpt6ETH+t8TTf5HorPBAAagYsMmqLmQVlAbjpR0Y/J1ssACCN4TT+6tWr4M6dO8GtW7dsR6KMyVO0X0lFtc1B3MR6+/ZtGzse8JqTghYbTQu9a3bTUFIAkn5aw5Mt0QBkFtAJKoCbN28G165dsx0q0mTarqSigBDv9evXA1MN2piJndciACwxmu6koVQAmiIAdABADNKzgPLL1MAWwOXLl23Hko7WJNLmAr6S4kBkFbEQ55UrV4KrV6/aWIlZRr9OPyGADqMZcdeBSgDc/N8W0uygCtKjlIbpgMwCylM6eenSJdvBaiGkNb4IEMRAfMTU399vY9Sj3zU/+NdIARB5HcgMQM8ADUB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AAvD8AD8PIAPAAvD8AD8PIAPAAvD8AD8CpO/wAnnXiPa3zSAAAAAABJRU5ErkJggg==`;

const DragCanvas = document.createElement("canvas")
DragCanvas.style.position = "absolute"
DragCanvas.style.zIndex = 0
document.body.appendChild(DragCanvas)

const PercentLoadedCache = {}
const PercentLoadedCanvas = document.createElement("canvas")
PercentLoadedCanvas.style.position = "absolute"
PercentLoadedCanvas.style.zIndex = 0
document.body.appendChild(PercentLoadedCanvas)

const SystemTrayCanvas = document.createElement("canvas")

export function roundRect(ctx, x, y, width, height, radius = 5, fill, stroke = true) {
  ctx.beginPath()
  ctx.moveTo(x + radius, y)
  ctx.lineTo(x + width - radius, y)
  ctx.quadraticCurveTo(x + width, y, x + width, y + radius)
  ctx.lineTo(x + width, y + height - radius)
  ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height)
  ctx.lineTo(x + radius, y + height)
  ctx.quadraticCurveTo(x, y + height, x, y + height - radius)
  ctx.lineTo(x, y + radius)
  ctx.quadraticCurveTo(x, y, x + radius, y)
  ctx.closePath()
  if (stroke) {
    ctx.stroke();
  }
  if (fill) {
    ctx.fill();
  }
}

export function dataURIForLoadedPercent(_percent) {
  const percent = Math.floor(_percent / 5.0) * 5.0
  const cacheKey = `${percent}%`;
  if (!PercentLoadedCache[cacheKey]) {
    const canvas = PercentLoadedCanvas
    const scale = window.devicePixelRatio
    canvas.width = 20 * scale
    canvas.height = 20 * scale
    canvas.style.width = "30px"
    canvas.style.height = "30px"

    const half = 10 * scale
    const ctx = canvas.getContext('2d')
    ctx.strokeStyle = "#AAA"
    ctx.lineWidth = 3 * scale
    ctx.clearRect(0, 0, 20 * scale, 20 * scale)
    ctx.beginPath()
    ctx.arc(half, half, half - ctx.lineWidth, -0.5 * Math.PI, (-0.5 * Math.PI) + (2 * Math.PI) * percent / 100.0)
    ctx.stroke()
    PercentLoadedCache[cacheKey] = canvas.toDataURL();
  }
  return PercentLoadedCache[cacheKey];
}

export function canvasWithThreadDragImage(count) {
  const canvas = DragCanvas

  // Make sure the canvas has a 2x pixel density on retina displays
  const scale = window.devicePixelRatio
  canvas.width = 58 * scale
  canvas.height = 55 * scale
  canvas.style.width = "58px"
  canvas.style.height = "55px"

  const ctx = canvas.getContext('2d')

  // mail background image
  if (count > 1) {
    ctx.rotate(-20 * Math.PI / 180)
    ctx.drawImage(ThreadDragImage, -10 * scale, 2 * scale, 48 * scale, 48 * scale)
    ctx.rotate(20 * Math.PI / 180)
  }
  ctx.drawImage(ThreadDragImage, 0, 0, 48 * scale, 48 * scale)

  // count bubble
  const dotGradient = ctx.createLinearGradient(0, 0, 0, 15 * scale)
  dotGradient.addColorStop(0, "rgb(116, 124, 143)")
  dotGradient.addColorStop(1, "rgb(67, 77, 104)")
  ctx.strokeStyle = "rgba(39, 48, 68, 0.6)"
  ctx.lineWidth = 1
  ctx.fillStyle = dotGradient

  let textX = 49
  let text = `${count}`;

  if (count < 10) {
    roundRect(ctx, 41 * scale, 1 * scale, 16 * scale, 14 * scale, 7 * scale, true, true)
  } else if (count < 100) {
    roundRect(ctx, 37 * scale, 1 * scale, 20 * scale, 14 * scale, 7 * scale, true, true)
    textX = 46
  } else {
    roundRect(ctx, 33 * scale, 1 * scale, 25 * scale, 14 * scale, 7 * scale, true, true)
    text = "99+"
    textX = 46
  }

  // count text
  ctx.fillStyle = "rgba(255,255,255,0.9)"
  ctx.font = `${11 * scale}px sans-serif`;
  ctx.textAlign = "center"
  ctx.fillText(text, textX * scale, 12 * scale, 30 * scale)

  return DragCanvas
}

export function measureTextInCanvas(text, font) {
  const canvas = document.createElement('canvas')
  const context = canvas.getContext('2d')
  context.font = font
  return Math.ceil(context.measureText(text).width)
}

export function canvasWithSystemTrayIconAndText(img, text) {
  const canvas = SystemTrayCanvas
  const w = img.width
  const h = img.height
  const font = '26px Nylas-Pro, sans-serif'

  const textWidth = text.length > 0 ? measureTextInCanvas(text, font) + 2 : 0;
  canvas.width = w + textWidth
  canvas.height = h

  const context = canvas.getContext('2d')
  context.font = font
  context.fillStyle = 'black'
  context.textAlign = 'start'
  context.textBaseline = 'middle'

  context.drawImage(img, 0, 0)
  // Place after img, vertically aligned
  context.fillText(text, w, h / 2)
  return canvas
}
