export default class boxSdk {
  constructor(name) {
    // this.cache = wx.getStorageSync('sdkStorage')
    this.heziUrl = '',
      this.name = name,
      this.api = 'https://www.27l.com/'
    this.img = null
  }

  showAdIcon(x, y, node) {
    let self = this
    // var img = wx.createImage()
    // if (self.cache && self.cache.ad_icon) {  
    //   img.src = self.cache.ad_icon.small_icon
    //   img.onload = function() {
    //     self.renderIcon(x, y, canvas, img)
    //   }

    // }

    wx.request({
      url: `${self.api}hz/common`,  
      data: {},
      success: function ({ data }) {
        console.log(data)
        self.heziUrl = data.data.hz_picture
        //获取图片进一步操作
        let icon = data.data.ad_icon.small_icon.replace(/http\:\/\//,'https://')

        // img.src = icon

        self.renderIcon(x, y, icon, node)


      }
    })


  }

  renderIcon(x, y, texture, parent) {
    var self = this
    var img = wx.createImage()
    img.src = texture
    self.img = img
    wx.downloadFile({
      url: texture,
      header: {},
      filePath: '',
      success: function (ret) {
        console.log(ret)
        var frame = new cc.SpriteFrame(ret.tempFilePath);
        // self.node.getComponent('adIcon').spriteFrame=frame;
        var node = new cc.Node('adIcon');
        var sp = node.addComponent(cc.Sprite);

        var widget = node.addComponent(cc.Widget)
        var btn = node.addComponent(cc.Button)
        btn.node.on(cc.Node.EventType.TOUCH_START, function () {
          wx.previewImage({
            urls: [self.heziUrl]
          })

          wx.request({
            url: `${self.api}hz/static?field=img`,
            data: { name: self.name },
            success: () => { }
          })
        })
        widget.isAlignTop = true
        widget.isAlignLeft = true
        widget.left = x
        widget.top = y


        sp.spriteFrame = frame
        node.parent = parent;
        console.log(widget)
        console.log(sp)
        


      }
    })



  }

  openQrcode() {
    let self = this
    wx.request({
      url: `${self.api}hz/common`,
      data: {},
      success: ({ data }) => {
        wx.previewImage({
          urls: [data.data.gzh_qr_code]
        })
      }
    })

  }


  oepnGameCenter() {
    wx.request({
      url: `${self.api}hz/common`,
      data: {},
      success: ({ data }) => {
        wx.previewImage({
          urls: [data.data.hz_picture]
        })
      }
    })
  }

  initSdk(sceneId, path) {
    let gameName = this.name
    if (sceneId == 1012) {
      wx.request({
        url: `${self.api}hz/static?field=qr`,
        data: { name: gameName },
        success: () => { },

      })
    }
  }



}