import { computed, onMounted, onUnmounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'

export default function () {
  const store = useStore()
  const router = useRouter()
  const route = useRoute()

  function init() {
    store.commit('SET_OPERATETIME')
    let lockData = localStorage.getItem('lockData')
    if (lockData) {
      lockData = JSON.parse(lockData)
      if ((lockData as any).isLock) router.push({ name: 'LockMe' })
    }
  }
  init()
  const operateTime = computed(() => store.state.app.operateTime)
  const lockScreenTime = computed(() => store.getters.systemConfig.lockScreenTime)

  let timer: any = 0
  function clearTimer() {
    clearInterval(timer)
    timer = 0
  }
  onMounted(() => {
    timer = setInterval(() => {
      // 锁屏
      const interval = 1000 * 60 * Number(lockScreenTime.value)
      // checkUserStatus()
      if (interval && new Date().getTime() - operateTime.value >= interval) {
        lockScreen()
      }
    }, 1000 * 20)
  })
  onUnmounted(clearTimer)
  // 锁屏
  function lockScreen() {
    localStorage.setItem(
      'lockData',
      JSON.stringify({
        path: route.fullPath,
        isLock: true
      })
    )
    router.push({ name: 'LockMe' })
  }
  return {
    lockScreen
  }
}
