// pages/shared/shared.js
const api = require('../../config/api.js');
const util = require('../../utils/util.js');
const atob = require('../../utils/atob')


Page({

  /**
   * 页面的初始数据
   */
  data: {
    shared_img: '',
    recommend_text: '',
    editShowStatus: false,
    loadHot: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let { companyName, vrLink, id } = options
    
    vrLink = atob(vrLink)
    this.vrLink = vrLink + `?id=${id}`
    this.companyName = companyName
    
    

    let data = await this.getBrandDetail(id)
    this.sceneUrl = data.sceneUrl
    this.shareCodeImg = data.shareWxQrCode

    this.setData({
      shared_img: data.appListPicUrl,
      layoutHeight: wx.getSystemInfoSync().windowHeight - 170
    })
    const query = wx.createSelectorQuery()
    query.select('.canvas').boundingClientRect().exec(res => {
      this.canvas_width = res[0].width
      this.canvas_height = res[0].height
      this.drawImage()
    })
  },

  getCompanyDetail () {
    // CompanyApi.getCompanyDetail(this.companyId).then(res => {
    //   console.log(res, 'company')
    //   this.setData({
    //     company: res.data
    //   })
    // })
  },
  showEdit () {
    this.setData({
      editShowStatus: true
    })
  },
  hideEdit () {
    this.setData({
      editShowStatus: false
    })
  },
  bindinput (e) {
    const { value } = e.detail
    value.substr(0,25)
    this.setData({
      recommend_text: value
    })
  },
  onShareAppMessage () {
    console.log(this.vrLink)
    return {
      path:  this.vrLink,
      imageUrl: this.data.shared_img
    }
  },
  copyLink () {
    wx.setClipboardData({
      data: decodeURIComponent(this.sceneUrl),
      success (res) {
        wx.showToast({
          title: '复制成功',
        })
      }
    })
  },
  
  getBrandDetail: async function (id, cb) {
    let res = await util.request(api.BrandDetail, { id: id })
    return res.data.brand
  },
  savePhoto () {
    wx.saveImageToPhotosAlbum({
      filePath: this.data.img_url,
      success(res) {
        wx.showModal({
          title: '图片保存成功',
          content: '图片成功保存到相册了，去发圈噻~',
          showCancel: false,
          confirmText: '好哒',
          confirmColor: '#72B9C3',
          success: (res) => {
            if (res.confirm) {
            }
          }
        })
      }
    })
  },
  async drawImage(recommend_text) {
    this.context = wx.createCanvasContext('content')
    this.context.lineJoin = 'miter'
    this.context.font = 'bold 15px sans-serif'
    this.context.setFillStyle('#fff')
    this.context.fillRect(0, 0, this.canvas_width, this.canvas_height)
    const dpr = wx.getSystemInfoSync().pixelRatio
    let cover = await this.downloadFile(this.data.shared_img)
    if (this.shareCodeImg) {
      var shareCode = await this.downloadFile(this.shareCodeImg)
    } else {
      var shareCode = null
    }
    
    const img_width = this.canvas_width * 0.8644,
          img_height = this.canvas_width * 0.8644
        const left = (this.canvas_width - img_width) / 2
        // 画圆弧矩形
        this.strokeRoundRect((this.canvas_width - img_width) / 2, (this.canvas_width - img_width) / 2, img_width, img_height, 4)
        this.context.clip()
        this.context.drawImage(cover, 0, 0, img_width, img_height)
        this.context.restore()
        this.context.fillStyle = '#131D34'
        this.context.font = 'normal bold 15px sans-serif'
        this.context.fillText(this.companyName, left, img_width + left + this.canvas_height * 15 / 460 + 21)
        this.context.restore()
        this.context.font = 'normal 300 13px sans-serif'
        this.context.fillStyle = '#79868F'
        let recommend_text1, recommend_text2
        if (recommend_text) {
          recommend_text1 = recommend_text.slice(0,11)
          recommend_text2 = recommend_text.slice(11)
        }
        this.context.fillText(recommend_text1 || '我的个性推荐语', left, img_width + left + this.canvas_height * 15 / 460 + 18 + 21)
        if (recommend_text2) {
          this.context.font = 'normal 300 13px sans-serif'
          this.context.fillStyle = '#79868F'
          this.context.fillText(recommend_text2 || '我的个性推荐语', left, img_width + left + this.canvas_height * 15 / 460 + 18 + 21 + 21)
        }
        this.context.font = 'normal bold 12px sans-serif'
        this.context.fillStyle = '#000'
        shareCode && this.context.drawImage(shareCode, left, img_width + left + this.canvas_height * 92 / 460 + 10, 60, 60)
        this.context.fillText('3D实景拍摄', left + 80, img_width + left + this.canvas_height * 92 / 460 + 20)
        this.context.fillText('手机逛遍卖场', left + 80, img_width + left + this.canvas_height * 92 / 460 + 40)
        this.context.font = 'normal 300 12px sans-serif'
        this.context.fillStyle = '#000'
        this.context.fillText('长按识别二维码  进入店铺', left + 80, img_width + left + this.canvas_height * 140 / 460 + 17)

        this.context.draw(false, () => {
          wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: this.canvas_width,
            height: this.canvas_height,
            canvasId: 'content',
            success: (res) => {
              this.setData({
                img_url: res.tempFilePath
              })
              
              setTimeout(() => {
                this.setData({loadHot: true})
              }, 100)
            }
          })
        })
        this.context.fill()
        this.context.restore()
        
  },
  submitRecommend() {
    this.drawImage(this.data.recommend_text)
    this.hideEdit()
  },
  strokeRoundRect(x, y, width, height, radius, /*optional*/ lineWidth, /*optional*/ strokeColor) {
    //圆的直径必然要小于矩形的宽高          
    if (2 * radius > width || 2 * radius > height) {
      return false;
    }

    this.context.save();
    this.context.translate(x, y);
    //绘制圆角矩形的各个边  
    this.drawRoundRectPath(width, height, radius);
    this.context.lineWidth = 0; //若是给定了值就用给定的值否则给予默认值2  
    this.context.strokeStyle = strokeColor || "#fff";
    this.context.stroke();

  },
  drawRoundRectPath(width, height, radius) {
    this.context.beginPath(0);
    //从右下角顺时针绘制，弧度从0到1/2PI  
    this.context.arc(width - radius, height - radius, radius, 0, Math.PI / 2);

    //矩形下边线  
    this.context.lineTo(radius, height);

    //左下角圆弧，弧度从1/2PI到PI  
    this.context.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);

    //矩形左边线  
    this.context.lineTo(0, radius);

    //左上角圆弧，弧度从PI到3/2PI  
    this.context.arc(radius, radius, radius, Math.PI, Math.PI * 3 / 2);

    //上边线  
    this.context.lineTo(width - radius, 0);

    //右上角圆弧  
    this.context.arc(width - radius, radius, radius, Math.PI * 3 / 2, Math.PI * 2);

    //右边线  
    this.context.lineTo(width, height - radius);
    this.context.closePath();
  },
  downloadFile (url) {
    return new Promise(resolve => {
      wx.downloadFile({
        url: url,
        success (res) {
          resolve(res.tempFilePath)
        }
      })
    })
  },


})