import SVGPathCommander from 'svg-path-commander'
const symbol = {
  point: 'M541.257143 1009.298286l11.483428-11.922286a2105.782857 2105.782857 0 0 0 164.205715-194.852571c55.734857-75.337143 101.376-148.772571 133.339428-218.624 32.475429-70.948571 50.102857-136.338286 50.102857-195.510858a388.388571 388.388571 0 1 0-776.777142 0c0 65.536 24.502857 141.531429 69.046857 226.669715a1469.44 1469.44 0 0 0 114.249143 180.589714 2375.753143 2375.753143 0 0 0 179.565714 217.307429c13.677714 14.336 36.425143 14.628571 50.468571 0.658285l4.242286-4.388571z m-3.657143-45.056a2181.558857 2181.558857 0 0 1-53.979429-60.123429 2307.949714 2307.949714 0 0 1-119.588571-150.016A1401.709714 1401.709714 0 0 1 255.195429 582.217143c-39.716571-75.922286-60.928-141.897143-60.928-193.901714a317.732571 317.732571 0 1 1 635.538285 0c0 47.616-15.067429 103.643429-43.666285 166.107428-29.696 64.804571-72.850286 134.363429-125.952 205.970286a2034.834286 2034.834286 0 0 1-173.129143 203.117714l50.468571 0.658286zM670.793143 388.388571a158.866286 158.866286 0 1 0-317.732572 0 158.866286 158.866286 0 0 0 317.732572 0z m-247.149714 0a88.283429 88.283429 0 1 1 176.566857 0 88.283429 88.283429 0 0 1-176.566857 0z',
  line: 'M0 870.4l153.6-153.6 51.2 51.2 563.2-563.2-51.2-51.2L870.4 0 1024 153.6l-153.6 153.6-83.309714-83.382857 22.674285 22.674286-563.2 563.2 60.635429 60.708571L153.6 1024 0 870.4z',
  polygon: 'M911.140571 548.571429l-186.733714 323.437714H350.939429L164.278857 548.571429l186.660572-323.437715h373.540571L911.140571 548.571429z m-141.165714 402.285714l232.228572-402.285714L769.828571 146.285714h-464.457142L73.142857 548.571429 305.371429 950.857143h464.530285z'
}

class PdfCreator {
  constructor (pdf) {
    this.pdf = pdf
  }

  addPage () {
    this.pdf.addPage({
      margin: 50
    })
    return 50
  }

  addHead (title, titleBg) {
    this.pdf.font('msbold').fontSize(20).fillColor('#333333').text(title, 50, 50)
    // Add an image, constrain it to a given size, and center it vertically and horizontally
    this.pdf.image(titleBg, 467, 50, {
      fit: [100, 30],
      align: 'right',
      valign: 'center'
    })
    this.pdf.moveTo(50, 85).lineTo(565, 85).stroke('#dadada')
    return 100
  }

  addTitle (pointer, title) {
    this.pdf.rect(50, pointer + 3, 2, 12).fill('#0099ff')
    this.pdf.font('msbold').fontSize(13).fillColor('#333333').text(title, 58, pointer)
    return pointer + 30
  }

  addSubTitle (pointer, label) {
    this.pdf.circle(51, pointer + 8, 2).fill('#0099ff')
    this.pdf.font('msbold').fontSize(11).fillColor('#333333').text(label, 58, pointer)
    return pointer + 25
  }

  addThirdTitle (pointer, label) {
    this.pdf.font('msbold').fontSize(10).fillColor('#333333').text(label, 50, pointer)
    return pointer + 20
  }

  addForm (pointer, items) {
    for (let i = 0; i < items.length; i = i + 2) {
      let { label, value } = items[i]
      this.pdf
        .font('MSYH')
        .fontSize(10)
        .fillColor('#333333')
        .text(`${label}：${value}`, 50, pointer)
      if (items[i + 1]) {
        this.pdf
          .font('MSYH')
          .fontSize(10)
          .fillColor('#333333')
          .text(`${items[i + 1].label}：${items[i + 1].value}`, 332.5, pointer)
      }
      pointer += 22
    }
    return pointer
  }

  addImage (pointer, url) {
    this.pdf.image(url, 50, pointer, { fit: [515, 400], align: 'center', valign: 'center' })
    return pointer + 410
  }

  addTable (pointer, cols, data) {
    const colsCount = cols.length
    const rowsCount = data.length + 1
    let colWidth = 515 / colsCount
    let rowHeight = 25
    this.pdf.rect(50, pointer, 515, rowHeight).fill('#f8f8f9')
    for (let i = 0; i < rowsCount; i++) {
      if (pointer > 720) {
        pointer = this.addPage()
      }
      this.pdf
        .moveTo(50, pointer + rowHeight)
        .lineTo(565, pointer + rowHeight)
        .stroke('#dadada')
      for (let j = 0; j < colsCount; j++) {
        if (i === 0) {
          // 表头
          this.pdf
            .font('msbold')
            .fontSize(8)
            .fillColor('#333333')
            .text(cols[j].title || '#', 55 + j * colWidth, pointer + 7)
        } else {
          if (cols[j].slot === 'color') {
            let {type, color} = data[i - 1]
            let p = new SVGPathCommander(symbol[type]).transform({
              translate: [58 + j * colWidth, pointer + 4],
              scale:0.015,
              origin: [0, 0]
            }).toString()
            this.pdf.path(p).fill(color)
          } else {
            this.pdf
              .font('MSYH')
              .fontSize(8)
              .fillColor('#333333')
              .text(data[i - 1][cols[j].key], 55 + j * colWidth, pointer + 7)
          }
        }
      }
      pointer += 30
    }
    if (!data.length) {
      this.pdf
        .moveTo(50, pointer + rowHeight)
        .lineTo(565, pointer + rowHeight)
        .stroke('#dadada')
      this.pdf
        .font('MSYH')
        .fontSize(8)
        .fillColor('#333333')
        .text('暂无数据', 300, pointer + 7)
      pointer += 30
    }
    return pointer + 20
  }

  addTree (pointer, items) {
    for (let i = 0; i < items.length; i++) {
      let { label, value, children: children1 } = items[i]
      this.pdf.font('msbold').fontSize(10).fillColor('#333333').text(label, 50, pointer)
      this.pdf.font('msbold').fontSize(10).fillColor('#333333').text(value, 332.5, pointer)
      pointer += 22
      if (children1) {
        for(let j = 0;j < children1.length;j++) {
          let { label, value, children: children2 } = children1[j]
          this.pdf.font('msbold').fontSize(10).fillColor('#333333').text(label, 50, pointer)
          this.pdf.font('msbold').fontSize(10).fillColor('#333333').text(value, 332.5, pointer)
          pointer += 22
          if (children2) {
            for(let n = 0;n < children2.length;n++) {
              let { label, value } = children2[n]
              this.pdf.font('MSYH').fontSize(10).fillColor('#333333').text(label, 60, pointer)
              this.pdf.font('MSYH').fontSize(10).fillColor('#333333').text(value, 332.5, pointer)
              pointer += 22
            }
          }
        }
      }
    }
    return pointer
  }

  addOverlapMap (pointer, image, legend, maxCount) {
    pointer += 20
    this.pdf.image(image, 50, pointer, { fit: [515, 330], align: 'center', valign: 'center' })
    this.pdf.image(legend, 232, pointer + 340, { width: 150, height: 10 })
    this.pdf
      .font('MSYH')
      .fontSize(9)
      .fillColor('#333333')
      .text('1', 225, pointer + 340)
    this.pdf
      .font('MSYH')
      .fontSize(9)
      .fillColor('#333333')
      .text(maxCount, 385, pointer + 340)
    return pointer + 390
  }

  addPhotosMap (pointer, image, legend) {
    this.pdf.image(image, 50, pointer, { fit: [420, 350], align: 'center', valign: 'center' })
    for (let i = 0; i < legend.stops.length; i++) {
      let p = pointer + i * 20
      let [value, color] = legend.stops[i]
      this.pdf
        .circle(485, p + 7, 6)
        .lineWidth(0.5)
        .fillAndStroke(color, '#333333')
      this.pdf.font('MSYH').fontSize(9).fillColor('#333333').text(`${value} ${legend.unit}`, 500, p)
    }
    this.pdf.rect(475, pointer + legend.stops.length * 20 + 10, 60, 2).fillAndStroke('#32de32')
    this.pdf
      .font('MSYH')
      .fontSize(9)
      .fillColor('#333333')
      .text(`X ${legend.ratio}`, 485, pointer + legend.stops.length * 20 + 20, {
        width: 40,
        align: 'center'
      })
    return pointer + 360
  }

  addDsmMap (pointer, image, legend, min, max) {
    this.pdf.image(image, 50, pointer, { fit: [515, 350], align: 'center', valign: 'center' })
    this.pdf.image(legend, 232, pointer + 360, { width: 150, height: 10 })
    this.pdf
      .font('MSYH')
      .fontSize(9)
      .fillColor('#333333')
      .text(`${min}米`, 195, pointer + 360)
    this.pdf
      .font('MSYH')
      .fontSize(9)
      .fillColor('#333333')
      .text(`${max}米`, 385, pointer + 360)
    return pointer + 410
  }

  addAnnotationDetail (pointer, items) {
    for(let i = 0;i < items.length;i++) {
      if (pointer + 190 > 720) {
        pointer = this.addPage()
      }
      let {name, type, color, data, image} = items[i]
      // 标注名称
      let p = new SVGPathCommander(symbol[type]).transform({
        translate: [51, pointer],
        scale:0.018,
        origin: [0, 0]
      }).toString()
      this.pdf.path(p).fill(color)
      this.pdf.font('msbold').fontSize(11).fillColor('#333333').text(name, 75, pointer + 2)
      // 测量数据
      let keys = Object.keys(data)
      let rowHeight = 24
      for(let j = 0;j < keys.length;j++) {
        if (j % 2) {
          this.pdf.rect(50, pointer + 30 + j * rowHeight, 245, rowHeight).stroke('#dedede')
        } else {
          this.pdf.rect(50, pointer + 30 + j * rowHeight, 245, rowHeight).fillAndStroke('#dedede', '#dedede')
        }
        let value = data[keys[j]]
        this.pdf.font('msbold').fontSize(9).fillColor('#333333').text(keys[j], 58, pointer + 35 + j * rowHeight)
        this.pdf.font('MSYH').fontSize(9).fillColor('#333333').text(value, 180, pointer + 35 + j * rowHeight)
      }
      // 缩略图
      image && this.pdf.image(image, 310, pointer + 30, { width: 245, height: 125 })
      pointer += 210
    }
  }
}

export default PdfCreator
