let location = require("../../../mybehaviors/location/location");
let app = getApp();

var angleX = Math.PI / 1000
var angleY = Math.PI / 1000
var fristclientX = 0;
var fristclientY = 0;
var fristtime = 0
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    address: "",
    city: '',
    point: {
      lat: "",
      lng: "",
    },
    
    bottomList: [],//底部热门应用
    navigateList: [],

    timer: '', //定时器任务
    tagEle: [],
    iPnoneBottomBol: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getiPnoneBottom();
    this.getAppBottom();
    this.getAppNavigate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init();
  },
  refreshList(){
    // 更新区域应用
    this.getAppData(true)
  },
  getAppData(_is_refresh) {
    let urlStr = app.getNetAddresss('plugin.tg-decorate.frontend.app.index');
    app._postNetWork({
      url: urlStr,
      data: {
        page_size: 30,
        is_refresh: _is_refresh || false,
        longitude: this.data.point.lng,
        latitude: this.data.point.lat,
        city: this.data.city,
        district: this.data.address
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let list = res.data.data;
          list.forEach((item, index) => {
            this.data.tagEle[index].title = item.title
            this.data.tagEle[index].color = item.font_color
            this.data.tagEle[index].logo = item.logo
            this.data.tagEle[index].link = item.mini
          });
        } else {
          app.tips(res.msg)
          wx.reLaunch({
            url: "/packageG/index/index",
          });
        }
      },
      fail: function (res) {}
    });
  },
  getAppBottom() {
    let urlStr = app.getNetAddresss('plugin.tg-decorate.frontend.app.bottom');
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            bottomList: res.data,
          });
        } else {
          app.tips(res.msg)
          wx.reLaunch({
            url: "/packageG/index/index",
          });
        }
      },
      fail: function (res) {}
    });
  },
  getAppNavigate() {
    let urlStr = app.getNetAddresss('plugin.tg-decorate.frontend.navigate.index');
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            navigateList: res.data,
          });
        } else {
          app.tips(res.msg)
          wx.reLaunch({
            url: "/packageG/index/index",
          });
        }
      },
      fail: function (res) {}
    });
  },
  gotoUrl(e){
    let _link = e.currentTarget.dataset.link;
    if(!_link) return;
    try {
      wx.navigateTo({
        url: _link
      });
    } catch (error) {
      wx.reLaunch({
        url: _link
      });
    }
  },
  tosearch() {
    if (!this.data.city) {
      wx.showToast({
        icon: 'none',
        title: '请先选择城市',
        duration: 1000
      });
      return;
    }
    wx.navigateTo({
      url: '/packageI/tagCloud/search/index?city=' + this.data.city + '&point=' + JSON.stringify(this.data.point)
    });
  },
  tolocation() {
    wx.navigateTo({
      url: '/packageC/o2o/o2oLocation/o2oLocation?tag=tagCloud'
    });
  },

  init: function () {
    var tagEle = []
    for (let i = 0; i < 30; i++) {
      tagEle.push({
        title: '成为合伙人',
        color: '#fff',
        logo: '',
        x: 0,
        y: 0,
        z: 0,
        s: 0,
        o: 1,
        f: 15,
        angleX: 0,
        angleY: 0
      })
    }

    for (var i = 0; i < tagEle.length; i++) {
      var fallLength = 100 //圆的焦点

      var k = (2 * (i + 1) - 1) / tagEle.length - 1;
      //计算按圆形旋转
      var a = Math.acos(k);
      var b = a * Math.sqrt(tagEle.length * Math.PI);
      //计算元素x，y坐标
      var numx = 140 * Math.sin(a) * Math.cos(b)
      var numy = 140 * Math.sin(a) * Math.sin(b);
      var numz = 280 * Math.cos(a);

      // console.log(numo)
      //计算元素缩放大小
      tagEle[i].x = numx * 2
      tagEle[i].y = numy * 2
      tagEle[i].z = numz
      tagEle[i].s = 350 / (640 - tagEle[i].z)
    }
    //动画

    this.data.timer = setInterval(() => {
      this.setBalls(tagEle)
    }, 25)
    this._getLocation((res) => {
      this.setData({
        address: res.address_component && res.address_component.district||res.city
      });
      this.getAppData();
    }, (err) => {
      let address_on = wx.getStorageSync('address-point');
      this.setData({
        address: address_on.address_component && address_on.address_component.district||address_on.city
      });
      this.getAppData();
    });

  },
  setBalls(tagEle) {
    var interBoolean = true;
    if (interBoolean == true) {
      interBoolean = false
      for (var i = 0; i < tagEle.length; i++) {
        tagEle[i].s = 350 / (650 - tagEle[i].z) //字体大小
        var cos = Math.cos(angleY); //返回余弦值
        var sin = Math.sin(angleY); //返回正弦值
        var y1 = tagEle[i].y * cos - tagEle[i].z * sin;
        var z1 = tagEle[i].z * cos + tagEle[i].y * sin;
        tagEle[i].y = y1;
        tagEle[i].z = z1;
        /**
         * ++++ 为左上
         * ---- 为右下
         * ++-- 为右上
         * --++ 为左下
         */
        var cos = Math.cos(angleX); //返回余弦值
        var sin = Math.sin(angleX); //返回正弦值
        var x1 = tagEle[i].x * cos - tagEle[i].z * sin;
        var z1 = tagEle[i].z * cos + tagEle[i].x * sin;
        tagEle[i].x = x1;
        tagEle[i].z = z1;
      }
      this.setData({
        tagEle: tagEle
      })
      interBoolean = true
    }
  },

  touchmove(event) {
    // console.log(event)
    let _clientX = event.changedTouches[0].clientX - fristclientX;
    let _clientY = event.changedTouches[0].clientY - fristclientY;

    angleX = -_clientX * 0.0001
    angleY = -_clientY * 0.0001

    this.setBalls(this.data.tagEle)
  },
  touchStart: function (event) {
    fristclientX = event.changedTouches[0].clientX;
    fristclientY = event.changedTouches[0].clientY;
    fristtime = new Date().getTime();
    clearInterval(this.data.timer);
  },
  touchEnd: function (event) {
    var time = new Date().getTime() - fristtime;
    fristclientX = event.changedTouches[0].clientX - fristclientX || 2;
    fristclientY = event.changedTouches[0].clientY - fristclientY || 2;

    //  if((fristclientX > 0 && fristclientY > 0)||(fristclientX < 0 && fristclientY < 0)){
    //   fristclientX = -fristclientX
    //   fristclientY = -fristclientY
    //  }

    /**
     * ++ 右下 angleY+ angleX+
     * -- 左上 angleY- angleX-
     * -+ 左下 angleY+ angleX-
     * +- 右上 angleY- angleX+
     */
    /**
     * ++++ 为左上
     * ---- 为右下
     * ++-- 为右上
     * --++ 为左下
     * angleX - angleX - angleY - angleY
     * 
     * X大 y轴
     * Y大 x轴
     */
    if (time == 0) {
      time = 100
    }

    angleX = -fristclientX / time * 0.1
    angleY = -fristclientY / time * 0.1

    angleX = angleX > 0 ? Math.min(angleX, 0.01) : Math.max(angleX, -0.01);
    angleY = angleY > 0 ? Math.min(angleY, 0.01) : Math.max(angleY, -0.01);
    this.data.timer = setInterval(() => {
      this.setBalls(this.data.tagEle)
    }, 25)
    // if(fristclientX > fristclientY){
    //   angleY = fristclientX/time * -0.1
    //   angleX = fristclientX/time * -0.1
    // }else{
    //   angleY = fristclientY/time * -0.1
    //   angleX = fristclientY/time * -0.1
    // }


    // tagEle
  },
  getiPnoneBottom() {
    try {
      var value = wx.getStorageSync("iPhoneBottom");
      if (value) {
        this.setData({
          iPnoneBottomBol: true,
        });
      }
    } catch (e) {}
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    let e = this;
    clearInterval(e.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let e = this;
    clearInterval(e.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})