import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { createNamespace } from '../../../../tools/namingTools';
import Button from '../../../../atoms/button/Button';
import { getFullDate } from '../../../../tools/dateHelpers';
import './styles/ProductQuestionItem.scss';

const n = createNamespace('ProductQuestionItem');

export const N_COLS_LIST = 1;
export const N_COLS_GRID = 4;

const ProductQuestionItem = ({
    data: {
        id,
        title,
        comment,
        author,
        lastModified,
        answers,
        byDate,
        i18n: {
            showAnswer,
            hideAnswer
        }
    },
    i18n
}) => {
    const ANSWERS_LIST_ID = `answersList-${id}`;
    const QUESTION_TITLE_ID = `questionTitle-${id}`;

    const i18nWithDefaults = { ...ProductQuestionItem.defaultProps.i18n, ...i18n };

    const [isCollapsed, setCollapsed] = useState(true);

    const renderAnswer = answer => (
        <li key={answer.id} {...n('answerItem').withE2EId().props}>
            <p {...n('description').withE2EId().props}>
                <strong>
                    <abbr title={i18nWithDefaults.answer}>
                        <span aria-hidden="true">{i18nWithDefaults.answer.trim()[0].toUpperCase()}</span>
                    </abbr>
                    <span aria-hidden="true">{': '}</span>
                </strong>
                {answer.comment}
            </p>
            <div {...n('author').props}>
                <span {...n('authorByDate').withE2EId().props}>{answer.i18n.byDate}</span>
                <span {...n('authorName').withE2EId().props}>{answer.author.name}</span>
                <span {...n('authorDate').withE2EId().props}>{getFullDate(answer.lastModified)}</span>
            </div>
        </li>
    );

    const answersBlock = answers && answers.length ? (
        <div {...n('answer').withE2EId().props}>
            <ul {...n('answers').props} aria-label={`${i18nWithDefaults.answersTo} ${title}`} id={ANSWERS_LIST_ID}>
                {isCollapsed ? renderAnswer(answers[0]) : answers.map(renderAnswer)}
            </ul>
            {answers.length > 1 ? (
                <div {...n('btnAnswer').props}>
                    <Button
                        buttonType="link"
                        type="primary"
                        onClick={() => setCollapsed(!isCollapsed)}
                        aria-expanded={!isCollapsed ? 'true' : 'false'}
                        aria-controls={ANSWERS_LIST_ID}
                    >
                        {isCollapsed ? showAnswer : hideAnswer}
                    </Button>
                </div>
            ) : ''}
        </div>
    ) : '';

    return (
        <article {...n('container').props} aria-labelledby={QUESTION_TITLE_ID}>
            {title && (
                <span {...n('title').withE2EId().props} aria-hidden="true">
                    {i18nWithDefaults.question && (
                        <>
                            <abbr title={i18nWithDefaults.question}>
                                {i18nWithDefaults.question.trim()[0].toUpperCase()}
                            </abbr>
                            {': '}
                        </>
                    )}
                    <span id={QUESTION_TITLE_ID}>{title}</span>
                </span>
            )}
            <p {...n('description').withE2EId().props}>{comment}</p>
            <div {...n('author').props}>
                <span {...n('authorByDate').withE2EId().props}>{byDate}</span>
                <span {...n('authorName').withE2EId().props} aria-hidden="true">{author.name}</span>
                <span {...n('authorDate').withE2EId().props} aria-hidden="true">{getFullDate(lastModified)}</span>
            </div>
            {answersBlock}
        </article>
    );
};

export const questionShape = {
    id: PropTypes.number,
    title: PropTypes.string,
    comment: PropTypes.string,
    author: PropTypes.shape({
        uuid: PropTypes.string,
        name: PropTypes.string,
        profilePicUrl: PropTypes.string
    }),
    byDate: PropTypes.string,
    lastModified: PropTypes.number,
    isCurrentUserQuestion: PropTypes.bool,
    answers: PropTypes.arrayOf(
        PropTypes.shape({
            id: PropTypes.number,
            comment: PropTypes.string,
            author: PropTypes.shape({
                uuid: PropTypes.string,
                name: PropTypes.string,
                profilePicUrl: PropTypes.string
            }),
            lastModified: PropTypes.number,
            official: PropTypes.bool,
            isCurrentUserAnswer: PropTypes.bool,
            actionEndpoint: PropTypes.string,
            i18n: PropTypes.shape({
                byDate: PropTypes.string
            })
        })
    ),
    actionEndpoints: PropTypes.shape({
        entry: PropTypes.string,
        comments: PropTypes.string
    }),
    i18n: PropTypes.shape({
        showAnswer: PropTypes.string,
        hideAnswer: PropTypes.string
    })
};

ProductQuestionItem.propTypes = {
    data: PropTypes.shape(questionShape),
    i18n: PropTypes.shape({
        question: PropTypes.string,
        answer: PropTypes.string,
        answersTo: PropTypes.string
    })
};

ProductQuestionItem.defaultProps = {
    data: {},
    i18n: {
        question: 'Question',
        answer: 'Answer',
        answersTo: 'Answers to'
    }
};

export default ProductQuestionItem;
