// Modules
import React, { Component, PropTypes } from 'react';
import { FormattedDate } from 'react-formatted';
import UI from 'jqestate.ru/components/ui';
import * as constants from 'core/config/constants';

import 'jqestate.ru/styles/property/description';

// Module
class Description extends Component {
  static propTypes = {
    property: PropTypes.object.isRequired,
  }

  renderDescription(propertyCategory) {
    const { landProperty, communication } = this.props.property;

    if (propertyCategory !== `flat`) {
      return (
        <UI.Row>
          <div className="col-md-6">
            <UI.Header className="description-title">Описание участка</UI.Header>

            <div className="description-list">
              <UI.DL name="landSize" type="dimension" value={landProperty.area} className="dl-horizont description-list-dl" />
              <UI.DL name="landType" type="dictionary" value={landProperty.landscapeKind[0]} className="dl-horizont description-list-dl" />
              <UI.DL name="landscaping" type="boolean" value={landProperty.landscapeKind[0] ? true : false} className="dl-horizont description-list-dl" />
            </div>
          </div>
          <div className="col-md-6">
            <UI.Header className="description-title">Коммуникации</UI.Header>

            <div className="description-list m-b-0">
              <UI.DL name="gasSupply" type="dictionary" value={communication.gasSupply} className="dl-horizont description-list-dl" />
              <UI.DL name="powerSupply" type="dimension" value={communication.powerSupply} className="dl-horizont description-list-dl" />
              <UI.DL name="waterSupply" type="dictionary" value={communication.waterSupply} className="dl-horizont description-list-dl" />
              <UI.DL name="sewers" type="dictionary" value={communication.sewerageSupply} className="dl-horizont description-list-dl" />
            </div>
          </div>
        </UI.Row>
      );
    }

    return null;
  }

  renderLayout(layout) {
    if (layout.length) {
      const { number, kind, items } = this.props.property.layouts[layout];

      return (
        <div key={layout} className="visible-xs-block visible-sm-inline-block visible-md-inline-block visible-lg-inline-block description-planning-dl">
          <dt>{constants.dictionary.floors[kind === `floor` ? number : kind] + `:`}</dt>

          {items.map(item =>
            <dd key={item}>{item}</dd>
          )}
        </div>
      );
    }

    return null;
  }

  renderLayouts() {
    const { layouts, propertyCategory } = this.props.property;

    if (layouts.length > 0) {
      return (
        <div>
          <div className="panel-body">
            <UI.Header className="hidden-xs description-title">Описание {constants.dictionary.propertyTypeDescription[propertyCategory]}</UI.Header>

            <UI.Row>
              {Object.keys(layouts).map(::this.renderLayout)}
            </UI.Row>
          </div>
          <hr className="description-hr"/>
        </div>
      );
    }

    return null;
  }

  renderFooter() {
    const { createdAt, updatedAt } = this.props.property;

    return (
      <div className="description-footer">
        <UI.Row>
          <div className="col-xs-6">
            <p className="description-footer-text">
              Опубликовано&nbsp;
              <FormattedDate value={createdAt} mask="dd.mm.yy" />
            </p>
          </div>
          <div className="col-xs-6 text-right">
            <p className="description-footer-text">
              Обновлено&nbsp;
              <FormattedDate value={updatedAt} mask="dd.mm.yy" />
            </p>
          </div>
        </UI.Row>
      </div>
    );
  }

  render() {
    const { property } = this.props;

    if (property) {
      return (
        <div>
          <UI.Panel bsSize="sm">
            <div className="description">
              {this.renderLayouts()}

              <div className="panel-body">
                {this.renderDescription(property.propertyCategory)}
              </div>
            </div>
          </UI.Panel>

          {this.renderFooter()}
        </div>
      );
    }

    return null;
  }
}

export default Description;
