import Head from 'next/head'
import Link from 'next/link'
import styles from '/styles/Home.module.css'
import fsPromises from 'fs/promises'
import path from 'path'
import type { CustomLink, LinkList } from '../interfaces/LinkTypes'

function Servers(props: LinkList) {
  const serverList = props.games
  return (
    <>
      <Head>
        <title>Neshura Servers</title>
        <meta charSet='utf-8' />
        <link rel="icon" href="/favicon.ico" />
      </Head>

      <h1 className={styles.title}>
        Server List
      </h1>

      <p className={styles.description}>
        Lists all available Services, probably up-to-date
      </p>
      <div className={styles.grid}>
        {Object.values(serverList).map((item: CustomLink) => {
          if (item.href != null) {
            return (
              <Link key={item.name} href={item.href}>
                <a className={styles.contentcard}>
                  <div className={styles.contenttitle}><h2>{item.name }</h2></div>
                  <div><p>{item.desc}</p></div> 
                  <div><p>{item.ip }</p></div> 
                  <div className={item.status == "Online" ? styles.contentonline : styles.contentoffline}><p>{item.status}</p></div> 
                </a>
              </Link>
            )
          }
          else {
            return (
              <a key={item.name} className={styles.contentcardstatic}>
                <div className={styles.contenttitle}><h2>{item.name }</h2></div>
                <div><p>{item.desc}</p></div> 
                <div><p>{item.ip}</p></div> 
                <div className={item.status == "Online" ? styles.contentonline : styles.contentoffline}><p>{item.status}</p></div> 
              </a>
            )
          }
        }

        )}
      </div>
    </>
  )
}

export async function getServerSideProps() {
  const filePath = path.join(process.cwd(), '/public/pages.json')
  const jsonData = await fsPromises.readFile(filePath)
  const list = JSON.parse(jsonData.toString())

  return { props: list }
}

export default Servers