import React, { Component, Fragment } from 'react';
import ReactMapboxGl, { Marker as MapboxMarker, ZoomControl } from 'react-mapbox-gl';
import injectSheet from 'react-jss';
import PropTypes from 'prop-types';
import queryString from 'query-string';
import classnames from 'classnames';
import { Link } from 'react-router-dom';

import * as appointmentUtils from '../../utils/appointment';

import DirectionsButton from './DirectionsButton';
import styles from './styles/BuildingMap';

export const MAPBOX_STYLE = 'mapbox://styles/nestio/cj5bm415v1lmb2rmccuf77etr';

function Marker({ center, classes }) {
    if (!center) {
        return null;
    }

    return (
        <MapboxMarker coordinates={center}>
            <div
                className={classes.marker}
            />
        </MapboxMarker>
    );
}

Marker.propTypes = {
    classes: PropTypes.object.isRequired,
    center: PropTypes.object.isRequired
};

const isFullScreen = (location) =>  !!queryString.parse(location.search).full_screen_map;

export class BuildingMap extends Component {
    constructor(props){
        super(props);
        this.state = {
            isFullScreen: isFullScreen(props.location),
            map: null
        };
        this.Mapbox = ReactMapboxGl({
            accessToken: props.mapboxToken,
            scrollZoom: !props.fullScreenOnClick
        });
        this.onStyleLoad = this.onStyleLoad.bind(this);
    }

    componentWillReceiveProps(nextProps){
        const oldIsFullScreen = isFullScreen(this.props.location);
        const newIsFullScreen = isFullScreen(nextProps.location);

        if (oldIsFullScreen === newIsFullScreen) {
            return;
        }

        this.setState({
            isFullScreen: newIsFullScreen
        }, () => {
            if (this.state.map) {
                this.state.map.resize();
            } else {
                this.setState({ needsResize: true });
            }
        });
    }

    onStyleLoad(map){
        this.setState({ map });

        if (this.state.needsResize) {
            this.setState({ needsResize: false }, () => {
                this.state.map.resize();
            });
        }
    }

    render(){
        const {
            location: { pathname },
            center,
            classes,
            className,
            fullScreenOnClick,
            publicAddress,
            zoom,
            gmapsUrl,
            appointment
        } = this.props;
        const Mapbox = this.Mapbox;

        if (!this.state.isFullScreen) {
            return (
                <div className={classnames(classes.mapAndDirectionsContainer, className)}>
                    <div className={classes.inlineMapContainer}>
                        <Mapbox
                            style={MAPBOX_STYLE}
                            center={center}
                            className={classes.inlineMap}
                            zoom={[zoom]}
                            onStyleLoad={this.onStyleLoad}
                        >
                            <Marker center={center} classes={classes} />
                            {!fullScreenOnClick && (<ZoomControl position="top-right"/>)}
                        </Mapbox>
                        {fullScreenOnClick && (
                            <Link to={`${pathname}?full_screen_map=true`}>
                                <div className={classes.inlineMapCover} onClick={this.toggleFullScreenMap} />
                            </Link>
                        )}
                    </div>
                    {!appointmentUtils.getIsVideoTour(appointment) && gmapsUrl && (<DirectionsButton gmapsUrl={gmapsUrl} />)}
                </div>
            );
        } else {
            return (
                <Fragment>
                    <div className={classes.fullScreenMapContainer}>
                        <Mapbox
                            style={MAPBOX_STYLE}
                            center={center}
                            className={classes.fullScreenMap}
                            zoom={[zoom]}
                            onStyleLoad={this.onStyleLoad}
                        >
                            <Marker center={center} classes={classes} />
                            <ZoomControl position="bottom-right"/>
                        </Mapbox>
                        <div className={classes.fullScreenBanner}>
                            <div className={classes.fullScreenBannerContent}>
                                { publicAddress }
                                <Link to={pathname}><span className={classes.fullScreenBannerToggle} onClick={this.toggleFullScreenMap}>&times;</span></Link>
                            </div>
                        </div>
                    </div>
                    <div className={classes.inlineMapContainer} />
                </Fragment>

            );
        }
    }
}

BuildingMap.defaultProps = {
    zoom: 13.5,
    fullScreenOnClick: true
};

BuildingMap.propTypes = {
    className: PropTypes.string,
    center: PropTypes.object.isRequired,
    classes: PropTypes.object.isRequired,
    fullScreenOnClick: PropTypes.bool.isRequired,
    location: PropTypes.object.isRequired,
    mapboxToken: PropTypes.string.isRequired,
    publicAddress: PropTypes.string.isRequired,
    zoom: PropTypes.number.isRequired,
    appointment: PropTypes.object,
    gmapsUrl: PropTypes.string
};

export default injectSheet(styles)(BuildingMap);
