<%#
 Copyright 2013-2024 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import './home.scss';

<%_ if (authenticationTypeOauth2) { _%>
import React, { useEffect } from 'react';
<%_ } else { _%>
import React from 'react';
<%_ } _%>
<%_ if (authenticationTypeOauth2) { _%>
import { useLocation, useNavigate } from 'react-router-dom';
<%_ } else { _%>
import { Link } from 'react-router-dom';
<%_ } _%>
import { Translate } from 'react-jhipster';
import { Row, Col, Alert } from 'reactstrap';

<%_ if (authenticationTypeOauth2) { _%>
import { getLoginUrl, REDIRECT_URL } from 'app/shared/util/url-utils';
<%_ } _%>
import { useAppSelector } from 'app/config/store';

export const Home = () => {
  const account = useAppSelector(state => state.authentication.account);
<%_ if (authenticationTypeOauth2) { _%>
  const pageLocation = useLocation();
  const navigate = useNavigate();

  useEffect(() => {
    const redirectURL = localStorage.getItem(REDIRECT_URL);
    if (redirectURL) {
      localStorage.removeItem(REDIRECT_URL);
      location.href = `${location.origin}${redirectURL}`;
    }
  });
<%_ } _%>

  return (
    <Row>
      <Col md="3" className="pad">
        <span className="hipster rounded" />
      </Col>
      <Col md="9">
        <h1 className="display-4"><Translate contentKey="home.title">Welcome, <%- hipsterName %>!</Translate></h1>
        <p className="lead"><Translate contentKey="home.subtitle">This is your homepage</Translate></p>
        {
          (account?.login) ? (
            <div>
              <Alert color="success">
                <Translate contentKey="home.logged.message" interpolate={{ username: account.login }}>You are logged in as user {account.login}.</Translate>
              </Alert>
            </div>
          ) : (
            <div>
              <Alert color="warning">
                <Translate contentKey="global.messages.info.authenticated.prefix">If you want to </Translate>
                <% if (!enableTranslation) { %><span>&nbsp;</span><% } %>
<%_ if (authenticationTypeOauth2) { _%>
                <a
                  className="alert-link"
                  onClick={() =>
                    navigate(getLoginUrl(), {
                      state: { from: pageLocation },
                    })
                  }
                >
                  <Translate contentKey="global.messages.info.authenticated.link">sign in</Translate>
                </a>
<%_ } else { _%>
                <Link to="/login" className="alert-link"><Translate contentKey="global.messages.info.authenticated.link"> sign in</Translate></Link>
<%_ } _%>
                <Translate contentKey="global.messages.info.authenticated.suffix">, you can try the default accounts:
                  <br />- Administrator (login=&quot;admin&quot; and password=&quot;admin&quot;)
                  <br />- User (login=&quot;user&quot; and password=&quot;user&quot;).
                </Translate>
              </Alert>

<%_ if (generateUserManagement) { _%>
              <Alert color="warning">
                <Translate contentKey="global.messages.info.register.noaccount">You do not have an account yet?</Translate>&nbsp;
                <Link to="/account/register" className="alert-link"><Translate contentKey="global.messages.info.register.link">Register a new account</Translate></Link>
              </Alert>
<%_ } _%>
            </div>
          )
        }
        <p>
          <Translate contentKey="home.question">If you have any question on begcode:</Translate>
        </p>

        <ul>
          <li>
            <a href="<%- hipsterDocumentationLink %>" target="_blank" rel="noopener noreferrer">
              <Translate contentKey="home.link.homepage">JHipster homepage</Translate>
            </a>
          </li>
          <li>
            <a href="<%- hipsterStackoverflowLink %>" target="_blank" rel="noopener noreferrer">
              <Translate contentKey="home.link.stackoverflow">JHipster on Stack Overflow</Translate>
            </a>
          </li>
          <li>
            <a href="<%- hipsterBugTrackerLink %>" target="_blank" rel="noopener noreferrer">
              <Translate contentKey="home.link.bugtracker">JHipster bug tracker</Translate>
            </a>
          </li>
          <li>
            <a href="<%- hipsterChatLink %>" target="_blank" rel="noopener noreferrer">
              <Translate contentKey="home.link.chat">JHipster public chat room</Translate>
            </a>
          </li>
          <li>
            <a href="<%- hipsterTwitterLink %>" target="_blank" rel="noopener noreferrer">
              <Translate contentKey="home.link.follow">follow @jhipster on Twitter</Translate>
            </a>
          </li>
        </ul>

        <p>
          <Translate contentKey="home.like">If you like JHipster, do not forget to give us a star on</Translate>
          {' '}
          <a href="<%- hipsterProjectLink %>" target="_blank" rel="noopener noreferrer">GitHub</a>!
        </p>
      </Col>
    </Row>
  );
};

export default Home;

