/*
 * Copyright (C) Contributors to the Suwayomi project
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

import Box from '@mui/material/Box';
import type { ReactNode } from 'react';
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
import type { CurrentPageSlotProps } from '@/features/reader/overlay/progress-bar/ReaderProgressBar.types.ts';
import { applyStyles } from '@/base/utils/ApplyStyles.ts';
import { getProgressBarPositionInfo } from '@/features/reader/overlay/progress-bar/ReaderProgressBar.utils.tsx';
import { READER_PROGRESS_BAR_POSITION_TO_PLACEMENT } from '@/features/reader/settings/ReaderSettings.constants.tsx';
import { coerceIn } from '@/lib/HelperFunctions.ts';

export const ReaderProgressBarCurrentPageSlot = ({
    pageName,
    currentPagesIndex,
    pagesLength,
    isDragging,
    boxProps,
    children,
    progressBarPosition,
}: CurrentPageSlotProps & { children?: ReactNode }) => {
    const coercedTotalPages = coerceIn(pagesLength - 1, 1);
    const coercedCurrentPagesIndex = coerceIn(currentPagesIndex - 1, 0, coercedTotalPages);

    return (
        <CustomTooltip
            title={pageName}
            slotProps={{
                tooltip: { sx: { backgroundColor: 'primary.main', color: 'primary.contrastText' } },
            }}
            placement={READER_PROGRESS_BAR_POSITION_TO_PLACEMENT[progressBarPosition]}
            disableTouchListener
        >
            <Box
                {...boxProps}
                sx={{
                    position: 'absolute',
                    cursor: isDragging ? 'grabbing' : 'grab',
                    ...applyStyles(getProgressBarPositionInfo(progressBarPosition).isHorizontal, {
                        left: `${(coercedCurrentPagesIndex / coercedTotalPages) * 100}%`,
                        width: `calc(100% / ${coercedTotalPages})`,
                        height: '100%',
                    }),
                    ...applyStyles(getProgressBarPositionInfo(progressBarPosition).isVertical, {
                        top: `${(coercedCurrentPagesIndex / coercedTotalPages) * 100}%`,
                        width: '100%',
                        height: `calc(100% / ${coercedTotalPages})`,
                    }),
                    ...boxProps?.sx,
                }}
            >
                {children}
            </Box>
        </CustomTooltip>
    );
};
