import { useEffect, useState } from "react";
import { ErrorBoundary } from "react-error-boundary";
import dynamic from "next/dynamic";
import Head from "next/head";
import { NextSeo } from "next-seo";
import Layout from "../components/Layout";
import Terminal from "../components/Terminal";
import { styled } from "../lib/styles/stitches.config";
import type { ReactElement, ComponentProps } from "react";

// obviously, an interactive VNC display will not work even a little bit server-side
const VNC = dynamic(() => import("../components/VNC"), { ssr: false });

// https://github.com/jakejarvis/y2k
const SOCKET_PROXY = "wss://y2k.pipe.fail";

const Wallpaper = styled("main", {
  display: "flex",
  width: "100%",
  minHeight: "500px",
  padding: "1.5em 0",
  justifyContent: "center",
  alignItems: "center",
  backgroundRepeat: "repeat",
  backgroundPosition: "center",
});

const DOS = styled(Terminal, {
  height: "400px",
  width: "100%",
  maxWidth: "700px",
});

const Wrapper = ({ style, ...rest }: ComponentProps<typeof Wrapper>) => {
  const [wallpaperUrl, setWallpaperUrl] = useState("");

  // set a random retro Windows ME desktop tile for the entire content area
  useEffect(() => {
    setWallpaperUrl(`/static/images/y2k/tiles/tile_${Math.floor(20 * Math.random())}.png`);
  }, []);

  return <Wallpaper style={{ backgroundImage: wallpaperUrl ? `url(${wallpaperUrl})` : "", ...style }} {...rest} />;
};

const Y2K = () => {
  // print a fancy console message (in browser only) just for funsies
  useEffect(() => {
    console.log(
      `
%c🤓 %cHey there, fellow nerd!%c Looking for the magic behind this page?

%cCheck out this post: https://jarv.is/notes/y2k-sandbox/

...or the source code here: https://github.com/jakejarvis/y2k
`,
      "font-size: 20px",
      "color: black; background: yellow; font-size: 20px",
      "font-size: 20px",
      "font-size: 15px"
    );
  }, []);

  return (
    <>
      <Head>
        {/* TODO: favicon doesn't change back to normal when navigating away from page */}
        <link
          href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAA2tJREFUOE+Nk21sU2Ucxc/T3Xu73vXedl3fNtuCNm4oW9jY1M2hRAIBZCSGxMWFOWIMBDRGzNzQ+PJh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P/BEU2W1FE/2aJAAAAAElFTkSuQmCC"
          rel="icon"
          type="image/png"
        />
      </Head>

      <NextSeo
        title="Y2K Sandbox: Powered by Windows Me™ 💾"
        description="My first website on a Windows Me-powered time machine. You've been warned."
        openGraph={{
          title: "Y2K Sandbox: Powered by Windows Me™",
        }}
      />

      <ErrorBoundary fallback={<DOS>Oh no, it looks like something's gone VERY wrong. Sorry about that!</DOS>}>
        <VNC server={SOCKET_PROXY} />
      </ErrorBoundary>
    </>
  );
};

// disable layout's default styles so the wallpaper component can go edge-to-edge:
Y2K.getLayout = (page: ReactElement) => {
  return (
    <Layout
      container={false}
      css={{
        // classic windows 9x cursor easter egg
        cursor: `url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAZklEQVR4AWIAgn/uBT6A9uoAAwAQiIJo97/0Rgy0ANoJH8MPeEgtqwPQEACqCoQHAKECQKgAECoAhAoAoQJAqAAQxh1oPQfcW3kJpxHtL1AAHAwEwwdYiH8BIEgBTBRAAAEEEEAAG7mRt30hEhoLAAAAAElFTkSuQmCC") 2 1, auto`,

        "a:hover, button": {
          // windows 9x hand cursor
          cursor: `url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgAgMAAAAOFJJnAAAACVBMVEVHcEwAAAD///8W1S+BAAAAAXRSTlMAQObYZgAAAEdJREFUeAFjoAVghTGkHIhghMAYmQEwxlIYYxlYlSiQMQEsELUKyli1ahWYwQZjMGIwGLKQGA4QA1EYEP0rGVAZrKGhSF4BAHw/HsVwshytAAAAAElFTkSuQmCC") 16 12, auto`,
        },
      }}
    >
      <Wrapper>{page}</Wrapper>
    </Layout>
  );
};

export default Y2K;
