// components/mine/index.js
import { getMineStatus, bootCoin, coinHistory } from "../../api/mine"
import { strip, tsToStringDate } from "../../utils/util"

var app = getApp()

Component({
  /**
   * 组件的属性列表
   */
  properties: {
  },

  /**
   * 组件的初始数据
   */
  data: {
    history: [],
    symbol: "DOGE",
    icon: "/images/one.png",
    total: 0.00,
    progress: 0.00, // 0 表示已停止
    step: 0.00001,  // 步长
    price: 0.0,

    displaying: false,  // 当前是否在这个界面
    iShidden: false,
    stepTimer: null,
    syncTimer: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    show: function() {
      console.log('show:', app.globalData.token)
      if (app.globalData.token) {
        this.setData({ iShidden:true})
        this.getStatus()
        this.getHistory()
      }

      this.setData({
        displaying: true,
      })
    },
  
    hide: function() {
      if (this.data.stepTimer) {
        clearInterval(this.data.stepTimer);
      }
      if (this.data.syncTimer) {
        clearInterval(this.data.syncTimer);
      }
      this.setData({
        stepTimer: null,
        syncTimer: null,
        displaying: false,
      })
    },

    getStatus: function() {
      if (app.globalData.isLog) {
        var that = this;
        getMineStatus().then(res=>{
          that.setData({
            symbol: res.data.symbol,
            icon: res.data.icon,
            total: res.data.total,
            progress: res.data.progress,
            step: res.data.step,
            price: res.data.price,
          });

          if (that.data.progress > 0.0) {
            that.setupTimers();
          }
        }, err=>{
          console.log(err);
          that.setData({
            total: 0,
            progress: 0
          });
        });
      }
    },

    getHistory: function() {
      if (app.globalData.isLog) {
        var that = this
        coinHistory().then(res=>{
          if (res.data.length > 0) {
            res.data.forEach(function(e){
              e.ts = tsToStringDate(e.ts)
            })
          }
          that.setData({
            history: res.data,
          })
        })
      }
    },

    onLoadFun:function() {
      if (this.data.displaying && app.globalData.isLog){
        console.log("onLoadFun")
        this.getStatus()
        this.getHistory()
      }
    },

    // 客户端走进度，定时同步
    clientStep: function() {
      if (this.data.progress > 0 && this.data.step > 0) {
        var harfStep = this.data.step / 2
        var curStep = Math.floor(Math.random() * 10) * harfStep / 10 + harfStep
        var curProgress = strip(this.data.progress + curStep)
        this.setData({
          progress: curProgress,
        })
      }
    },
    
    setupTimers: function() {
      var that = this
      if (that.data.syncTimer == null) {
        that.setData({
          syncTimer: setInterval(() => {
            that.getStatus()
          }, 1000 * 60 * 2),
        })
      }

      if (that.data.stepTimer == null && that.data.step > 0.0) {
        that.setData({
          stepTimer: setInterval(() => {
            that.clientStep();
          }, 1000),
        })
      }
    },

    onClickStart: function() {
      if (this.data.progress > 0.0) {
        return
      }
      var that = this
      bootCoin().then(res=>{
        if (res.status === 200) {
          that.setData({
            symbol: res.data.symbol,
            coin: res.data.coin,
            price: res.data.price,
            step: res.data.step,
            progress: res.data.progress,
            total: res.data.total,
          })
          if (that.data.progress > 0.0) {
            that.setupTimers();
          }
        }
      }).catch(err=>{
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000,
        })
      })
    },

    // goRule: function() {
    //   wx.navigateTo({
    //     url: '/pages/mine_rule/index',
    //   })
    // },
  }
})
