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'
// 引入兼容性工具
import { getAccountInfoSync } from './utils/compatibility'
//引入官方小说阅读插件
const novelPlugin = Taro.requirePlugin('novel-plugin')


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) {
 // 监听进入插件页事件
    novelPlugin.onPageLoad(this.onNovelPluginLoad)
  }
  onNovelPluginLoad(data) {
    console.log('onNovelPluginLoad', data)
  // data.id - 阅读器实例 id，每个插件页对应一个阅读器实例
  const novelManager = novelPlugin.getNovelManager(data.id)
  
  // getId() 返回当前阅读器实例 id
  console.log('id:', novelManager.getId())

  // 设置目录状态
  novelManager.setContents({
    contents: [
      {
        index: 0, // 第一章
        status: 0, // 免费
      },
      {
        index: 1, // 第二章
        status: 2, // 未解锁
      },
      {
        index: 2, // 第三章
        status: 1, // 已解锁
      },
    ],
  })

  // 监听用户行为事件
  novelManager.onUserTriggerEvent(res => {
    console.log('onUserTriggerEvent', res.event_id, res)
  })
}
  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("appcomponentDidShow",options)
    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 accountInfo = getAccountInfoSync().miniProgram//获取当前小程序的APPID 
    let { appId, version, envVersion } = accountInfo;
    setApp({ 
      appId, 
      proVersion: version, 
      envVersion: envVersion as "develop" | "trial" | "release",
      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