import React, {useMemo} from 'react'
import PropTypes from 'prop-types'
import PageNav from 'components/page-nav'
import alertTypes from 'containers/alert-manager/alert-types'
import modalTypes from 'containers/modal-manager/modal-types'
import {TABS} from '../../constants'

const CourseNav = ({
    activeTab,
    className,
    course,
    isOwner,
    portfolio,
    tabs: courseTabs,
    showAlert,
    showModal,
}) => {
    // Exclude create a course as a navigation button
    const tabs = courseTabs.filter((tab) => tab.value !== TABS.NO_COURSES)

    const menuItems = useMemo(() => {
        const ownerMenuItems = [
            {
                isDanger: false,
                label: 'Invite',
                description: 'Invite participants to the course',
                onClick: () =>
                    showModal?.({
                        type: modalTypes.MODAL_COURSE_JOIN_LINK,
                        courseId: course.id,
                        name: course.name,
                    }),
            },
            {
                isDanger: false,
                label: 'Settings',
                description: 'Update the course settings',
                onClick: () =>
                    showModal?.({
                        type: modalTypes.MODAL_COURSE_SETTINGS,
                        course,
                        isUpdate: true,
                    }),
            },
            {
                isDanger: true,
                label: 'Delete',
                description: 'Delete the course',
                onClick: () =>
                    showAlert?.({
                        type: alertTypes.CONFIRM_COURSE_DELETE,
                        courseId: course.id,
                    }),
            },
        ]

        const participantMenuItems = [
            {
                isDanger: true,
                label: 'Leave course',
                description: 'Drop out',
                onClick: () =>
                    showAlert?.({
                        type: alertTypes.CONFIRM_PORTFOLIO_DELETE,
                        portfolioId: portfolio?.id,
                    }),
            },
        ]

        return isOwner ? ownerMenuItems : participantMenuItems
    }, [isOwner, course])

    return (
        <PageNav
            activeTab={activeTab}
            className={className}
            tabs={tabs}
            menuItems={menuItems}
            title="Challenge"
        />
    )
}

CourseNav.propTypes = {
    activeTab: PropTypes.number,
    className: PropTypes.string,
    course: PropTypes.object,
    isOwner: PropTypes.bool,
    portfolio: PropTypes.object,
    showAlert: PropTypes.func,
    showModal: PropTypes.func,
    tabs: PropTypes.array,
}

export default CourseNav
