import React from "react";
import styled from "styled-components";
import logo from "../../assets/images/logo.png";
import bg from "./images/bg-404.png";
import useCustomNavigate from "../../hooks/useCustomNavigate";
import getCookie from "../../utils/getCookie";
import useTranslation from "../../hooks/useTranslation.jsx";
import FullPageLoader from "../fullPageLoader";
import wordStore from "../../mc/wordStore.json";
const ErrorWrapper = ({
  title = "",
  subTitle = "",
  message = "",
  btnText = "",
  bgSrc,
  fallbackPath,
  isDashboard,
}) => {
  const navigate = useCustomNavigate();
  const handleNav = () => {
    const accountType = getCookie("defaultAccountType")?.toLocaleLowerCase();
    if (isDashboard && accountType) {
      const route =
        accountType === "developer"
          ? "/developer"
          : `/${accountType}/dashboard`;
      navigate(fallbackPath || route || "/", {
        reload: true,
      });
    } else {
      navigate(fallbackPath || "/", {
        reload: true,
      });
    }
  };
  const wordBank = [
    "Opss...",
    "Page not found",
    "Seems we can't find the page you are looking for. Let's get you back home.",
    "Go Back",
    "There was an error",
    "Learngual logo",
  ];
  const { findText, translations, isTranslationsLoading } =
    useTranslation(wordStore);
  return isTranslationsLoading && Object.keys(translations)?.length === 0 ? (
    <FullPageLoader hasBackground fixed={true} />
  ) : (
    <Container>
      <Navbar>
        <a href="/">
          <img src={logo} alt={findText("Learngual logo")} />
        </a>
      </Navbar>
      <Content>
        <LeftDiv>
          <h1>{title || findText("Oops!")}</h1>
          <div>
            {subTitle && <h2>{subTitle || findText("Page not found")}</h2>}
            {message && (
              <p>
                {message ||
                  findText(
                    "Seems we can't find the page you are looking for. Let's get you back home.",
                  )}
              </p>
            )}
          </div>

          <button onClick={handleNav}>
            {btnText || findText("Go to home page")}
          </button>
        </LeftDiv>
        <ImageBox>
          <img src={bgSrc || bg} alt="" />
        </ImageBox>
      </Content>
    </Container>
  );
};
const Container = styled.div`
  min-height: 100vh;
  width: 100%;
  background-color: #fff;
  padding: 0 25px;
  display: flex;
  flex-direction: column;
  @media screen and (min-width: 850px) {
    padding: 0 104px;
  }
`;
const Navbar = styled.nav`
  width: 100%;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  img {
    width: 52px;
  }
  @media screen and (min-width: 850px) {
    height: 100px;
    img {
      width: 60px;
    }
  }
`;
const Content = styled.div`
  flex: 1;
  display: flex;
  align-items: center;
  padding-bottom: 80px;
  gap: 30px;
  flex-direction: column-reverse;
  justify-content: flex-end;
  padding-top: 60px;
  max-width: 390px;
  margin: 0 auto;
  @media screen and (min-width: 850px) {
    flex-direction: row;
    padding-top: 0px;
    justify-content: flex-start;
    gap: 100px;
    padding-bottom: 100px;
    max-width: none;
    margin: 0px;
  }
`;
const LeftDiv = styled.div`
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
  align-items: center;
  color: #313333;
  border-radius: 15px;
  background: #f5f7f7;
  padding: 20px 18px;
  h1 {
    font-size: 25px;
  }
  > div {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 8px;
    h2 {
      font-weight: 500;
      font-size: 18px;
    }
    p {
      font-size: 16px;
      color: #636666;
    }
  }
  button {
    width: fit-content;
    height: 40px;
    padding: 0px 40px;
    border-radius: 6px;
    background: #febf10;
    box-shadow: 0px 10px 20px 0px rgba(254, 191, 16, 0.25);
    border: none;
    outline: none;
    cursor: pointer;
    color: white;
    font-weight: 700;
    font-size: 14px;
    margin-top: 12px;
  }

  @media screen and (min-width: 850px) {
    max-width: 462px;
    gap: 24px;
    text-align: left;
    flex: none;
    align-items: flex-start;
    border-radius: 0px;
    background: transparent;
    padding: 0px;
    flex: 1;
    h1 {
      font-size: 44px;
    }
    > div {
      gap: 12px;
      h2 {
        font-size: 30px;
      }
      p {
        font-size: 18px;
        line-height: 32px;
        color: #313333;
      }
    }
    button {
      margin-top: 0;
      height: 50px;
      font-size: 16px;
      border-radius: 12px;
    }
  }
`;
const ImageBox = styled.div`
  width: 100%;
  height: 160px;
  display: grid;
  place-items: center;
  img {
    width: 100%;
    max-width: 550px;
    height: 100%;
    max-height: 550px;
    object-fit: contain;
  }

  @media screen and (min-width: 850px) {
    height: 100%;
    flex: 1;
  }
`;
export default ErrorWrapper;
