import { useState } from "react";
import type { NewWeeklyReport } from "../Types/goTypes";
import { api } from "../API/API";
import { useNavigate, useParams } from "react-router-dom";
import Button from "./Button";

export default function NewWeeklyReport(): JSX.Element {
  const [week, setWeek] = useState<number>(0);
  const [developmentTime, setDevelopmentTime] = useState<number>();
  const [meetingTime, setMeetingTime] = useState<number>();
  const [adminTime, setAdminTime] = useState<number>();
  const [ownWorkTime, setOwnWorkTime] = useState<number>();
  const [studyTime, setStudyTime] = useState<number>();
  const [testingTime, setTestingTime] = useState<number>();

  const { projectName } = useParams();
  const token = localStorage.getItem("accessToken") ?? "";

  const handleNewWeeklyReport = async (): Promise<void> => {
    const newWeeklyReport: NewWeeklyReport = {
      projectName: projectName ?? "",
      week: week,
      developmentTime: developmentTime ?? 0,
      meetingTime: meetingTime ?? 0,
      adminTime: adminTime ?? 0,
      ownWorkTime: ownWorkTime ?? 0,
      studyTime: studyTime ?? 0,
      testingTime: testingTime ?? 0,
    };

    await api.submitWeeklyReport(newWeeklyReport, token);
  };

  const navigate = useNavigate();

  return (
    <>
      <div className="border-4 border-black bg-white flex flex-col justify-start min-h-[65vh] h-fit w-[50vw] rounded-3xl overflow-scroll space-y-[2vh] p-[30px] items-center">
        <form
          onSubmit={(e) => {
            if (week === 0) {
              alert("Please enter a week number");
              e.preventDefault();
              return;
            }
            e.preventDefault();
            void handleNewWeeklyReport();
            navigate(-1);
          }}
        >
          <div className="flex flex-col items-center">
            <input
              className="w-fill h-[5vh] font-sans text-[3vh] pl-[1vw] rounded-full text-center pt-[1vh] pb-[1vh] border-2 border-black"
              type="week"
              placeholder="Week"
              onChange={(e) => {
                const weekNumber = parseInt(e.target.value.split("-W")[1]);
                setWeek(weekNumber);
              }}
              onKeyDown={(event) => {
                const keyValue = event.key;
                if (!/\d/.test(keyValue) && keyValue !== "Backspace")
                  event.preventDefault();
              }}
              onPaste={(event) => {
                event.preventDefault();
              }}
            />
            <table className="w-full text-center divide-y divide-x divide-white text-[30px]">
              <thead>
                <tr>
                  <th className="w-1/2 py-2 border-b-2 border-black">
                    Activity
                  </th>
                  <th className="w-1/2 py-2 border-b-2 border-black">
                    Total Time (min)
                  </th>
                </tr>
              </thead>
              <tbody className="divide-y divide-black">
                <tr className="h-[10vh]">
                  <td>Development</td>
                  <td>
                    <input
                      type="number"
                      min="0"
                      className="border-2 border-black rounded-md text-center w-1/2"
                      value={developmentTime}
                      onChange={(e) => {
                        setDevelopmentTime(parseInt(e.target.value));
                      }}
                      onKeyDown={(event) => {
                        const keyValue = event.key;
                        if (!/\d/.test(keyValue) && keyValue !== "Backspace")
                          event.preventDefault();
                      }}
                    />
                  </td>
                </tr>
                <tr className="h-[10vh]">
                  <td>Meeting</td>
                  <td>
                    <input
                      type="number"
                      min="0"
                      className="border-2 border-black rounded-md text-center w-1/2"
                      value={meetingTime}
                      onChange={(e) => {
                        setMeetingTime(parseInt(e.target.value));
                      }}
                      onKeyDown={(event) => {
                        const keyValue = event.key;
                        if (!/\d/.test(keyValue) && keyValue !== "Backspace")
                          event.preventDefault();
                      }}
                    />
                  </td>
                </tr>
                <tr className="h-[10vh]">
                  <td>Administration</td>
                  <td>
                    <input
                      type="number"
                      min="0"
                      className="border-2 border-black rounded-md text-center w-1/2"
                      value={adminTime}
                      onChange={(e) => {
                        setAdminTime(parseInt(e.target.value));
                      }}
                      onKeyDown={(event) => {
                        const keyValue = event.key;
                        if (!/\d/.test(keyValue) && keyValue !== "Backspace")
                          event.preventDefault();
                      }}
                    />
                  </td>
                </tr>
                <tr className="h-[10vh]">
                  <td>Own Work</td>
                  <td>
                    <input
                      type="number"
                      min="0"
                      className="border-2 border-black rounded-md text-center w-1/2"
                      value={ownWorkTime}
                      onChange={(e) => {
                        setOwnWorkTime(parseInt(e.target.value));
                      }}
                      onKeyDown={(event) => {
                        const keyValue = event.key;
                        if (!/\d/.test(keyValue) && keyValue !== "Backspace")
                          event.preventDefault();
                      }}
                    />
                  </td>
                </tr>
                <tr className="h-[10vh]">
                  <td>Studies</td>
                  <td>
                    <input
                      type="number"
                      min="0"
                      className="border-2 border-black rounded-md text-center w-1/2"
                      value={studyTime}
                      onChange={(e) => {
                        setStudyTime(parseInt(e.target.value));
                      }}
                      onKeyDown={(event) => {
                        const keyValue = event.key;
                        if (!/\d/.test(keyValue) && keyValue !== "Backspace")
                          event.preventDefault();
                      }}
                    />
                  </td>
                </tr>
                <tr className="h-[10vh]">
                  <td>Testing</td>
                  <td>
                    <input
                      type="number"
                      min="0"
                      className="border-2 border-black rounded-md text-center w-1/2"
                      value={testingTime}
                      onChange={(e) => {
                        setTestingTime(parseInt(e.target.value));
                      }}
                      onKeyDown={(event) => {
                        const keyValue = event.key;
                        if (!/\d/.test(keyValue) && keyValue !== "Backspace")
                          event.preventDefault();
                      }}
                    />
                  </td>
                </tr>
              </tbody>
            </table>
            <Button
              text="Submit"
              onClick={(): void => {
                return;
              }}
              type="submit"
            />
          </div>
        </form>
      </div>
    </>
  );
}
