import BasicPage from "../../components/basic-page-template";
import WCard from "../../components/werefox-card";
import { useEffect, useState } from "react";

function getTimes() {
  let total_ms = Date.now() - new Date("December 11, 2020 00:00:00");
  let days = Math.round(total_ms / 1000 / 60 / 60 / 24);
  let hours = (new Date(total_ms).getHours() + 6) % 24;
  let minutes = new Date(total_ms).getMinutes();
  let seconds = new Date(total_ms).getSeconds();
  return {
    days: `${days} days,`,
    hours: `${hours} hours,`,
    minutes: `${minutes} minutes,`,
    seconds: `and ${seconds}.`,
  };
}

export const getServerSideProps = async () => {
  return {
    props: {
      startTimes: getTimes(),
    },
  };
};

export default function HRT({ startTimes }) {
  const [timesArray, setTimesArray] = useState(startTimes);

  useEffect(() => {
    const interval = setInterval(() => {
      setTimesArray(getTimes(), 1000);
    });
    return () => clearInterval(interval);
  }, [getTimes, setTimesArray]);

  return (
    <BasicPage
      page_title="Alexis Werefox HRT Tracker"
      card_title="Track my HRT progress!"
    >
      <WCard>
        <p className="text-lg md:text-2xl text-center text-werefox-blue-dark dark:text-werefox-blue">
          I'm so glad you're interested!!
        </p>
        <p className="text-lg md:text-2xl text-center text-werefox-blue-dark dark:text-werefox-blue">
          I have been on HRT for:
        </p>
        <div className="text-lg md:text-2xl text-center text-werefox-blue-dark dark:text-werefox-blue grid grid-cols-1 grid-rows-4">
          {Object.keys(timesArray).map((t) => (
            <p>{timesArray[t]}</p>
          ))}
        </div>
      </WCard>
    </BasicPage>
  );
}
