//app.js
require('./utils/strophe.js')
var WebIM = require('./utils/WebIM.js').default
App({
  getRoomPage: function () {
    return this.getPage("pages/chatroom/chatroom")
  },
  getPage: function (pageName) {
    var pages = getCurrentPages()
    console.log("pages:" + pages)
    return pages.find(function (page) {
      console.log("getPage:" + page.__route__)
      return page.__route__ == pageName
    })
  },
  onLaunch: function (option) {
    var that = this
    //调用API从本地缓存中获取数据
    var userToken = wx.getStorageSync('userToken')
    var userId = wx.getStorageSync('userId')
    var tourist = wx.getStorageSync('tourist')
    if (tourist || tourist == true ){
      this.globalData.tourist = true
    }
    this.globalData.token = userToken
    this.globalData.userId = userId
    this.globalData.currentId = userId[0]
    console.log(userToken)
    
    // wx.navigateTo({
    //     url: 'pages/BloodSugerHome/BloodSugerHome'
    // })
    if (option.query) {
      if (option.query.mpid) {
        this.globalData.openInfo.mpid = option.query.mpid
        wx.setStorageSync('mpid', option.query.mpid)
      }
    }
    var mpid = wx.getStorageSync('mpid')
    if(mpid){
      this.globalData.openInfo.mpid = mpid
    }
    wx.login({
      success: function (res) {
        // success
        that.globalData.openInfo.code = res.code
      },
      fail: function (res) {
        // fail
      },
      complete: function (res) {
        // complete
      }
    })
    WebIM.conn.listen({
      onOpened: function (message) {
        console.log("onOpened")
        wx.setStorageSync('isHxOpend', true)
        // WebIM.conn.setPresence()
        // WebIM.conn.getRoster(rosters)
      },
      onPresence: function (message) {
        console.log('onPresence', message)
        var pages = getCurrentPages()
        if (message.type == "unsubscribe") {
          pages[0].moveFriend(message)
        }
        if (message.type === "subscribe") {
          //console.log('MMMMMMMMMMMMMM',message.status)
          if (message.status === '[resp:true]') {
            return
          } else {
            pages[0].handleFriendMsg(message)
          }
        }
      },
      onRoster: function (message) {
        console.log('onRoster', message)
        var pages = getCurrentPages()
        if (pages[0]) {
          pages[0].onShow()
        }
      },
      onAudioMessage: function (message) {
        console.log('onAudioMessage', message)
        var page = that.getRoomPage()
        console.log(page)
        if (message) {
          if (page && page.data != null && page.data.yourname && message.from == page.data.yourname) {
            page.receiveMsg(message, 'audio')
          } else {
            var chatMsg = that.globalData.chatMsg || []
            // if (message.type == 'txt') {
            //   var value = WebIM.parseEmoji(message.data.replace(/\n/mg, ''))
            // } else if (type == 'emoji') {
            //   var value = msg.data
            // }
            var time = WebIM.time()
            var msgData = {
              info: {
                from: message.from,
                to: message.to
              },
              username: message.from,
              yourname: message.from,
              msg: {
                type: 'audio',
                // data: value,
                url: message.url
              },
              style: '',
              time: time,
              mid: 'audio' + message.id
            }
            chatMsg = wx.getStorageSync(msgData.yourname + message.to) || []
            chatMsg.push(msgData)
            wx.setStorage({
              key: msgData.yourname + message.to,
              data: chatMsg,
              success: function () {
                //console.log('success')
              }
            })
          }
        }
      },
      onTextMessage: function (message) {
        console.log('onTextMessage', message)
        var page = that.getRoomPage()
        // console.log("yourname:", page.data.yourname)
        if (message) {
          if (page && page.data != null && page.data.yourname && message.from == page.data.yourname) {
            page.receiveMsg(message, 'txt')
          } else {
            var chatMsg = that.globalData.chatMsg || []
            var value = WebIM.parseEmoji(message.data.replace(/\n/mg, ''))
            var time = WebIM.time()
            var msgData = {
              info: {
                from: message.from,
                to: message.to
              },
              username: message.from,
              yourname: message.from,
              msg: {
                type: 'txt',
                data: value
              },
              style: '',
              time: time,
              mid: 'txt' + message.id
            }
            chatMsg = wx.getStorageSync(msgData.yourname + message.to) || []
            chatMsg.push(msgData)
            wx.setStorage({
              key: msgData.yourname + message.to,
              data: chatMsg,
              success: function () {
                console.log('setStorage success')
              }
            })
          }
        }
      },
      onEmojiMessage: function (message) {
        console.log('onEmojiMessage', message)
        var page = that.getRoomPage()
        //console.log(pages)
        if (message) {
          if (page && page.data != null && page.data.yourname && message.from == page.data.yourname) {
            page.receiveMsg(message, 'emoji')
          } else {
            var chatMsg = that.globalData.chatMsg || []
            var time = WebIM.time()
            var msgData = {
              info: {
                from: message.from,
                to: message.to
              },
              username: message.from,
              yourname: message.from,
              msg: {
                type: 'emoji',
                data: message.data
              },
              style: '',
              time: time,
              mid: 'emoji' + message.id
            }
            msgData.style = ''
            chatMsg = wx.getStorageSync(msgData.yourname + message.to) || []
            chatMsg.push(msgData)
            //console.log(chatMsg)
            wx.setStorage({
              key: msgData.yourname + message.to,
              data: chatMsg,
              success: function () {
                //console.log('success')
              }
            })
          }
        }
      },
      onPictureMessage: function (message) {
        console.log('Picture', message);
        var page = that.getRoomPage()
        if (message) {
          if (page && page.data != null && page.data.yourname && message.from == page.data.yourname) {
            //console.log("wdawdawdawdqwd")
            page.receiveImage(message, 'img')
          } else {
            var chatMsg = that.globalData.chatMsg || []
            var time = WebIM.time()
            var msgData = {
              info: {
                from: message.from,
                to: message.to
              },
              username: message.from,
              yourname: message.from,
              msg: {
                type: 'img',
                data: message.url
              },
              style: '',
              time: time,
              mid: 'img' + message.id
            }
            msgData.style = ''
            chatMsg = wx.getStorageSync(msgData.yourname + message.to) || []
            chatMsg.push(msgData)
            wx.setStorage({
              key: msgData.yourname + message.to,
              data: chatMsg,
              success: function () {
                //console.log('success')
              }
            })
          }
        }
      },
      onClosed: function (message) { 
        console.log("onClosed:", message)
        wx.setStorageSync('isHxOpend', false)
      },
      // 各种异常
      onError: function (error) {
        console.log("error:", error)
        // 16: server-side close the websocket connection
        if (error.type == WebIM.statusCode.WEBIM_CONNCTION_DISCONNECTED) {
          console.log('WEBIM_CONNCTION_DISCONNECTED 123', WebIM.conn.autoReconnectNumTotal, WebIM.conn.autoReconnectNumMax);
          if (WebIM.conn.autoReconnectNumTotal < WebIM.conn.autoReconnectNumMax) {
            return;
          }
          // wx.('Error', 'server-side close the websocket connection')
          // NavigationActions.login()/
          wx.showToast({
            title: 'server-side close the websocket connection',
            duration: 1000
          });
          // wx.redirectTo({
          //   url: '../login/login'
          // });
          return;
        }
        // 8: offline by multi login
        if (error.type == WebIM.statusCode.WEBIM_CONNCTION_SERVER_ERROR) {
          console.log('WEBIM_CONNCTION_SERVER_ERROR');
          // Alert.alert('Error', 'offline by multi login')
          // NavigationActions.login()
          wx.showModal({
            title: '提示',
            content: '你的帐号在其他设备上登录,如需使用聊天,请重新进入首页',
            showCancel: false,
            success: function (res) {
            }
          })
          // wx.showToast({
          //   title: 'offline by multi login',
          //   duration: 1000
          // })
          // wx.redirectTo({
          //   url: '../login/login'
          // })
          return;
        }
      },
      onOffline: function () {
        console.log("onOffline:")
      },
      onOnline: function () {
        console.log("onOnline:")
      }
    })
  },
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  globalData: {
    userInfo: null,
    token: '',
    userId:'',
    openInfo: {},
    artList:[],
    days:0,
    currentId:'',
    tourist:false
  },
})