var keyHeight = 0;
const app = getApp();
import openIM from "../../../../utils/openim"
const {
  baseURL
} = app.globalData;
Page({
  data: {
    scrollHeight: '90vh',
    inputBottom: 0,
    msgList: [],
    inputVal: '',
    refer: false,
    textareaHeight: 8,
    lineCount: 1,
    isEmoji: true,
    isMore: true,
    focus: false,
    emojiList: [
      "😀", "😁", "😂", "🤣", "😃", "😄", "😅", "😆", "😉", "😊", "😋", "😎", "😍", "😘", "😗", "😙", "😚", "☺️", "🙂", "🤗", "🤩", "🤔", "🤨", "😐", "😑", "😶", "🙄", "😏", "😣", "😥", "😮", "🤐", "😯", "😪", "😫", "😴", "😌", "😛", "😜", "😝", "🤤", "😒", "😓", "😔", "😕", "🙃", "🤑", "😲", "☹️", "🙁", "😖", "😞", "😟", "😤", "😢", "😭", "😦", "😧", "😨", "😩", "🤯", "😬", "😰", "😱", "😳", "🤪", "😵", "😡", "😠", "🤬", "😷", "🤒", "🤕", "🤢", "🤮", "🤧", "😇", "🤠", "🤡", "🤥", "🤫", "🤭", "🧐", "🤓", "😈", "👿", "👹", "👺", "💀", "👻", "👽", "🤖", "💩", "😺", "😸", "😹", "😻", "😼", "😽", "🙀", "😿", "😾"
    ],
    direction: {
      1: '东',
      2: '南',
      3: '西',
      4: '北'
    },
  },
  onLoad(options) {
    const now = Date.parse(new Date());
    const {
      userID,
      showname,
      faceurl
    } = options;
    wx.setNavigationBarTitle({
      title: showname
    })
    const safeHeight = app.globalData.systemInfo.screenHeight - app.globalData.systemInfo.safeArea.bottom
    this.setData({
      userID,
      now,
      safeHeight,
      inputBottom: safeHeight,
      faceurl
    })
    app.getMessage(false, this);
    app.readMessage();
    app.onMessage(this);
    const that = this;
    wx.getStorage({
      key: 'userInfo',
      success(res) {
        const {
          avatar
        } = res.data;
        that.setData({
          avatar
        })
      }
    });
  },
  focus(e) {
    keyHeight = e.detail.height;
    this.setData({
      isEmoji: true,
      isMore: true
    });
    this.onfocus(false, 208);
  },
  onfocus(flag, size) {
    const {
      msgList,
      lineCount,
      safeHeight
    } = this.data;
    if (flag) {
      this.setData({
        scrollHeight: `90vh - ${(lineCount)*8}vw - ${size}px + ${safeHeight}px`,
        inputBottom: size * 2,
      })
    } else {
      this.setData({
        scrollHeight: `90vh - ${(lineCount)*8}vw - ${keyHeight}px + ${safeHeight}px`,
        inputBottom: keyHeight * 2 - safeHeight,
      })
    }
    this.setData({
      toView: 'msg-' + [msgList.length - 1],
    })
  },
  bindlinechange(e) {
    const {
      lineCount
    } = e.detail;
    const {
      msgList,
      inputVal,
      safeHeight,
      isEmoji,
    } = this.data;
    if (lineCount < 4 && inputVal) {
      let textareaHeight = 8 * lineCount;
      this.setData({
        textareaHeight,
        lineCount,
        scrollHeight: `90vh - ${(lineCount)*8}vw - ${!isEmoji?208:keyHeight}px + ${safeHeight}px`,
      });
      this.setData({
        toView: 'msg-' + [msgList.length - 1],
      })
    }
  },
  blur(e) {
    const {
      cursor
    } = e.detail;
    const {
      msgList,
      lineCount,
      safeHeight,
      isEmoji,
      isMore
    } = this.data;
    if (isEmoji && isMore) {
      this.setData({
        scrollHeight: `90vh - ${(lineCount-1)*8}vw`,
        inputBottom: safeHeight,
      })
    } else {
      this.setData({
        scrollHeight: isMore ? `90vh - ${(lineCount-1)*8}vw - 208px` : `90vh - ${(lineCount-1)*8}vw - 104px`,
        inputBottom: isMore ? 416 : 208,
      })
    }
    this.setData({
      toView: 'msg-' + [msgList.length - 1],
      cursor,
    })
  },
  bindinput(e) {
    const inputVal = e.detail.value
    this.setData({
      inputVal
    })
  },
  sendClick(e) {
    const {
      inputVal
    } = this.data;
    this.setData({
      lineCount: 1
    })
    app.sendMessage(inputVal, this);
  },
  previewImage(e) {
    var current = e.target.dataset.src;
    var imgList = [];
    for (let i = 0; i < this.data.msgList.length; i++) {
      if (this.data.msgList[i].contentType == 102) {
        imgList.push(this.data.msgList[i].content.sourcePicture.url);
      }
    }
    wx.previewImage({
      current: current, //当前点击的图片链接
      urls: imgList //图片数组
    })
  },
  onMessage() {
    app.onMessage(this);
  },
  bindscrolltoupper() {
    const {
      refer
    } = this.data;
    if (refer) return
    app.getMessage(true, this);
  },
  choicePhote() {
    this.chooseWxImage('album');
  },
  choiceCarema() {
    this.chooseWxImage('camera');
  },
  chooseWxImage(type) {
    var that = this;
    wx.chooseMedia({
      count: 9,
      mediaType: ['image'],
      sourceType: [type],
      camera: 'back',
      success(res) {
        that.uploadWx(res, type);
      }
    })
  },
  uploadWx(res, type) {
    const that = this;
    const userInfo = wx.getStorageSync('userInfo');
    res.tempFiles.map(item => {
      wx.uploadFile({
        filePath: item.tempFilePath,
        name: 'file',
        url: baseURL + '/openim_upload',
        header: {
          "token": userInfo.token
        },
        success(res) {
          let url = JSON.parse(res.data).data;
          if (type == 'camera') {
            setTimeout(() => {
              app.createImage(url, that);
            }, 500)
          } else {
            app.createImage(url, that);
          }
        }
      })
    })
  },
  onUnload() {
    app.globalData.userID = "";
    keyHeight = 0;
  },
  openEmoji() {
    this.setData({
      isEmoji: false,
      isMore: true
    });
    this.onfocus(true, 208);
  },
  closeEmoji() {
    this.setData({
      isEmoji: true,
      focus: true
    })
  },
  closeText() {
    const {
      msgList,
      lineCount,
      safeHeight,
    } = this.data;
    this.setData({
      scrollHeight: `90vh - ${(lineCount-1)*8}vw`,
      inputBottom: safeHeight,
      toView: 'msg-' + [msgList.length - 1],
      isEmoji: true,
      isMore: true
    })
  },
  emojiInput(e) {
    const {
      cursor,
      inputVal
    } = this.data;
    const {
      emoji
    } = e.currentTarget.dataset;
    let newVal = inputVal.slice(0, cursor) + emoji + inputVal.slice(cursor);
    this.setData({
      inputVal: newVal,
      cursor: cursor + 2
    });
  },
  openMore() {
    this.setData({
      isMore: false,
      isEmoji: true
    })
    this.onfocus(true, 104);
  },
  closeMore() {
    this.setData({
      isMore: true,
      focus: true
    })
  },
  goHouse(e) {
    const data = e.currentTarget.dataset.data.data;
    wx.navigateTo({
      url: '../../../home/pages/house/detail/detail',
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data
        })
      }
    })
    console.log(e);
  }
})