import PropTypes from 'prop-types';
import React, { Component } from 'react';
import injectSheet from 'react-jss';
import { withRouter } from 'react-router-dom';

import { PAGE_TOUR_RESCHEDULE } from '../../constants/formSections';
import ConfirmModal from './ConfirmModal';

import styles from './styles/UpdatedConfirmationPageActions';

export class UpdatedConfirmationPageActions extends Component {
    constructor(){
        super();
        this.state = {
            isShowingCancelModal: false
        };

        this.onRescheduleButtonClick = this.onRescheduleButtonClick.bind(this);
        this.onCancelButtonClick = this.onCancelButtonClick.bind(this);
        this.closeModal = this.closeModal.bind(this);
        this.cancelAppointment = this.cancelAppointment.bind(this);
    }

    onRescheduleButtonClick(){
        this.props.history.push(PAGE_TOUR_RESCHEDULE);
    }

    onCancelButtonClick(){
        this.setState({
            isShowingCancelModal: true
        });
    }

    closeModal(){
        this.setState({
            isShowingCancelModal: false
        });
    }

    cancelAppointment(){
        return this.props.cancelAppointment().then(() => {
            this.setState({
                isShowingCancelModal: false
            });
        });
    }

    render(){
        const { building, classes, isSubmitting } = this.props;
        const { isShowingCancelModal } = this.state;
        const confirmCancelText = `Would you like to cancel this tour of ${building.building_name || building.street_address}?`;

        return (
            <div className={classes.confirmationActions}>
                <span role="button" aria-label="Cancel" className={classes.confirmationAction}  onClick={this.onCancelButtonClick}>
                    <i className="ni ni-times-circle ni-lg" />
                    <span className={classes.confirmationActionText}>Cancel tour</span>
                </span>
                <span role="link" aria-label="Reschedule" className={classes.confirmationAction}  onClick={this.onRescheduleButtonClick}>
                    <i className="ni ni-clock ni-lg" />
                    <span className={classes.confirmationActionText}>Reschedule tour</span>
                </span>
                <ConfirmModal
                    isOpen={isShowingCancelModal}
                    isSubmitting={isSubmitting}
                    closeModal={this.closeModal}
                    onConfirm={this.cancelAppointment}
                    contentLabel="Confirm Cancel"
                    bodyText={confirmCancelText}
                    confirmButtonActiveText="Cancelling..."
                />
            </div>
        );
    }
}

UpdatedConfirmationPageActions.propTypes = {
    className: PropTypes.string,
    building: PropTypes.object.isRequired,
    cancelAppointment: PropTypes.func.isRequired,
    history: PropTypes.object.isRequired,
    classes: PropTypes.object.isRequired,
    isSubmitting: PropTypes.bool
};

export default withRouter(injectSheet(styles)(UpdatedConfirmationPageActions));
