<%#
 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.
-%>
import React, { useEffect } from 'react';
import { connect } from 'react-redux';
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 { IRootState } from 'app/shared/reducers';
import { getEntity } from './<%= entityFileName %>.reducer';
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 StateProps, DispatchProps, RouteComponentProps<{id: string}>  {}

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

  const { <%= entityInstance %>Entity } = props;
  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 fieldType = field.fieldType;
              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 (fieldType === 'Boolean') { _%>
              {<%= entityInstance %>Entity.<%= fieldName %> ? 'true' : 'false'}
            <%_ } else if (fieldType === 'Instant' || fieldType === 'ZonedDateTime') { _%>
              {<%= entityInstance %>Entity.<%= fieldName %> ? <TextFormat value={<%= entityInstance %>Entity.<%= fieldName %>} type="date" format={APP_DATE_FORMAT} />: null}
            <%_ } else if (fieldType === 'Duration') { _%>
              {<%= entityInstance %>Entity.<%= fieldName %> ? <DurationFormat value={<%= entityInstance %>Entity.<%= fieldName %>} />: null} ({<%= entityInstance %>Entity.<%= fieldName %>})
            <%_ } else if (fieldType === 'LocalDate') { _%>
              {<%= entityInstance %>Entity.<%= fieldName %> ? <TextFormat value={<%= entityInstance %>Entity.<%= fieldName %>} type="date" format={APP_LOCAL_DATE_FORMAT} />: null}
            <%_
                } else if (['byte[]', 'ByteBuffer'].includes(fieldType)) {

                  const fieldBlobType = field.fieldTypeBlobContent;
                  if (fieldBlobType !== 'text') {
            _%>
                {<%= entityInstance %>Entity.<%= fieldName %> ? (
                  <div>
                    {<%= entityInstance %>Entity.<%= fieldName %>ContentType ? (
                      <a onClick={openFile(<%= entityInstance %>Entity.<%= fieldName %>ContentType, <%= entityInstance %>Entity.<%= fieldName %>)}>
                      <%_ if (fieldBlobType === 'image') { _%>
                        <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 relationshipType = relationship.relationshipType;
                const ownerSide = relationship.ownerSide;
                const relationshipName = relationship.relationshipName;
                const relationshipFieldName = relationship.relationshipFieldName;
                const relationshipFieldNamePlural = relationship.relationshipFieldNamePlural;
                const relationshipNameHumanized = relationship.relationshipNameHumanized;
                const otherEntityName = relationship.otherEntityName;
                const otherEntityPkName = relationship.otherEntity.primaryKey && relationship.otherEntity.primaryKey.name || 'id';
                const otherEntityStateName = relationship.otherEntityStateName;
                const otherEntityField = relationship.otherEntityField;
                const otherEntityFieldCapitalized = relationship.otherEntityFieldCapitalized;
                if (relationshipType === 'many-to-one'
                || (relationshipType === 'one-to-one' && ownerSide === true)
                || (relationshipType === 'many-to-many' && ownerSide === true)) { _%>
            <dt>
              <Translate contentKey="<%= i18nKeyPrefix %>.<%= relationshipName %>">
                <%= relationshipNameHumanized %>
              </Translate>
            </dt>
            <dd>
                <%_ if (otherEntityName === 'user') { _%>
                    <%_ if (relationshipType === 'many-to-many') { _%>
    {
      (<%= 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 (relationshipType === 'many-to-many') { _%>
    {
        (<%= 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 mapStateToProps = ({ <%= entityInstance %> }: IRootState) => ({
    <%= entityInstance %>Entity: <%= entityInstance %>.entity
});

const mapDispatchToProps = { getEntity };

type StateProps = ReturnType<typeof mapStateToProps>;
type DispatchProps = typeof mapDispatchToProps;

export default connect(mapStateToProps, mapDispatchToProps)(<%= entityReactName %>Detail);
