import { useState } from "react";
import { useNavigate, useLocation } from "react-router-dom";

import { toast } from "react-toastify"

import useAuth from "../../auth/auth";

import FormField from "../../components/FormField"
import "./login.css"


const Login = () => {
  const { setToken } = useAuth();
  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");
  const navigate = useNavigate();
  const location = useLocation();

  const [usernameEmpty, setUsernameEmpty] = useState(false);
  const [passwordEmpty, setPasswordEmpty] = useState(false);

  const from = location.state?.from?.pathname || "/";

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();

    setUsernameEmpty(!username);
    setPasswordEmpty(!password);

    if (!username || !password) {
      return;
    }

    // TODO: request to API
    if ((username === "admin" && password === "1234") || (username === "a" && password === "a")) {
      const token = "todo.jwt.token";
      setToken(token);
      navigate(from, { replace: true });
    } else {
      toast.error("Wrong login or password");
    }
  };

  return (
    <div className="login">
      <div className="login-left">
        <video className='videoTag' autoPlay loop muted>
          <source src={"./test.mp4"} type='video/mp4' />
        </video>
      </div>
      <div className="login-right">
        <h2 className="login-logo-block">
          <img className="login-logo" src="./myautag.png" alt="Picrinth logo" />
        </h2>
        <div className="login-login-block">
          <form onSubmit={handleSubmit}
            style={{
              flexDirection: "column",
              display: "flex",
              gap: "10px",
              alignItems: "center",
            }}
            noValidate
          >
            <FormField
              type="text"
              placeholder="Username"
              value={username}
              onChange={(e) => setUsername(e.target.value)}
              required
              showError={usernameEmpty}
              errorMessage="Please enter your username"
            />
            <FormField
              type="password"
              placeholder="Password"
              value={password}
              onChange={(e) => setPassword(e.target.value)}
              required
              showError={passwordEmpty}
              errorMessage="Please enter your password"
            />
            <button type="submit" className="login-button">
              Login
            </button>
          </form>
          <h2 style={{textAlign: "center"}}>
            <a href="http://localhost:5173/register" className="login-link">Create account</a>
          </h2>
        </div>
        <div className="login-links-block">
          <h2>
            <a href="https://git.frik.su/Beesquit/picrinth-admin" className="login-link">Frontend</a>
          </h2>
          <h2>
            <a href="https://git.frik.su/Beesquit/picrinth-server" className="login-link">Backend</a>
          </h2>
          <h2>
            <a href="https://git.frik.su/Beesquit" className="login-link">Author</a>
          </h2>
        </div>
      </div>
    </div>
  );
};

export default Login;
