import { createRouter, createWebHistory } from "vue-router";
import storage, { actions } from "@/utils/storage";
import { toast } from "@/utils/toast";
import { getLocationParams } from "@/utils";

const p = getLocationParams();

const routes: Array<any> = [
  {
    path: "/:pathMatch(.*)",
    name: "404",
    redirect: "/",
  },
  {
    path: "/",
    component: () => import("../views/home/index.vue"),
    meta: {
      needLogin: false,
      keepAlive: true,
      index: 1,
    },
  },
  {
    path: "/deposit",
    component: () => import("../views/deposit/index.vue"),
    meta: {
      index: 4,
    },
  },
  {
    path: "/perfil",
    name: "perfil",
    component: () => import("../views/perfil/index.vue"),
    meta: {
      title: "Perfil",
      keepAlive: true,
      index: 2,
    },
  },
  {
    //促销活动
    path: "/activity",
    name: "activity",
    component: () => import("../views/activity/index.vue"),
    meta: {
      needLogin: false,
      index: 5,
    },
  },
  {
    //搜索页面
    path: "/search",
    name: "search",
    component: () => import("../views/search/index.vue"),
    meta: {
      needLogin: false,
      keepAlive: true,
      index: 6,
    },
  },
  {
    //游戏详情
    path: "/gameDetails",
    name: "gameDetails",
    component: () => import("../views/game-details/index.vue"),
    meta: {
      needLogin: false,
      index: 7,
    },
  },
  {
    //游戏任务活动
    path: "/gameTask",
    name: "gameTask",
    component: () => import("../views/gameTask/index.vue"),
    meta: {
      index: 8,
    },
  },
  {
    path: "/banco",
    name: "banco",
    component: () => import("../views/perfil/page/banco.vue"),
    meta: {
      index: 9,
    },
  },
  {
    path: "/betting",
    name: "betting",
    component: () => import("../views/perfil/page/betting.vue"),
    meta: {
      index: 10,
    },
  },
  {
    path: "/cashback",
    name: "cashback",
    component: () => import("../views/perfil/page/cashback.vue"),
    meta: {
      index: 11,
    },
  },

  {
    path: "/withdrawal",
    component: () => import("../views/withdrawal/index.vue"),
    meta: {
      title: "Withdrawal",
      index: 12,
    },
  },
  {
    path: "/wallet",
    component: () => import("../views/wallet/index.vue"),
    meta: {
      title: "Wallet",
      index: 13,
    },
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login/index.vue"),
    meta: {
      needLogin: false,
      index: 14,
    },
  },
  {
    path: "/register",
    name: "register",
    component: () => import("../views/register/index.vue"),
    meta: {
      needLogin: false,
      index: 15,
    },
  },
  {
    path: "/bind-phone",
    name: "bind-phone",
    component: () => import("../views/bind-phone/index.vue"),
    meta: {
      needLogin: false,
      index: 16,
    },
  },
  {
    path: "/level",
    name: "level",
    component: () => import("../views/perfil/page/level.vue"),
    meta: {
      index: 17,
    },
  },
  {
    path: "/introduceVip",
    name: "introduceVip",
    component: () => import("../views/perfil/page/introduceVip.vue"),
    meta: {
      index: 18,
    },
  },
  {
    path: "/changePassword",
    name: "changePassword",
    component: () => import("../views/changePassword/index.vue"),
    meta: {
      needLogin: false,
      index: 19,
    },
  },
  {
    path: "/password",
    name: "password",
    component: () => import("../views/password/index.vue"),
    meta: {
      needLogin: false,
      index: 20,
    },
  },
  {
    path: "/successful",
    name: "successful",
    component: () => import("../views/successful/index.vue"),
    meta: {
      index: 21,
    },
  },
  {
    //Vip 介绍
    path: "/levelReceive",
    name: "levelReceive",
    component: () => import("../views/perfil/page/levelReceive.vue"),
    meta: {
      index: 22,
    },
  },
  {
    //转盘
    path: "/turntable",
    name: "turntable",
    component: () => import("../views/turntable/index.vue"),
    meta: {
      index: 23,
    },
  },
  {
    //E-mail
    path: "/email",
    name: "email",
    component: () => import("../views/email/index.vue"),
    meta: {
      index: 24,
    },
  },
  {
    path: "/other",
    name: "other",
    component: () => import("../views/other/index.vue"),
    meta: {
      needLogin: false,
      index: 25,
    },
  },
  {
    path: "/about-us",
    name: "about-us",
    component: () => import("../views/about-us/index.vue"),
    meta: {
      needLogin: false,
      index: 26,
    },
  },
  {
    path: "/help",
    name: "help",
    component: () => import("../views/help/index.vue"),
    meta: {
      needLogin: false,
      index: 27,
    },
  },
  {
    path: "/download",
    name: "download",
    component: () => import("../views/download/index.vue"),
    meta: {
      needLogin: false,
      index: 28,
    },
  },
  {
    path: "/allGame",
    name: "allGame",
    component: () => import("../views/allGame/index.vue"),
    meta: {
      needLogin: false,
      index: 29,
    },
  },
  {
    path: "/setting",
    name: "setting",
    component: () => import("../views/setting/index.vue"),
    meta: {
      needLogin: true,
      index: 30,
    },
  },
  {
    path: "/apostas",
    name: "apostas",
    component: () => import("../views/perfil/page/apostas.vue"),
    meta: {
      needLogin: true,
      index: 31,
    },
  },
  {
    path: "/modifyPassWord",
    name: "modifyPassWord",
    component: () => import("../views/modifyPassWord/index.vue"),
    meta: {
      needLogin: true,
      index: 32,
    },
  },
  {
    //无限代理
    path: "/infiniteProxy",
    name: "infiniteProxy",
    component: () => import("../views/infiniteProxy/index.vue"),
    meta: {
      needLogin: true,
      keepAlive: false,
      index: 33,
    },
  },
  {
    //收藏
    path: "/collect",
    name: "collect",
    component: () => import("../views/collect/index.vue"),
    meta: {
      needLogin: true,
      keepAlive: false,
      index: 34,
    },
  },
  {
    // 拼单单
    path: "/loteria",
    name: "loteria",
    component: () => import("../views/loteria/index.vue"),
    meta: {
      needLogin: true,
      keepAlive: false,
      index: 35,
    },
  },
  {
    // 签到
    path: "/signIn",
    name: "signIn",
    component: () => import("../views/signIn/index.vue"),
    meta: {
      needLogin: true,
      keepAlive: false,
      index: 36,
    },
  },
  {
    // 公告详情
    path: "/details",
    name: "details",
    component: () => import("../views/details/index.vue"),
    meta: {
      needLogin: false,
      keepAlive: false,
      index: 37,
    },
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    return { top: 0 };
  },
});

router.beforeEach((to: any, from: any, next: any) => {
  const isLogin: boolean = storage.getStorage(actions.USER_TOEKN)
    ? true
    : false;

  // 设置网页标题
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  if (isLogin) {
    if (to.path == "/login" || to.path == "/register") {
      if (Object.keys(to.query).length) {
        next({ path: from.path, query: to.query });
        return;
      }
      next(from.path);
      return;
    }
  }

  if (Object.keys(to.query).length) {
    next();
    return;
  }

  if (Object.keys(from.query).length) {
    let toQuery = JSON.parse(JSON.stringify(to.query));
    toQuery = { ...from.query };
    if (to.meta.needLogin == false) {
      next({ path: to.path, query: toQuery });
    } else if (!isLogin) {
      toast("Por favor, entre!");
      next({ path: "/login", query: toQuery });
    } else {
      next({ path: to.path, query: toQuery });
    }
  } else {
    if (to.meta.needLogin == false) {
      next();
    } else if (!isLogin) {
      toast("Por favor, entre!");
      next("/login");
    } else {
      next();
    }
  }
});

router.afterEach(() => {
  if (p.fbPixelId) {
    window.fbq("track", "PageView");
    window.fbq("track", "ViewContent");
  }
});

export default router;
