import React from 'react';
import PropTypes from 'prop-types';
import { getVideoPaddingTop } from '../../tools/aspectRatios';
import { ASPECT_RATIO_VIDEO } from '../../constants/aspectRatios';
import { createNamespace } from '../../tools/namingTools';
// IMPORTANT: (s)css should be imported after other atoms/components
import './styles/VideoPlayer.scss';

const n = createNamespace('VideoPlayer');

const VideoPlayer = ({
    title,
    src,
    current,
    i18n
}) => {
    if (!src) {
        return null;
    }

    // Small hack for having iframe to keep the video aspect ratio of 16:9
    const paddingTop = getVideoPaddingTop(ASPECT_RATIO_VIDEO);

    const modifiers = paddingTop && 'smart-ratio';

    const componentVariables = {
        'wrapper-padding-top': paddingTop && `${paddingTop}%`
    };

    let tabIndexValue = 0;
    if (current !== undefined) {
        tabIndexValue = !current && '-1';
    }
    return (
        <div {...n('wrapper', modifiers).withVariables(componentVariables).props}>
            <iframe
                {...n('video', modifiers).withTestId().props}
                title={title || i18n.video}
                src={src}
                allowFullScreen
                // eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
                tabIndex={tabIndexValue}
            />
        </div>
    );
};

VideoPlayer.propTypes = {
    title: PropTypes.string,
    src: PropTypes.string,
    current: PropTypes.oneOfType([PropTypes.bool, undefined]),
    i18n: PropTypes.shape({
        video: PropTypes.string
    })
};

VideoPlayer.defaultProps = {
    title: '',
    src: '',
    current: undefined,
    i18n: {
        video: 'Video'
    }
};

export default VideoPlayer;
