import React from 'react';
import PropTypes from 'prop-types';
import { createNamespace } from '../../../../tools/namingTools';
import { screenshotShape } from '../../../../constants/components/prop-types/product-image-viewer';
import Icon from '../../../../atoms/icon/Icon';
import Image from '../../../../atoms/image/Image';
import { getImageSrc } from '../../../../tools/images';
// IMPORTANT: (s)css should be imported after other atoms/components
import './styles/ProductImageViewerThumbnail.scss';

const n = createNamespace('ProductImageViewerThumbnail');

const ProductImageViewerThumbnail = ({
    name,
    imageAlt,
    imageUrl,
    imageUrlSrcset,
    isVideo,
    navigateToSlide,
    slideIndex,
    isCurrentSlide,
    className: slickClassName = '',
    style: slickStyle,
    onClick: slickOnClick,
    tabIndex: _slickTabIndex,
    'aria-hidden': ariaHidden,
    'data-index': dataIndex,
    videoUrl: _videoUrl,
    ...rest
}) => {
    const hasImage = imageUrl || imageUrlSrcset;

    const baseProps = n('wrapper', hasImage && 'with-image').withE2EId().withClass(slickClassName).props;
    const slickDataIndex = dataIndex === undefined || dataIndex === null ? null : Number(dataIndex);
    const isCanonicalThumbnailCell = isCurrentSlide && (
        slickDataIndex === null
            || Number.isNaN(slickDataIndex)
            || slickDataIndex === slideIndex
    );
    const hiddenFromAssistiveTech = ariaHidden === true || ariaHidden === 'true';

    let ariaCurrent;
    let tabIndex = -1;
    if (isCanonicalThumbnailCell && !hiddenFromAssistiveTech) {
        ariaCurrent = 'true';
        tabIndex = 0;
    }

    let onClick = slickOnClick;
    if (!onClick && navigateToSlide) {
        onClick = () => navigateToSlide(slideIndex);
    }

    return (
        <button
            {...baseProps}
            {...rest}
            style={{ ...baseProps.style, ...slickStyle }}
            tabIndex={tabIndex}
            aria-hidden={ariaHidden}
            aria-current={ariaCurrent}
            data-index={dataIndex}
            onClick={onClick}
            type="button"
        >
            {isVideo && (
                <>
                    <span {...n('text').props}>{name}</span>
                    <Icon {...n('icon').props} type="play" />
                </>
            )}
            {hasImage && (
                <Image
                    {...n('image', isVideo && 'video-preview').withTestId().withE2EId().props}
                    alt={imageAlt || name}
                    src={getImageSrc(imageUrl, imageUrlSrcset)}
                />
            )}
        </button>
    );
};

ProductImageViewerThumbnail.propTypes = {
    ...screenshotShape,
    ...{
        isVideo: PropTypes.bool,
        videoUrl: PropTypes.string,
        navigateToSlide: PropTypes.func,
        slideIndex: PropTypes.number.isRequired,
        isCurrentSlide: PropTypes.bool.isRequired
    }
};

ProductImageViewerThumbnail.defaultProps = {
    name: '',
    imageUrl: null,
    imageUrlSrcset: null,
    imageAlt: null,
    isVideo: false
};

export default ProductImageViewerThumbnail;
