<%#
 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 React, { useEffect } from 'react';
import { Link, useParams } from 'react-router-dom';
import { Button, <% if (anyFieldHasDocumentation) { %>UncontrolledTooltip, <% } %>Row, Col } from 'reactstrap';
import {
  Translate,
<%_ if (anyFieldIsBlobDerived) { _%>
  <%_ if (anyFieldHasFileBasedContentType) { _%>
  openFile,
  <%_ } _%>
  byteSize,
<%_ } _%>
<%_ if (anyFieldIsDateDerived) { _%>
  TextFormat
<%_ } _%>
} from 'react-jhipster';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

<%_ if (anyFieldIsDateDerived) { _%>
import { APP_DATE_FORMAT, APP_LOCAL_DATE_FORMAT } from 'app/config/constants';
<%_ } _%>
<%_ if (anyFieldIsDuration) { _%>
import { DurationFormat } from 'app/shared/DurationFormat';
<%_ } _%>
import { useAppDispatch, useAppSelector } from 'app/config/store';

import { getEntity } from './<%= entityFileName %>.reducer';

export const <%= entityReactName %>Detail = () => {
  const dispatch = useAppDispatch();

  const { id } = useParams<'id'>();

  useEffect(() => {
    dispatch(getEntity(id));
  }, []);

  const <%= entityInstance %>Entity = useAppSelector(state => state.<%= entityReactState %>.entity);
  return (
    <a-row>
      <a-col md="8">
        <h2 data-cy="<%= entityInstance %>DetailsHeading">
          <Translate contentKey="<%= i18nKeyPrefix %>.detail.title"><%= entityClass %></Translate>
        </h2>
          <dl className="jh-entity-details">
<%_ for (field of fields) {
  const fieldName = field.fieldName;
  const fieldNameHumanized = field.fieldNameHumanized;
_%>
            <dt>
              <span id="<%= fieldName %>">
                <Translate contentKey="<%= field.fieldTranslationKey %>">
                  <%= fieldNameHumanized %>
                </Translate>
              </span>
  <%_ if (field.documentation) { _%>
              <UncontrolledTooltip target="<%= fieldName %>">
    <%_ if (enableTranslation) { _%>
                <Translate contentKey="<%= i18nKeyPrefix %>.help.<%= fieldName %>"/>
    <%_ } else { _%>
                <%= field.documentation %>
    <%_ } _%>
              </UncontrolledTooltip>
  <%_ } _%>
            </dt>
            <dd>
  <%_ if (field.fieldTypeBoolean) { _%>
              {<%= entityInstance %>Entity.<%= fieldName %> ? 'true' : 'false'}
  <%_ } else if (field.fieldTypeTimed) { _%>
              {<%= entityInstance %>Entity.<%= fieldName %> ? <TextFormat value={<%= entityInstance %>Entity.<%= fieldName %>} type="date" format={APP_DATE_FORMAT} />: null}
  <%_ } else if (field.fieldTypeDuration) { _%>
              {<%= entityInstance %>Entity.<%= fieldName %> ? <DurationFormat value={<%= entityInstance %>Entity.<%= fieldName %>} />: null} ({<%= entityInstance %>Entity.<%= fieldName %>})
  <%_ } else if (field.fieldTypeLocalDate) { _%>
              {<%= entityInstance %>Entity.<%= fieldName %> ? <TextFormat value={<%= entityInstance %>Entity.<%= fieldName %>} type="date" format={APP_LOCAL_DATE_FORMAT} />: null}
  <%_ } else if (field.fieldTypeBinary) { _%>
    <%_ if (!field.blobContentTypeText) { _%>
                {<%= entityInstance %>Entity.<%= fieldName %> ? (
                  <div>
                    {<%= entityInstance %>Entity.<%= fieldName %>ContentType ? (
                      <a onClick={openFile(<%= entityInstance %>Entity.<%= fieldName %>ContentType, <%= entityInstance %>Entity.<%= fieldName %>)}>
        <%_ if (field.blobContentTypeImage) { _%>
                        <img src={`data:${<%= entityInstance %>Entity.<%= fieldName %>ContentType};base64,${<%= entityInstance %>Entity.<%= fieldName %>}`} style={{ maxHeight: '30px' }} />
        <%_ } else { _%>
                        <Translate contentKey="entity.action.open">Open</Translate>&nbsp;
        <%_ } _%>
                      </a>
                    ) : null}
                    <span>{<%= entityInstance %>Entity.<%= fieldName %>ContentType}, {byteSize(<%= entityInstance %>Entity.<%= fieldName %>)}</span>
                  </div>
                ) : null}
    <%_ } else { _%>
                {<%= entityInstance %>Entity.<%= field.fieldName %>}
    <%_ } _%>
  <%_ } else { _%>
            {<%= entityInstance %>Entity.<%= field.fieldName %>}
  <%_ } _%>
            </dd>
<%_ } _%>
<%_ for (relationship of relationships) {
    const relationshipName = relationship.relationshipName;
    const relationshipFieldName = relationship.relationshipFieldName;
    const relationshipFieldNamePlural = relationship.relationshipFieldNamePlural;
    const relationshipNameHumanized = relationship.relationshipNameHumanized;
    const otherEntityPkName = relationship.otherEntity.primaryKey && relationship.otherEntity.primaryKey.name || 'id';
    const otherEntityField = relationship.otherEntityField; _%>
  <%_ if (relationship.persistableRelationship) { _%>
            <dt>
              <Translate contentKey="<%= i18nKeyPrefix %>.<%= relationshipName %>">
                <%= relationshipNameHumanized %>
              </Translate>
            </dt>
            <dd>
    <%_ if (relationship.otherEntityUser) { _%>
      <%_ if (relationship.relationshipManyToMany) { _%>
    {
      (<%= entityInstance %>Entity.<%= relationshipFieldNamePlural %>) ?
          (<%= entityInstance %>Entity.<%= relationshipFieldNamePlural %>.map((val, i) =>
              <span key={val.<%= otherEntityPkName %>}><a>{val.<%= otherEntityField %>}</a>{(<%= entityInstance %>Entity.<%= relationshipFieldNamePlural %> && i === <%= entityInstance %>Entity.<%= relationshipFieldNamePlural %>.length - 1) ? '' : ', '}</span>
          )
      ) : null
    }
      <%_ } else { _%>
                {(<%= entityInstance + "Entity." + relationshipFieldName %>) ? <%= entityInstance + "Entity." + relationshipFieldName + "." + otherEntityField %> : ''}
      <%_ } _%>
    <%_ } else { _%>
      <%_ if (relationship.relationshipManyToMany) { _%>
    {
        (<%= entityInstance %>Entity.<%= relationshipFieldNamePlural %>) ?
            (<%= entityInstance %>Entity.<%= relationshipFieldNamePlural %>.map((val, i) =>
                <span key={val.<%= otherEntityPkName %>}><a>{val.<%= otherEntityField %>}</a>{(<%= entityInstance %>Entity.<%= relationshipFieldNamePlural %> && i === <%= entityInstance %>Entity.<%= relationshipFieldNamePlural %>.length - 1) ? '' : ', '}</span>
            )
        ) : null
    }
      <%_ } else { _%>
                {(<%= entityInstance + "Entity." + relationshipFieldName %>) ? <%= entityInstance + "Entity." + relationshipFieldName + "." + otherEntityField %> : ''}
      <%_ } _%>
    <%_ } _%>
            </dd>
  <%_ } _%>
<%_ } _%>
          </dl>
        <Button tag={Link} to="/<%= entityPage %>" replace color="info" data-cy="entityDetailsBackButton">
          <FontAwesomeIcon icon="arrow-left" /> <span className="d-none d-md-inline" ><Translate contentKey="entity.action.back">Back</Translate></span>
        </Button>&nbsp;
        <Button tag={Link} to={`/<%= entityPage %>/${<%= entityInstance %>Entity.<%= primaryKey.name %>}/edit`} replace color="primary">
          <FontAwesomeIcon icon="pencil-alt" /> <span className="d-none d-md-inline"><Translate contentKey="entity.action.edit">Edit</Translate></span>
        </Button>
      </a-col>
    </a-row>
  );
};

export default <%= entityReactName %>Detail;
