import { messages } from '../core/libs/messages'
import { useRouter } from '../core/utils/router'
import { useUserStore } from '../store'
import { storeToRefs } from 'pinia'
import { computed } from 'vue'
export const usePermissions = () => {
  const userStore = useUserStore()
  const { isLogined, isDesigner, userInfo } = storeToRefs(userStore)
  const routes = [
    { path: '/pages/mine/homepage/index', meta: { canNotLogin: false, canNotDesigner: true } },
    {
      path: '/pages/material/mini-class/index',
      meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
    },
    {
      path: '/pages/material/recommend/index',
      meta: { canNotLogin: false, canNotDesigner: false, toLogin: true },
    },
    {
      path: '/pages/publish/moment/index',
      meta: { canNotLogin: false, canNotDesigner: false, toLogin: true },
    },
    {
      path: '/pages/messages/index',
      meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
    },
    {
      path: '/pages/mine/setting/index',
      meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
    },
    {
      path: '/pages/mine/homepage/statistics/index',
      meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
    },
    {
      path: '/pages/mine/points/index',
      meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
    },
    {
      path: '/pages/mine/coupons/index',
      meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
    },
    {
      path: '/pages/mine/orders/index',
      meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
    },
    // {
    //   path: '/pages/mine/agents/index',
    //   meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
    // },
    {
      path: '/pages/mine/invite/index',
      meta: { canNotLogin: false, canNotDesigner: false, toLogin: true },
    },
    {
      path: '/pages/mall/confirm-order/index',
      meta: { canNotLogin: false, canNotDesigner: true, toLogin: true },
    },
  ]
  const features = computed(() => ({
    /**
     * 1分钟了解筑巢荟
     */
    about: true,
    toDesignerHomePage: isLogined.value,
    checkInAtStoreTask: isDesigner.value,
    shareMoment: isLogined.value && isDesigner.value && userInfo.value.level.level > 1,
    personalCode: isLogined.value && isDesigner.value && userInfo.value.level.level > 1,
    /**
     * 微信代运营兑换
     */
    wechatAgentExchange: isLogined.value,
    /**
     * 案例拍摄兑换
     */
    caseExchange: isLogined.value,
    /**
     * 商城兑换
     */
    mallExchange: [isLogined.value, isDesigner.value],
  }))
  /**
   * 按钮操作权限
   */
  const clickByPermission = (
    name:
      | 'wechatAgentExchange'
      | 'caseExchange'
      | 'mallExchange'
      | 'thumbsUp'
      | 'exchange'
      | 'share'
      | 'scan'
      | 'download'
      | 'task',
    callback: () => void,
  ) => {
    const features = [
      { name: 'mallExchange', meta: { canNotLogin: false, canNotDesigner: false } },
      { name: 'wechatAgentExchange', meta: { canNotLogin: false, canNotDesigner: false } },
      /**
       * 点赞需登录
       */
      { name: 'thumbsUp', meta: { canNotLogin: false, canNotDesigner: true } },
      /**
       * 活动游学兑换
       */
      { name: 'exchange', meta: { canNotLogin: false, canNotDesigner: false } },
      { name: 'share', meta: { canNotLogin: false, canNotDesigner: false, minLevel: 2 } },
      { name: 'scan', meta: { canNotLogin: false, canNotDesigner: false, minLevel: 2 } },
      { name: 'task', meta: { canNotLogin: false, canNotDesigner: false } },
      { name: 'download', meta: { canNotLogin: false, canNotDesigner: false } },
    ]
    const feature = features.find((item) => item.name === name)
    if (feature) {
      if (!feature.meta.canNotLogin && !isLogined.value) {
        uni.showToast({ title: messages.components.toast.pleaseLogin, icon: 'none' })
        useRouter().push('/pages/login/index')
        return
      }
      if (!feature.meta.canNotDesigner && !isDesigner.value) {
        uni.showToast({ title: messages.components.toast.pleaseAuthentication, icon: 'none' })
        useRouter().push('/pages/mine/authentication/index')
        return
      }
      if (feature.meta.minLevel && userInfo.value.level.level < feature.meta.minLevel) {
        uni.showToast({ title: messages.components.toast.levelNotEnough, icon: 'none' }).then()
        return
      }
    }
    callback()
  }
  return { isLogined, isDesigner, routes, features, clickByPermission }
}
