import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { collect } from '@/api/common_api';
import * as user_api from '@/api/user_api';
import { useStore } from 'vuex';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Live',
    meta: { title: '赛事比分', hidden: true, keepAlive: true },
    component: () => import('../views/Live.vue'),
    redirect: 'live_all',
    children: [
      {
        path: 'live_all',
        name: 'LiveAll',
        component: () => import('../views/live/LiveAll.vue'),
        meta: { title: '赛事比分-全部', keepAlive: true }
      },
      {
        path: 'live_fixture',
        name: 'LiveFixture',
        component: () => import('../views/live/LiveFixture.vue'),
        meta: { title: '赛事比分-赛程', keepAlive: true }
      },
      {
        path: 'live_result',
        name: 'LiveResult',
        component: () => import('../views/live/LiveResult.vue'),
        meta: { title: '赛事比分-赛果', keepAlive: true }
      },
      {
        path: 'live_attention',
        name: 'LiveAttention',
        component: () => import('../views/live/LiveAttention.vue'),
        meta: { title: '赛事比分-关注', keepAlive: true, isLoggedIn: true }
      },
    ]
  },
  {
    path: '/schedule/:league(\\d+)?/game/:yiqiuid(\\d+)?',
    name: 'Game',
    component: () => import('../views/Game.vue'),
    meta: { title: '详情', hidden: true, keepAlive: true },
    alias: ['/game0/:yiqiuid(\\d+)?'],
    children: [
      {
        path: 'situation',
        name: 'GameSituationNm',
        component: () => import('../views/game/GameSituationNm.vue'),
        meta: { title: '动态', keepAlive: true }
      },
      {
        path: 'lineup',
        name: 'GameLineup',
        component: () => import('../views/game/GameLineup.vue'),
        meta: { title: '阵容', keepAlive: true }
      },
      {
        path: 'analysis',
        name: 'GameAnalysis',
        component: () => import('../views/game/GameAnalysis.vue'),
        meta: { title: '分析', keepAlive: true }
      },
      {
        path: 'analysisnm',
        name: 'GameAnalysisNm',
        component: () => import('../views/game/GameAnalysisNm/index.vue'),
        meta: { title: '新分析', keepAlive: true }
      },
      {
        path: 'proposal',
        name: 'GameProposal',
        component: () => import('../views/game/GameProposal/index.vue'),
        meta: { title: '方案', keepAlive: true }
      },
      {
        path: 'experts',
        name: 'GameExperts',
        component: () => import('../views/game/GameExperts.vue'),
        meta: { title: '专家', keepAlive: true }
      },
      {
        path: 'info',
        name: 'GameInfo',
        component: () => import('../views/game/GameInfo.vue'),
        meta: { title: '情报', keepAlive: true }
      },
      {
        path: 'predict',
        name: 'GamePredict',
        component: () => import('../views/game/GamePredict.vue'),
        meta: { title: '预测', keepAlive: true }
      },
      {
        path: 'bonus',
        name: 'GameBonus',
        component: () => import('../views/game/GameBonus.vue'),
        meta: { title: '红利', keepAlive: true }
      },
      {
        path: 'odds',
        name: 'GameOddsNm',
        component: () => import('../views/game/GameOddsNm.vue'),
        meta: { title: '指数', keepAlive: true }
      },
      {
        path: 'goal',
        name: 'GameGoal',
        component: () => import('../views/game/GameGoal.vue'),
        meta: { title: '进球', keepAlive: true }
      },
      {
        path: 'talk',
        name: 'GameTalk',
        component: () => import('../views/game/GameTalk.vue'),
        meta: { title: '聊球', keepAlive: true }
      },
    ]
  },
  {
    path: '/integral',
    name: 'Integral',
    component: () => import('../views/Integral.vue'),
    meta: { title: '积分排名', hidden: false, keepAlive: true },
  },
  {
    path: '/rebate',
    name: 'Rebate',
    component: () => import('../views/rebate/index.vue'),
    meta: { title: '2025不中返', hidden: false, keepAlive: true },
  }
];

const router: any = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});

// 设置路由守卫
router.beforeEach(async (to: any, from: any, next: any) => {
  if (Object.keys(from.query).indexOf('zqsx') != -1 && Object.keys(to.query).indexOf('zqsx') == -1) {
    const query = { ...to.query, zqsx: from.query.zqsx };
    const newUrl = `${to.path}?${new URLSearchParams(query)}`;
    next(newUrl);
  } else if (Object.keys(from.query).indexOf('zq') != -1 && Object.keys(to.query).indexOf('zq') == -1) {
    const query = { ...to.query, zq: from.query.zq };
    const newUrl = `${to.path}?${new URLSearchParams(query)}`;
    next(newUrl);
  } else {
    collect(to);
    const store = useStore();
    if (to.meta.isLoggedIn && JSON.stringify(store.state.userInfo) === '{}') {
      const result: any = await user_api.user_login_state({});
      if (result.res_code && result.res_code == 1) {
        next();
      } else {
        next(false);
        return location.href = `/login?return_url=${encodeURIComponent(location.origin + to.href)}`;
      }
    } else {
      next();
    }
  }
});

export default router;