import React, {useCallback, useEffect} from 'react'
import PropTypes from 'prop-types'

import alertTypes from 'containers/alert-manager/alert-types'
import modalTypes from 'containers/modal-manager/modal-types'
import AccountSettings from './partials/account'
import BillingOverview from './partials/billing'

const SettingsPage = ({
    billing,
    getBillingCharges,
    getBillingOrders,
    getBillingSubscriptions,
    getNotificationSettings,
    showAlert,
    showModal,
    updateNotificationSettings,
    updateSchoolSearch,
    user,
}) => {
    const showAvatarUpdateForm = useCallback(
        () => showModal?.({type: modalTypes.MODAL_AVATAR_UPLOAD}),
        []
    )
    const showDeactivateAccountConfirm = useCallback(
        () => showAlert?.({type: alertTypes.CONFIRM_ACCOUNT_DEACTIVATE}),
        []
    )
    const showCancelSubscriptionConfirm = useCallback(
        ({id}) =>
            showAlert?.({
                type: alertTypes.CONFIRM_SUBSCRIPTION_CANCEL,
                userId: user.id,
                id,
            }),
        []
    )

    const getSubscriptions = useCallback(
        ({startingAfter} = {}) =>
            getBillingSubscriptions?.({startingAfter, userId: user.id}),
        [user.id]
    )
    const getOrders = useCallback(
        ({startingAfter} = {}) =>
            getBillingOrders?.({startingAfter, userId: user.id}),
        [user.id]
    )
    const getCharges = useCallback(
        ({startingAfter} = {}) =>
            getBillingCharges?.({startingAfter, userId: user.id}),
        [user.id]
    )

    useEffect(() => {
        getNotificationSettings?.({userId: user.id})
    }, [])

    return (
        <React.Fragment>
            <AccountSettings
                user={user}
                onDeactivateAccount={showDeactivateAccountConfirm}
                onShowAvatarUpdate={showAvatarUpdateForm}
                onUpdateNotificationSettings={updateNotificationSettings}
                onUpdateSchoolSearch={updateSchoolSearch}
            />

            <BillingOverview
                billing={billing}
                showModal={showModal}
                onCancelSubscriptions={showCancelSubscriptionConfirm}
                onGetCharges={getCharges}
                onGetOrders={getOrders}
                onGetSubscriptions={getSubscriptions}
            />
        </React.Fragment>
    )
}

SettingsPage.pageTitle = 'Settings'

SettingsPage.propTypes = {
    user: PropTypes.shape({
        email: PropTypes.string,
        firstName: PropTypes.string,
        id: PropTypes.string,
        lastName: PropTypes.string,
        getNotificationSettings: PropTypes.object,
        roles: PropTypes.array,
        schoolId: PropTypes.string,
        studentNumber: PropTypes.string,
    }).isRequired,
    billing: PropTypes.object,
    cancelSubscriptions: PropTypes.func,
    getBillingCharges: PropTypes.func,
    getBillingOrders: PropTypes.func,
    getBillingSubscriptions: PropTypes.func,
    getNotificationSettings: PropTypes.func,
    showAlert: PropTypes.func,
    showModal: PropTypes.func,
    updateNotificationSettings: PropTypes.func,
    updateSchoolSearch: PropTypes.func,
}

export default SettingsPage
