//index.js
//获取应用实例
import { XYTCallEngine, HangupReason, EventType} from '../../js/XYTCallEngine.js';
import { shareData } from '../../utils/util.js';

Page({
  lastDstNumber: "",
  lastLogin: false,
  eventCallBack: null,
  rtmpServer: "",
  url: "",
  data: {
    username: "",
    password: "",
    dstNumber: "",
    isLogin: false
  },
  onShow: function (options) {
    this.url = wx.getStorageSync("url");
    this.rtmpServer = wx.getStorageSync("rtmpserver");
    if (this.rtmpServer == null || this.rtmpServer.length == 0) {
      this.rtmpServer = "rtmp://121.196.151.58:18085/live";
    }
    if (this.url == null || this.url.length == 0) {
      this.url = "wss://xyt.yunzongji.cn:443";
    }
  },
  onLoad: function (options) {
    try {
      let username = wx.getStorageSync("username");
      let password = wx.getStorageSync("password");
      if (username && password) {
        this.setData({
          username: username,
          password: password
        })
      }
    } catch (e) {
    }
    this.addNetWorkListening();
    this.addCallEventListening();
  },
  onUnload: function () {
    XYTCallEngine.removeEventCallBack(this.eventCallBack);
  },
  bindUsernameInput: function (e) {
    let value = e.detail.value;
    let username = this.parseKeyboardInput(this.data.username, value);
    if (username) {
      this.data.username = username;
      console.log(username);
      return username;
    }
    this.data.username = value;
  },
  bindPasswordInput: function (e) {
    this.data.password = e.detail.value;
  },
  bindKeybordClick: function (e) {
    let key = e.currentTarget.dataset.item;
    if (!key) return;
    let keyValues = { '0_0': '1', '0_1': '2', '0_2': '3', '1_0': '4', '1_1': '5', '1_2': '6', '2_0': '7', '2_1': '8', '2_2': '9', '3_0': '*', '3_1': '0', '3_2': '#',};
    let value = keyValues[key];
    if (!value) return;
    this.setData({
      dstNumber: this.data.dstNumber+value
    });
  },
  bindLogoutTap: function() {
    XYTCallEngine.logout();
    this.lastLogin = false;
    this.setData({
      isLogin:false,
      dstNumber:""
    });
  },
  bindLoginTap: function () {
    if (this.data.username == "advanced") {
      this.setData({
        username:""
      });
      //高级设置
      wx.navigateTo({
        url: '../advance/advance?url='+this.url+'&rtmpserver='+this.rtmpServer
      });
      return;
    }

    if (this.data.username && this.data.password && this.url) {
      this.setData({
        username: this.data.username,
        password:this.data.password
      });
      wx.setStorage({
        key: 'username',
        data: this.data.username,
      });
      wx.setStorage({
        key: 'password',
        data: this.data.password,
      });
      this.login();
    }
  },
  login: function () {
    wx.showLoading({
      title: '登录中...',
      mask: true
    })

    XYTCallEngine.setRtmpServer(this.rtmpServer);

    console.log(this.url);
    console.log(this.data.username);

    XYTCallEngine.login(this.data.username, this.data.password, "111", "111", "111", null, this.url, (success, data) => {
      wx.hideLoading();
      if (!success) {
        console.log(data);
        wx.showToast({
          title: data,
          icon: 'none'
        });
      } else {
        this.lastLogin = true;
        this.setData({
          isLogin: true
        });
        console.log("login success");
      }
    });
  },
  bindCallTap: function () {
    if (!(this.data.dstNumber && this.data.dstNumber.length > 0)) {
      if (this.lastDstNumber.length > 0) {
        this.setData({
          dstNumber: this.lastDstNumber
        });
      }
      return;
    }
    let dstNumber = this.data.dstNumber;
    this.lastDstNumber = dstNumber;
    this.setData({
      dstNumber: ""
    });
    wx.showActionSheet({
      itemList: ["音频", "视频", "会议"],
      success: (res)=>{
        let video = (res.tapIndex == 1);
        let meeting = (res.tapIndex == 2);
        if (meeting) {
          wx.navigateTo({
            url: '../meeting/meeting?dstNumber=' + dstNumber
          });
        } else {
          wx.navigateTo({
            url: '../call/call?type=outgoing&dstNumber=' + dstNumber + '&video=' + video
          });
        }
      }
    })

    
  },
  bindDeleteDstNumber: function () {
    let dstNumber = this.data.dstNumber;
    if (!(dstNumber && dstNumber.length > 0)) return;
    dstNumber = dstNumber.substr(0, dstNumber.length-1);
    this.setData({
      dstNumber: dstNumber
    });
  },
  addNetWorkListening: function () {
    wx.getNetworkType({
      success: (res) => {
        const network = res.networkType;
        this.showNetWorkToast(network);
      },
    });
    wx.onNetworkStatusChange((res) => {
      const network = res.networkType;
      this.showNetWorkToast(network);
    });
  },
  showNetWorkToast:function (type) {
    if (type == 'none') {
      wx.showToast({
        title: '当前无网络',
        icon: 'none'
      });
    } else {
      wx.showToast({
        title: '当前网络为' + type,
        icon: 'none'
      })
    }
  },
  addCallEventListening:function () {
    this.eventCallBack = (eventType, data) => {
      if (eventType == EventType.Invite) {
        //接收到呼入请求
        let callerName = data.callerName;
        let callerNumber = data.callerNumber;
        let video = data.video;
        wx.navigateTo({
          url: '../call/call?type=incoming&callerNumber=' + callerNumber + '&callerName=' + callerName + '&video=' + video
        });
      } else if (eventType == EventType.NetWorkError) {
        //网络错误
        console.log(data);
        XYTCallEngine.logout();
        wx.hideLoading();
        wx.showToast({
          title: data,
          icon: 'none'
        })
        this.setData({
          isLogin: false
        });
      }
    };
    XYTCallEngine.addEventCallBack(this.eventCallBack);
  },
  parseKeyboardInput: function (src, value) {
    if (src.length == 0 && value.length == 0) return null;
    if (value.length == 1) {
      return value.toLowerCase();
    }
    return null;
  },
  onShareAppMessage: function (res) {
    return shareData();
  }

})
