import React from "react";
import { NextPageWithLayout } from "./_app";
import Head from "next/head";
import logoImg from "../ui/assets/logo.png";
import Image from "next/image";
import { Button, Card, Colors, Icon, Text } from "@blueprintjs/core";
import Link from "next/link";
import { useDarkMode } from "../ui/providers/dark-mode-provider";
import LANGUAGES from "./languages.json";
import { Providers } from "../ui/providers";

const styles = {
  topPanel: {
    position: "absolute" as "absolute",
    top: 0,
    right: 0,
    padding: 10,
  },
  rootContainer: {
    height: "100%",
    display: "flex",
    flexDirection: "column" as "column",
    alignItems: "center",
    justifyContent: "center",
    padding: "10%",
    textAlign: "center" as "center",
  },
  headerContainer: {
    display: "flex",
    alignItems: "center",
  },
  langsContainer: {
    marginTop: 30,
    width: "100%",
    display: "flex",
    flexWrap: "wrap" as "wrap",
    alignContent: "flex-start",
    justifyContent: "center",
  },
  langCard: {
    minWidth: 200,
    textAlign: "center" as "center",
    margin: 20,
    padding: "30px 0",
  },
};

const Index: NextPageWithLayout = () => {
  const DarkMode = useDarkMode();
  const backgroundColor = DarkMode.isDark ? Colors.DARK_GRAY3 : Colors.WHITE;

  return (
    <>
      <Head>
        <title>Esolang Park</title>
      </Head>
      {/* Buttons in the top-right */}
      <div style={styles.topPanel}>
        <Button
          minimal
          title="Toggle light mode"
          icon={<Icon icon={DarkMode.isDark ? "flash" : "moon"} />}
          onClick={DarkMode.toggleDark}
        />
      </div>
      {/* Container for center content */}
      <div style={{ ...styles.rootContainer, backgroundColor }}>
        {/* Project heading */}
        <div style={styles.headerContainer}>
          <div style={{ flexGrow: 0, marginRight: 10 }}>
            <Image src={logoImg} alt="logo" width={52} height={52} />
          </div>
          <Text tagName="div">
            <h1>Esolang Park</h1>
          </Text>
        </div>

        {/* Language cards */}
        <div style={styles.langsContainer}>
          {LANGUAGES.map(({ display, id }) => (
            <Link href={`/ide/${id}`} key={id}>
              <a style={{ all: "unset" }}>
                <Card interactive style={styles.langCard}>
                  <Text style={{ fontWeight: "bold" }}>{display}</Text>
                </Card>
              </a>
            </Link>
          ))}
        </div>
      </div>
    </>
  );
};

// Feature guide should not be shown on the home page
Index.getLayout = function getLayout(page: React.ReactNode) {
  return <Providers omitFeatureGuide={true}>{page}</Providers>;
};

export default Index;
