/**
 * @author heyuchang
 * @file  排行榜组件
 * @description 用户点击查看排行榜才检查授权,如果此时用户没有授权则进入授权界面
 */
//电子邮件puhalskijsemen@gmail.com
//源码网站 开vpn全局模式打开 http://web3incubators.com/
//电报https://t.me/gamecode999
//网页客服 http://web3incubators.com/kefu.html
cc.Class({
  extends: cc.Component,
  properties: {
    display: cc.Sprite,
    groupDisplay: cc.Sprite,
    _isShow: false,
    // score: 0
  },
  init(c) {

    this._controller = c
    wx.showShareMenu({
      withShareTicket: true
    })
    wx.onShareAppMessage(function () {
      return {
        title: "开局只是个农民，现在已经做到宰相",
        imageUrl: 'https://mmocgame.qpic.cn/wechatgame/LtJZOjH6Z9ibiaMlpqzldsOf46Q7TZiaysI1fwc4Oj1L3CkbCaJMAMoicibbHu2HUQkOib/0'
      }
    })
    // 监听
    wx.onAudioInterruptionEnd(() => {
      c.musicManager.pauseBg()
      c.musicManager.resumeBg()
    })
    wx.onShow((options) => {
      if (options.scene == 1044) {
        wx.postMessage({
          message: 'group',
          shareTicket: options.shareTicket
        })
        c.openGroupRank()
        this.display.node.active = false
        c.totalRank.active = false
      }
      cc.director.resume()
    })
    wx.onHide(() => {
      cc.director.pause()
    })
    // 获取最高官阶
    this.getHighestLevel()
  },
  getHighestLevel() {
   // let highLevel = wx.getStorageSync('highLevel')
    let highLevel =cc.sys.localStorage.getItem('highLevel')
    return highLevel
  },
  getHighestScore() {
    //let score = wx.getStorageSync('highScore')
    let score =cc.sys.localStorage.getItem('highScore')
    return score
  },
  // --------------- share ----------------
  onShareButton() {
    var self = this;
    wx.shareAppMessage({
      title: "我终于成为了" + this._controller.scoreMgr.levelData[this._controller.scoreMgr.level - 1].name + ",真是开心",
      // imageUrlId: 'oxEwGvClT0uldQ470pM84w',
      imageUrl: 'https://mmocgame.qpic.cn/wechatgame/LtJZOjH6Z9ibiaMlpqzldsOf46Q7TZiaysI1fwc4Oj1L3CkbCaJMAMoicibbHu2HUQkOib/0'
    })
  },
  onUsualShareButton() {
    wx.shareAppMessage({
      title: "只是个农民，现在已经做到宰相",
      // imageUrlId: 'oxEwGvClT0uldQ470pM84w',
      imageUrl: 'https://mmocgame.qpic.cn/wechatgame/LtJZOjH6Z9ibiaMlpqzldsOf46Q7TZiaysI1fwc4Oj1L3CkbCaJMAMoicibbHu2HUQkOib/0'
    })
  },
  onShakePhone() {
    wx.vibrateShort()
  },
  // ---------------分数上传---------------
  onGameOver(level, score) {
    console.log('zh:onGameOver'+level)
    //上传分数
    //打开开放域
    this.score = score
    let highLevel = level
    let highScore = score
    let self = this

    wx.postMessage({
      event: 'setScore',
      score: score,
      level: level,
    })
    
    //highLevel = wx.getStorageSync('highLevel')

    highLevel = cc.sys.localStorage.getItem('highLevel')

    highLevel = parseInt(highLevel)
    if (highLevel) {
      highLevel = highLevel < level ? level : highLevel
    } else {
      highLevel = level
    }
    //highScore = wx.getStorageSync('highScore')
    highScore = cc.sys.localStorage.getItem('highScore')


    if (highScore) {
      highScore = parseInt(highScore)
      highScore = highScore < score ? score : highScore
    } else {
      highScore = score
    }
    var highLevelName = this._controller.gameData.json.levelData[highLevel - 1].name
   
    //wx.setStorageSync('highLevel', highLevel + '')
    //wx.setStorageSync('highScore', highScore + '')
    cc.sys.localStorage.setItem('highLevel', highLevel + '');
    cc.sys.localStorage.setItem('highScore', highScore + '');
    self._controller.scoreMgr.failHighScore.string = "Your highest score:" + (highScore + '')
    var kvDataList = new Array()
    kvDataList.push({
      key: "highLevel",
      value: highLevelName,
    }, {
      key: "highScore",
      value: highScore + '',
    })
  },
  showRank() {
    wx.postMessage({
      message: 'Show',
      event: 'getRank',
    })
    this.display.node.active = true
    this._isShow = true
  },

  closeRank() {
    this.display.node.active = false
    wx.postMessage({
      message: 'Hide'
    })
    this._isShow = false
  },
  showGroupRank() {
    wx.postMessage({
      message: 'Show'
    })
    this.groupDisplay.node.active = true
    this._isShow = true
  },
  // switchRankType() {
  //   wx.postMessage({
  //     message: 'switchRank'
  //   })
  //   this._isShow = true
  // },
  closeGroupRank() {
    this.groupDisplay.node.active = false
    wx.postMessage({
      message: 'Hide'
    })
    this._isShow = false
  },
  createImage(sprite, url) {
    let image = wx.createImage();
    image.onload = function () {
      let texture = new cc.Texture2D();
      texture.initWithElement(image);
      texture.handleLoadedTexture();
      sprite.spriteFrame = new cc.SpriteFrame(texture);
    };
    image.src = url;
  },
  update() {
    if (this._isShow) {
      if(2>1){
        return;//zh
      }
      if (this.display.node.active) {
        this.display.node.getComponent(cc.WXSubContextView).update()
      }
      if (this.groupDisplay.node.active) {
        this.groupDisplay.node.getComponent(cc.WXSubContextView).update()
      }
    }
  },
  // 控制打开广告
  onReviveButton(type) {
    // 广告位
    let self = this
    this.adType = type //0表示加倍 1表示复活
    if (this.audioAd) {
      this.audioAd.show().catch(() => {
        // 失败重试
        this.audioAd.load()
          .then(() => this.audioAd.show())
          .catch(err => {
            console.log('激励视频 广告显示失败', err.errMsg)
            if (self.adType) {
              self._controller.game.onSkipRevive()
            } else {
              self._controller.scoreMgr.onLevelUpButton()
            }
          })
      })
      return
    }
    this.audioAd = wx.createRewardedVideoAd({
      adUnitId: 'adunit-482148cfeb243378'
    })
    this.audioAd.show().catch(() => {
      // 失败重试
      this.audioAd.load()
        .then(() => this.audioAd.show())
        .catch(err => {
          self.fakeShare()
        })
    })
    this.audioAd.onError(err => {
      self.fakeShare()
    })
    this.audioAd.onClose((res) => {
      console.log('zh: audioAd.onClose a1')
      if (self.adType) {
        if (res && res.isEnded || res === undefined) {
          self._controller.game.showReviveSuccess()
        } else {
          self._controller.game.askRevive()
        }
      } else {
        if (res && res.isEnded || res === undefined) {
          self._controller.scoreMgr.onLevelUpButton(2)
        }
      }
    })
  },
  fakeShare() {
    let self = this
    wx.shareAppMessage({
      title: "我已经玩到" + this._controller.scoreMgr.score + "分了，邀请你来挑战",
      // imageUrlId: 'oxEwGvClT0uldQ470pM84w',
      imageUrl: 'https://mmocgame.qpic.cn/wechatgame/LtJZOjH6Z9ibiaMlpqzldsOf46Q7TZiaysI1fwc4Oj1L3CkbCaJMAMoicibbHu2HUQkOib/0'
    })
    if (this.adType) {
      self._controller.game.showReviveSuccess()
    } else {
      self._controller.scoreMgr.onLevelUpButton(2)
    }
  },
  openBannerAdv() {
    // 创建 Banner 广告实例，提前初始化
    // let screenWidth = wx.getSystemInfoSync().screenWidth
    // let bannerHeight = screenWidth / 350 * 120
    // let screenHeight = wx.getSystemInfoSync().screenHeight - 108
    // let adUnitIds = [
    //   'adunit-510a4ec39065ef96',
    //   'adunit-29b0fa7a2db8e8cb',
    //   'adunit-4020bb9ea439e6a5'
    // ]
    // if (this.bannerAd) {
    //   this.bannerAd.destroy()
    // }
    // this.bannerAd = wx.createBannerAd({
    //   adUnitId: adUnitIds[Math.floor(Math.random() * 3)],
    //   style: {
    //     left: 0,
    //     top: screenHeight,
    //     width: 620,
    //   }
    // })
    // // 在适合的场景显示 Banner 广告
    // this.bannerAd.onLoad(() => {
    //   // console.log('banner 广告加载成功')
    // })
    // this.bannerAd.onError((e) => {
    //   console.log('banner 广告加载失败', e)
    // })
    // this.bannerAd.show()
    //   .then()
  },
  navToMiniprogram(event,custom) {
    console.log(custom)
    wx.navigateToMiniProgram({
      appId: custom
    })
  },
  closeBannerAdv() {
    if (this.bannerAd) {
      this.bannerAd.hide()
    }
  }
});