import { Component } from 'react'
import { Provider, ProviderProps } from 'mobx-react'
import Taro from '@tarojs/taro'
import './app.less'
// ======store======
import userInfoStore, { UserInfoStore } from './store/userInfo'
import indexStore, { IndexStore } from './store/index'
import Book, { BookStore } from './store/book'
import classifyStore, { ClassifyStore } from './store/classIfy'
import modalStore from './store/modalStore'

// ======引入全局配置======
import { initApp, setApp } from './config'
// ======引入拦截器=========
import './interceptor'
import api from './server/index'
import { getShortBookInfo } from './server/book/short'
import { getUserInfo } from './server/wx/login'



export interface Store {
  userInfoStore: UserInfoStore,
  indexStore: IndexStore,
  bookStore: BookStore,
  classifyStore: ClassifyStore,
  modalStore: {
    visible: boolean,    // 弹窗开关
    showModal: () => void,
    hideModal: () => void
  }
}
const store: Store = {
  userInfoStore,
  indexStore,
  bookStore: Book,
  classifyStore,
  modalStore
}
class App extends Component {

  //每次打开小程序触发，后台切入不算
  onLaunch(options) {

  }
  componentDidMount() {
    console.log("APPcomponentDidMount")
    /**获取状态栏高度 */
    Taro.getSystemInfo({}).then(res => {
      let { system, statusBarHeight,platform } = res
      indexStore.setData({ navBarTop: statusBarHeight, system })//设置navbar高度和当前系统
      setApp({ system:platform })
    })
  }
  async componentDidShow(options) {
    console.log("appcomponentDidShow11111111111111111111111")
    let { scene, query ,path} = options//获取当前小程序进入的场景值
    let bookConfig = Taro.getStorageSync("bookConfig")
    if (bookConfig) {
      Book.setData({ bookConfig: JSON.parse(bookConfig) })
    }
    //阅读页面并且路径存在bookId请求数据
    if(query?.bookId && path === "pages/book/bookArticle/index"){
      getShortBookInfo(query?.bookId)
    }
    indexStore.setData({ scene, query })
    // 启动初始化
    console.log("初始化")
    initApp()//初始化全局变量
    let { appId, version, envVersion } = Taro.getAccountInfoSync().miniProgram//获取当前小程序的APPID 
    setApp({ appId, proVersion: version, envVersion,pathParams:{ scene, ...query } })
    // 初始化登录,获取登录使用的token存放用于登录使用
    await api.loginInit(appId)
  }
  componentDidHide() { }

  componentDidCatchError() { }
  render() {
    console.log("store.userInfoStore.userInfo", store.userInfoStore.userInfo)
    return <Provider store={store}>
      {(this.props as ProviderProps).children}
    </Provider >
  }
}

export default App
