'use strict';

import React from 'react';
import PropTypes from 'prop-types';
import {Popup} from './Popup';
import {Button} from '../Button';
import {DangerousHTML} from '../DangerousHTML';

export class Alert extends React.Component {
    static propTypes = {
        buttonYesStyle: PropTypes.string,
        submitAction: PropTypes.func,
        submitText: PropTypes.string,
        title: PropTypes.string,
        testAutomationId: PropTypes.string,
        shouldDeriveDescFromBody: PropTypes.bool
    };

    static defaultProps = {
        buttonYesStyle: 'primary',
        title: 'TRANSLATE:ALERT',
        submitText: 'TRANSLATE:OK',
        submitAction: null,
        testAutomationId: 'alert',
        shouldDeriveDescFromBody: true
    };

    constructor(props, context) {
        super(props, context);

        this.submitAction = this.submitAction.bind(this);
    }

    submitAction() {
        Popup.close();
        if (this.props.submitAction) {
            this.props.submitAction();
        }
    }

    render() {
        const {buttonYesStyle, submitText, submitAction, ...props} = this.props;
        let buttons = [
            <Button
                testAutomationId="buttonYes"
                key="buttonYes"
                bsStyle={buttonYesStyle}
                onClick={this.submitAction}>
                <DangerousHTML>{submitText}</DangerousHTML>
            </Button>
        ];

        return <Popup {...props} bsSize="sm" footer={buttons} onClose={this.submitAction} />;
    }
}
