import Button from "./Button";
import DeleteUser from "./DeleteUser";
import UserProjectListAdmin from "./UserProjectListAdmin";
import { useState } from "react";
import InputField from "./InputField";
import ChangeUsername from "./ChangeUsername";
import { StrNameChange } from "../Types/goTypes";

function UserInfoModal(props: {
  isVisible: boolean;
  username: string;
  onClose: () => void;
}): JSX.Element {
  const [showInput, setShowInput] = useState(false);
  const [newUsername, setNewUsername] = useState("");
  const [newPassword, setNewPassword] = useState("");
  if (!props.isVisible) {
    return <></>;
  }

  const handleChangeNameView = (): void => {
    if (showInput) {
      setShowInput(false);
    } else {
      setShowInput(true);
    }
  };

  const handleClickChangeName = (): void => {
    if (newUsername === "") return;
    if (
      confirm(
        `Do you really want to change username of ${props.username} to ${newUsername}?`,
      )
    ) {
      const nameChange: StrNameChange = {
        prevName: props.username,
        newName: newUsername.replace(/ /g, ""),
      };
      ChangeUsername({ nameChange: nameChange });
    } else {
      alert("Name was not changed!");
    }
  };

  const handleClickChangePassword = (): void => {
    if (newPassword === "") return;

    if (
      confirm(`Are you sure you want to change password of ${props.username}?`)
    ) {
      //TODO: insert change password functionality
      alert("Not implemented yet");
      setNewPassword("");
    } else {
      alert("Password was not changed!");
    }
  };

  return (
    <div
      className="fixed inset-0 bg-black bg-opacity-30 backdrop-blur-sm 
      flex justify-center items-center"
    >
      <div className="border-4 border-black bg-white rounded-2xl text-center flex flex-col">
        <div className="mx-10">
          <p className="font-bold text-[30px]">{props.username}</p>
          <p
            className="mb-[10px] hover:font-bold hover:cursor-pointer underline"
            onClick={handleChangeNameView}
          >
            (Change Username or Password)
          </p>
          {showInput && (
            <div>
              <InputField
                label={"New username"}
                type={"text"}
                value={newUsername}
                onChange={function (e): void {
                  setNewUsername(e.target.value);
                }}
                placeholder={"Username"}
              />
              <div className="m-4"></div>
              <InputField
                label={"New password"}
                type={"password"}
                value={newPassword}
                onChange={function (e): void {
                  setNewPassword(e.target.value);
                }}
                placeholder={"Password"}
              />
              <Button
                text={"Change"}
                onClick={function (): void {
                  handleClickChangeName();
                  handleClickChangePassword();
                }}
                type={"submit"}
              />
            </div>
          )}
          <h2 className="font-bold text-[20px]">Member of these projects:</h2>
          <UserProjectListAdmin username={props.username} />
          <div className="items-center space-x-6">
            <Button
              text={"Delete"}
              onClick={function (): void {
                if (
                  window.confirm("Are you sure you want to delete this user?")
                ) {
                  DeleteUser({
                    usernameToDelete: props.username,
                  });
                }
              }}
              type="button"
            />
            <Button
              text={"Close"}
              onClick={function (): void {
                setNewUsername("");
                setShowInput(false);
                props.onClose();
              }}
              type="button"
            />
          </div>
        </div>
      </div>
    </div>
  );
}

export default UserInfoModal;
