<%#
 Copyright 2019-2021 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

      http://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.<%= styleSheetExt %>';

import React, { useEffect } from 'react';
import { Link } from 'react-router-dom';
import { Translate } from 'react-jhipster';
import { connect } from 'react-redux';
import { Row, Col, Alert } from 'reactstrap';

<%_ if (authenticationType === 'oauth2') { _%>
import { getLoginUrl, REDIRECT_URL } from 'app/shared/util/url-utils';
<%_ } _%>
import { Storage } from 'react-jhipster';
import { AUTH_TOKEN_KEY } from '../../config/constants'

export type IHomeProp = StateProps;

export const Home = (props: IHomeProp) => {
<%_ if (authenticationType === 'oauth2') { _%>
  useEffect(() => {
    const redirectURL = localStorage.getItem(REDIRECT_URL);
    if (redirectURL) {
      localStorage.removeItem(REDIRECT_URL);
      location.href = `${location.origin}${redirectURL}`;
    }
  });
<%_ } _%>
  extractJWTfromURL()
  const { account } = props;

  return (
    <Row>
        <Col md="3" className="pad">
          <span className="hipster rounded" />
        </Col>
        <Col md="9">
          <h2><Translate contentKey="home.title">Welcome, <%= backendName %> Hipster!</Translate></h2>
        <p className="lead"><Translate contentKey="home.subtitle">This is your homepage</Translate></p>
        {
          (account && 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 (authenticationType === 'oauth2') { _%>
                <a href={getLoginUrl()} className="alert-link">
                  <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 (!skipUserManagement) { _%>
              <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 JHipster or NHipster:</Translate>
        </p>

        <ul>
          <li>
            <a href="https://www.jhipster.tech/" target="_blank" rel="noopener noreferrer">
              <Translate contentKey="home.link.homepage">JHipster homepage</Translate>
            </a>
          </li>
          <li>
            <a href="http://stackoverflow.com/tags/jhipster/info" target="_blank" rel="noopener noreferrer">
              <Translate contentKey="home.link.stackoverflow">JHipster on Stack Overflow</Translate>
            </a>
          </li>
          <li>
            <a href="https://github.com/jhipster/generator-jhipster-nodejs/issues?state=open" target="_blank" rel="noopener noreferrer">
              <Translate contentKey="home.link.bugtracker">NHipster bug tracker</Translate>
            </a>
          </li>
          <li>
            <a href="https://gitter.im/jhipster/generator-jhipster-nodejs" target="_blank" rel="noopener noreferrer">
              <Translate contentKey="home.link.chat">NHipster public chat room</Translate>
            </a>
          </li>
          <li>
            <a href="https://twitter.com/java_hipster" target="_blank" rel="noopener noreferrer">
              <Translate contentKey="home.link.follow">follow @java_hipster on Twitter</Translate>
            </a>
          </li>
        </ul>

        <p>
          <Translate contentKey="home.like">If you like NHipster, do not forget to give us a star on</Translate>
          {' '}
          <a href="https://github.com/jhipster/generator-jhipster-nodejs" target="_blank" rel="noopener noreferrer">Github</a>!
        </p>
      </Col>
      <Col md="3" className="pad">
        <span className="hipster rounded" />
      </Col>
    </Row>
  );
};

const extractJWTfromURL = () => {
  const queryParams = new URLSearchParams(window.location.search);
  const jwt = queryParams.get('token');
  if (jwt) {
    Storage.local.set(AUTH_TOKEN_KEY, jwt);
  }
}

const mapStateToProps = storeState => ({
  account: storeState.authentication.account,
  isAuthenticated: storeState.authentication.isAuthenticated
});

type StateProps = ReturnType<typeof mapStateToProps>;

export default connect(mapStateToProps)(Home);
