'use strict';

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

type Props = {
    buttonYesStyle?: string;
    submitAction?: Function;
    submitText?: string;
    title?: string;
    testAutomationId?: string;
} & DefaultProps;

type DefaultProps = {
    buttonYesStyle: string;
    title: string;
    submitText: string;
    submitAction: Function;
    testAutomationId: string;
};

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

    static defaultProps = {
        buttonYesStyle: 'primary',
        get title() {
            return lang.ALERT;
        },
        get submitText() {
            return lang.OK;
        },
        submitAction: () => {},
        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 as any} bsSize="sm" footer={buttons} onClose={this.submitAction} />;
    }
}
