import React, { Component } from 'react'
import Taro, { getCurrentInstance } from '@tarojs/taro'
import { StoreRoot, store } from './store'
import { deepCopy } from './utils'
import request, { apis } from './utils/request'
import { trackUserSource } from './utils/track'
import './app.less'

class App extends Component {
  updateTracing;

  onLaunch (options) {
    // 此处获取不到 state, 因为 app 不会刷新
    const { setSysInfo } = store.getModel('sys').getState()

    // 预拉取数据
    wx.getBackgroundFetchData({
      fetchType: 'pre',
      success (res) {
        if (res.errMsg === 'getBackgroundFetchData:ok') {
          const data = JSON.parse(res.fetchedData)
          if (data.code === 1000) {
            setSysInfo(data.data)
          }
        }
      }
    })
  }

  // 在此处是为了兼容热启动
  componentDidShow () {
    const { setUser } = store.getModel('user').getState()

    // 保存刚进入系统时的router
    const router = getCurrentInstance().router

    // 如果是分享或者扫码
    // scene 与其它参数时互斥的. 有 scene 说明时扫码进来的
    const { from = '', recommender = '', scene = '' } = router.params

    // 登录
    Taro.login({
      success: res => {
        if (res.errMsg === 'login:ok') {
          request({ ...apis.login, params: { code: res.code, from, recommender, scene } }).then(x => {
            setUser(x.person)
            Taro.setStorage({ key: 'token', data: x.token })
            Taro.setStorage({ key: 'sessionKey', data: x.sessionKey })

            // 埋点
            this.updateTracing = trackUserSource(router, x.scene)
          })
        }
      }
    })
  }

  componentDidHide () {
    if (this.updateTracing) {
      this.updateTracing()
    }
  }

  componentWillUnmount () {
    if (this.updateTracing) {
      this.updateTracing()
    }
  }

  // this.props.children 是将要会渲染的页面
  render () {
    return (
      <StoreRoot>
        {this.props.children}
      </StoreRoot>
    )
  }
}

export default App
