import NotFound from "../pages/NotFound.svelte";
import ProfilePage from "../pages/profile/ProfilePage.svelte";
import SplashPage from "../pages/SplashPage.svelte";
import TwoFactorAuthentication from '../pages/TwoFactorAuthentication.svelte';
import UnauthorizedAccessPage from '../pages/UnauthorizedAccessPage.svelte';
import { wrap } from 'svelte-spa-router/wrap'
import Game from '../pages/game/Game.svelte';
import Ranking from '../pages/game/Ranking.svelte';
import GameSpectator from '../pages/game/GameSpectator.svelte';



export const primaryRoutes = {
  '/': SplashPage,
  '/2fa': TwoFactorAuthentication,
  '/game': wrap({
    component: Game,
    conditions: [
      async(detail) => {
        const user = await fetch('http://' + process.env.WEBSITE_HOST  + ":" + process.env.WEBSITE_PORT + '/api/v2/user')
        .then((resp) => resp.json())

        console.log('in /profile what is in user')
        console.log(user)

        if (user && user.username)
          return true;
        else
          return false;
      }
    ]
  }),
  '/spectator': wrap({
    component: GameSpectator,
    conditions: [
      async(detail) => {
        const user = await fetch('http://' + process.env.WEBSITE_HOST  + ":" + process.env.WEBSITE_PORT + '/api/v2/user')
        .then((resp) => resp.json())

        console.log('in /profile what is in user')
        console.log(user)

        if (user && user.username)
          return true;
        else
          return false;
      }
    ]
  }),
  '/ranking': wrap({
    component: Ranking,
    conditions: [
      async(detail) => {
        const user = await fetch('http://' + process.env.WEBSITE_HOST  + ":" + process.env.WEBSITE_PORT + '/api/v2/user')
        .then((resp) => resp.json())

        console.log('in /profile what is in user')
        console.log(user)

        if (user && user.username)
          return true;
        else
          return false;
      }
    ]
  }),
  '/profile': wrap({
    component: ProfilePage,
    conditions: [
      async(detail) => {
        const user = await fetch('http://' + process.env.WEBSITE_HOST  + ":" + process.env.WEBSITE_PORT + '/api/v2/user')
        .then((resp) => resp.json())

        console.log('in /profile what is in user')
        console.log(user)

        if (user && user.username)
          return true;
        else
          return false;
      }
    ]
  }),
  '/profile/*': wrap({
    component: ProfilePage,
    conditions: [
      async(detail) => {
        const user = await fetch('http://' + process.env.WEBSITE_HOST  + ":" + process.env.WEBSITE_PORT + '/api/v2/user')
        .then((resp) => resp.json())

        console.log('in /profile/* what is in user')
        console.log(user)

        if (user && user.username)
          return true;
        else
          return false;
      }
    ]
  }),
  '/unauthorized-access': UnauthorizedAccessPage,
  '*': NotFound
};
