import React from 'react'
import Gallery from './Gallery'
import {formatPrice, formatRooms, formatFloor} from '../lib/helpers'
import ContactModal from './ContactModal';

export default class ListingDetails extends React.Component {
  onReadMore (e) {
    e.preventDefault()
    this.props.dispatch({type: 'EXPAND_DESCRIPTION'})
  }

  onBackClick () {
    this.props.dispatch({
      type: 'UPDATE_ROUTE',
      action: {
        toDispatch: {
          type: 'BACK_TO_RESULTS'
        },
        params: {
          listingId: null
        }
      }
    })
    this.props.dispatch({type: 'BACK_TO_RESULTS'})
  }

  onContactClick () {
    this.props.dispatch({
      type: 'OPEN_CONTACT'
    });
  }

  render () {
    const {listing, descriptionExpanded, contactFormOpen} = this.props
    if (!listing) {
      return null
    }
    const descriptionClasses = descriptionExpanded
      ? 'ld-description expanded'
      : 'ld-description'
    const images = listing.images.map(image => ({
      original: image,
      thumbnail: image
    }))

    return (
      <div className="ld-container">
        <div className="ld-header">
          <div className="back-to-results">
            <button
              className="back-to-results-btn"
              onClick={this.onBackClick.bind(this)}
            >
              <i className="fa fa-arrow-left" aria-hidden="true" />
              <span>
                Nazad na rezultate
              </span>
            </button>
          </div>
        </div>

        <div className="ld-details">
          <Gallery
            dispatch={this.props.dispatch}
            images={listing.images}
            imageIndex={this.props.imageIndex}
          />
          <div className="ld-price-address-box">
            <div className="ld-price">
              {formatPrice(listing.price)}
            </div>

            <div className="ld-address">
              <div className="">{listing.address}</div>
              <div className="">{listing.location}</div>
            </div>
          </div>

          <div className="ld-features">
            <div className="ld-feature-box">
              <i className="fa fa-bed" />
              {formatRooms(listing.rooms)}
            </div>
            <div className="ld-feature-box">
              <i className="fa fa-home" />
              {listing.size}m2
            </div>
            <div className="ld-feature-box">
              <i className="fa fa-home" />
              {formatFloor(listing.floor)}
            </div>
            <div className="ld-feature-box">
              <i className="fa fa-home" />
              --
            </div>
          </div>
          <div className="ld-check-availability">
            <button onClick={this.onContactClick.bind(this)}>Kontaktiraj</button>
          </div>
          <div className={descriptionClasses}>
            {listing.longDescription}
          </div>
          {!descriptionExpanded
            ? <div className="ld-read-more">
                <a href="" onClick={this.onReadMore.bind(this)}>
                  Pročitajte više
                </a>
              </div>
            : null}
          <div className="ld-footer" />
        </div>

      </div>
    )
  }
}
