<%#
 Copyright 2013-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

      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.
-%>
<%_ const storeName = entityInstance + 'Store'; _%>
import React, { useEffect } from 'react';
import { connect } from 'app/shared/util/typed-inject';
import { Link, RouteComponentProps } from 'react-router-dom';
import { Button, <% if (haveFieldWithJavadoc) { %>UncontrolledTooltip, <% } %>Row, Col } from 'reactstrap';
import {
  Translate,
<%_ if (fieldsContainBlob) { _%>
  <%_ if (fieldsContainBlobOrImage) { _%>
  openFile,
  <%_ } _%>
  byteSize,
<%_ } _%>
<%_ if (fieldsContainDate) { _%>
  TextFormat
<%_ } _%>
} from 'react-jhipster';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

import { IRootStore } from 'app/shared/stores';
import { APP_DATE_FORMAT, APP_LOCAL_DATE_FORMAT } from 'app/config/constants';
<%_ if (fieldsContainDuration) { _%>
import { DurationFormat } from 'app/shared/DurationFormat';
<%_ } _%>
export interface I<%= entityReactName %>DetailProps extends StoreProps, RouteComponentProps<{id: string}>  {}

export const <%= entityReactName %>Detail = (props: I<%= entityReactName %>DetailProps) => {
  useEffect(() => {
    props.getEntity(props.match.params.id);
  }, []);

  const <%= entityInstance %>Entity = props.<%= entityInstance %>Entity
  return (
    <Row>
      <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.javadoc) { _%>
              <UncontrolledTooltip target="<%= fieldName %>">
    <%_ if (enableTranslation) { _%>
                <Translate contentKey="<%= i18nKeyPrefix %>.help.<%= fieldName %>"/>
    <%_ } else { _%>
                <%= field.javadoc %>
    <%_ } _%>
              </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 ownerSide = relationship.ownerSide;
    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 (ownerSide) { _%>
            <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="/<%= entityFileName %>" 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={`/<%= entityFileName %>/${<%= 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>
      </Col>
    </Row>
  );
};

const mapStoreToProps = ({ <%= storeName %> }: IRootStore) => ({
    <%= entityInstance %>Entity: <%= storeName %>.entity,
    getEntity: <%= storeName %>.getEntity });

type StoreProps = ReturnType<typeof mapStoreToProps>;

export default connect(mapStoreToProps)(<%= entityReactName %>Detail);
