import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
// import { toast } from "react-toastify";
import googleLogo from "../../../assets/images/google-logo.svg";
// import titleShape from "../../../assets/images/shape/title-shape.svg";
import titleShape from "../../../assets/images/shape/login_straight_underline.svg";
import usersService from "../../../services/UsersService";
import InputCom from "../../Helpers/Inputs/InputCom";
import AuthLayout from "../AuthLayout";

import { useDispatch, useSelector } from "react-redux";
import { updateUserInfo } from "../../../store/userInfo";

export default function Login() {
  const dispatch = useDispatch()
  const [checked, setValue] = useState(false);
  const [loginLoading, setLoginLoading] = useState(false);

  //login error state
  const [loginError, setLoginError] = useState(false);

  const rememberMe = () => {
    setValue(!checked);
  };

  // email
  const [email, setMail] = useState("");
  const handleEmail = (e) => {
    setMail(e.target.value);
  };
  // password
  const [password, setPassword] = useState("");
  const handlePassword = (e) => {
    setPassword(e.target.value);
  };
  const navigate = useNavigate();
  const userApi = new usersService();
  const doLogin = async () => {
    if (email !== "" && password !== "") {
      var postData = {
        username: email,
        password: password
      };
      const loginResult = await userApi.logInUser(postData);  // just for a test
      // if (email === "support@mermsemr.com") {
      if (loginResult.data.status > 0 && loginResult.data.session_token !='') { // just for a start
        localStorage.setItem("email", `${email}`);
        localStorage.setItem("member_id", loginResult.data.member_id);
        localStorage.setItem("member_uuid", loginResult.data.member_uuid);
        localStorage.setItem("session_token", `${loginResult.data.session_token}`);
        localStorage.setItem("status", loginResult.data.status);
        localStorage.setItem("profile", `${JSON.stringify(loginResult.data.profile)}`);
        setLoginLoading(true);
         // userApi.getUserReminders(); //testing
        setTimeout(() => {         
          navigate("/", { replace: true });
          setLoginLoading(false);
          dispatch(updateUserInfo(loginResult.data.profile))
        }, 2000);
      } else {
        setLoginError(true)
      }
    }
  };

  return (
    <>
      <AuthLayout
        slogan="Welcome to myFit"
      >
        <div className="content-wrapper w-full  flex justify-center items-center xl:bg-white dark:bg-dark-white   2xl:w-[828px] xl:w-[500px] 2xl:h-[818px] xl:h-[600px] rounded-xl 2xl:px-[164px] xl:px-[56px] sm:px-7 px-5 ">
          <div className="w-full">
            <div className="title-area flex flex-col justify-center items-center relative text-center mb-7">
              <h1 className="text-5xl font-bold leading-[74px]   text-dark-gray dark:text-white">
                Log In
              </h1>
              <div className="shape mb-[10px]">
                <img src={titleShape} alt="shape" />
              </div>
            </div>
            <div className="input-area">
              <div className="input-item mb-5">
                <InputCom
                  value={email}
                  inputHandler={handleEmail}
                  placeholder="support@mermsemr.com"
                  label="Email Address"
                  name="email"
                  type="email"
                  iconName="message"
                />
              </div>
              <div className="input-item mb-5">
                <InputCom
                  value={password}
                  inputHandler={handlePassword}
                  placeholder="● ● ● ● ● ●"
                  label="Password"
                  name="password"
                  type="password"
                  iconName="password"
                />
              </div>
              <div className="forgot-password-area flex justify-between items-center mb-7">
                <div className="remember-checkbox flex items-center space-x-2.5">
                  <button
                    onClick={rememberMe}
                    type="button"
                    className="w-5 h-5 text-dark-gray dark:text-white flex justify-center items-center border border-light-gray"
                  >
                    {checked && (
                      <svg
                        xmlns="http://www.w3.org/2000/svg"
                        className="h-5 w-5"
                        viewBox="0 0 20 20"
                        fill="currentColor"
                      >
                        <path
                          fillRule="evenodd"
                          d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
                          clipRule="evenodd"
                        />
                      </svg>
                    )}
                  </button>
                  <span
                    onClick={rememberMe}
                    className="text-base text-dark-gray dark:text-white"
                  >
                    Remember Me
                  </span>
                </div>
                <a href="/forgot-password" className="text-base text-purple">
                  Forgot Password
                </a>
              </div>
              <div className="signin-area mb-3.5">
                <div className="flex justify-center">
                  <button
                    onClick={doLogin}
                    type="button"
                    className={`btn-login  rounded-[50px] mb-6 text-xl text-white font-bold flex justify-center bg-purple items-center ${loginLoading ? "active" : ""
                      }`}
                  >
                    {loginLoading ? (
                      <div className="signup btn-loader"></div>
                    ) : (
                      <span>Sign In</span>
                    )}
                  </button>
                </div>
                {loginError && <p class="text-center text-red-700 pb-4">Invalid username or password</p>}
                <a
                  href="#"
                  className="w-full border border-light-purple dark:border-[#5356fb29]  rounded-[50px] h-[58px] flex justify-center bg-[#FAFAFA] dark:bg-[#11131F]  items-center"
                >
                  <img className="mr-3" src={googleLogo} alt="google logo" />
                  <span className="text-lg text-thin-light-gray font-normal">
                    Sign In with Google
                  </span>
                </a>
              </div>
              <div className="signup-area flex justify-center">
                <p className="sm:text-lg text-sm text-thin-light-gray font-normal">
                  Don't have an account ?
                  <a href="/signup" className="ml-2   text-dark-gray dark:text-white">
                    Sign up free
                  </a>
                </p>
              </div>
            </div>
          </div>
        </div>
      </AuthLayout>
    </>
  );
}