import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Slider from 'react-slick';
import classnames from 'classnames';
import 'slick-carousel/slick/slick.css';
import 'slick-carousel/slick/slick-theme.css';
import injectSheet from 'react-jss';
import Lightbox from 'react-images';
import Photo from './Photo';

import styles from './styles/PhotoCarousel';

export class PhotoCarousel extends Component {
    constructor() {
        super();
        this.state = {
            currentLightboxIdx: null
        };

        this.openLightbox = this.openLightbox.bind(this);
        this.onClickNext = this.onClickNext.bind(this);
        this.onClickPrev = this.onClickPrev.bind(this);
        this.onClickThumbnail = this.onClickThumbnail.bind(this);
        this.closeLightbox = this.closeLightbox.bind(this);

        this.slider = React.createRef();
    }

    openLightbox(idx) {
        const { lightboxEnabled } = this.props;
        if (lightboxEnabled) {
            this.setState({
                currentLightboxIdx: idx
            });
        }
    }

    closeLightbox() {
        this.setState({
            currentLightboxIdx: null
        });
    }

    onClickNext() {
        this.handleImageNav('next');
    }

    onClickPrev() {
        this.handleImageNav('prev');
    }

    onClickThumbnail(nextImageIdx) {
        this.setState({ currentLightboxIdx: nextImageIdx });
        this.slider.current.slickGoTo(nextImageIdx, true);
    }

    handleImageNav(direction) {
        const { photos } = this.props;
        let nextImage = this.state.currentLightboxIdx;

        if (direction === 'prev') {
            nextImage = nextImage === 0 ? photos.length - 1 : nextImage - 1;
        }
        else if (direction === 'next') {
            nextImage = nextImage === photos.length - 1 ? 0 : nextImage + 1;
        }

        this.setState({ currentLightboxIdx: nextImage });
        this.slider.current.slickGoTo(nextImage, true);
    }

    _getLightboxImages() {
        const { photos } = this.props;
        return photos.map(image => ({
            src: image.xlarge,
            thumbnail: image.thumbnail,
        }));
    }

    render() {

        const { photos, classes, publicAddress, unitNumber, lazyLoad, photoSize, isReschedulingFlow, isNearbyCommunityFlow, labelComponent, buildingName } = this.props;

        const { currentLightboxIdx } = this.state;
        const hasPhotos = !!photos && !!photos.length;
        const showLightbox = currentLightboxIdx !== null;
        const settings = {
            accessibility: false, // disables keyboard controls
            dots: false,
            infinite: true,
            speed: 250,
            arrows: false,
            lazyLoad: lazyLoad
        };

        return (
            <div className={classnames(classes.carousel, !hasPhotos && classes.noPhotos)}>
                {
                    hasPhotos &&
                    <Slider {...settings} ref={this.slider}>
                        {
                            photos.map((photo, idx) => (
                                <Photo
                                    classes={classes}
                                    key={idx}
                                    idx={idx}
                                    onClick={this.openLightbox}
                                    url={photo[photoSize]}
                                />
                            ))
                        }
                    </Slider>
                }
                {isNearbyCommunityFlow
                    ? labelComponent()
                    : <div className={classes.labelContainer}>
                        <div className={classes.labelHeader}>
                            {isReschedulingFlow ? 'Reschedule Tour' : 'Schedule a Tour'}
                        </div>
                        <div>
                            {buildingName && (
                                <div className={classes.labelSubheader}>{buildingName}</div>
                            )}
                            <div className={classes.labelSubheader}>
                                {unitNumber ? `${publicAddress} #${unitNumber}` : publicAddress}
                            </div>
                        </div>
                    </div>
                }
                {
                    hasPhotos &&
                    <Lightbox
                        images={this._getLightboxImages()}
                        isOpen={showLightbox}
                        currentImage={currentLightboxIdx}
                        onClickPrev={this.onClickPrev}
                        onClickNext={this.onClickNext}
                        onClickThumbnail={this.onClickThumbnail}
                        onClose={this.closeLightbox}
                        showThumbnails
                        backdropClosesModal
                        enableKeyboardInput
                    />
                }
            </div>
        );
    }
}

PhotoCarousel.propTypes = {
    classes: PropTypes.object.isRequired,
    photos: PropTypes.array.isRequired,
    publicAddress: PropTypes.string.isRequired,
    unitNumber: PropTypes.string.isRequired,
    lightboxEnabled: PropTypes.bool,
    lazyLoad: PropTypes.string,
    photoSize: PropTypes.string,
    isReschedulingFlow: PropTypes.bool,
    isNearbyCommunityFlow: PropTypes.bool,
    buildingName: PropTypes.string,
    labelComponent: PropTypes.func,
};

PhotoCarousel.defaultProps = {
    lazyLoad: 'progressive',
    isNearbyCommunityFlow: false,
    photoSize: 'xlarge'
};

export default injectSheet(styles)(PhotoCarousel);
