/*
 * 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 ArrowForwardIosIcon from '@mui/icons-material/ArrowForwardIos';
import ArrowBackIosNewIcon from '@mui/icons-material/ArrowBackIosNew';
import AutoModeIcon from '@mui/icons-material/AutoMode';
import { useLingui } from '@lingui/react/macro';
import { msg } from '@lingui/core/macro';
import type { IReaderSettings } from '@/features/reader/Reader.types.ts';
import { ProgressBarPosition, ProgressBarPositionAutoVertical } from '@/features/reader/Reader.types.ts';
import type { ValueToDisplayData } from '@/base/Base.types.ts';
import { ButtonSelectInput } from '@/base/components/inputs/ButtonSelectInput.tsx';

const VALUE_TO_DISPLAY_DATA: ValueToDisplayData<ProgressBarPosition> = {
    [ProgressBarPosition.AUTO]: {
        title: msg`Auto`,
        icon: <AutoModeIcon />,
    },
    [ProgressBarPosition.BOTTOM]: {
        title: msg`Bottom`,
        icon: <ArrowBackIosNewIcon sx={{ transform: 'rotate(90deg)' }} />,
    },
    [ProgressBarPosition.LEFT]: {
        title: msg`Left`,
        icon: <ArrowForwardIosIcon />,
    },
    [ProgressBarPosition.RIGHT]: {
        title: msg`Right`,
        icon: <ArrowBackIosNewIcon />,
    },
};

const PROGRESS_BAR_POSITION_VALUES = Object.values(ProgressBarPosition).filter((value) => typeof value === 'number');

const PROGRESS_BAR_AUTO_VERTICAL_POSITION_VALUES = Object.values(
    ProgressBarPositionAutoVertical,
) as unknown as TupleUnion<keyof typeof ProgressBarPositionAutoVertical>;

export const ReaderSettingProgressBarPosition = ({
    progressBarPosition,
    progressBarPositionAutoVertical,
    updateSetting,
}: Pick<IReaderSettings, 'progressBarPosition' | 'progressBarPositionAutoVertical'> & {
    updateSetting: <
        Position extends keyof Pick<IReaderSettings, 'progressBarPosition' | 'progressBarPositionAutoVertical'>,
    >(
        filter: Position,
        value: IReaderSettings[Position],
    ) => void;
}) => {
    const { t } = useLingui();

    const isAutoPosition = progressBarPosition === ProgressBarPosition.AUTO;

    return (
        <>
            <ButtonSelectInput
                label={t`Progress bar position`}
                value={progressBarPosition}
                values={PROGRESS_BAR_POSITION_VALUES}
                setValue={(position) => updateSetting('progressBarPosition', position)}
                valueToDisplayData={VALUE_TO_DISPLAY_DATA}
            />
            {isAutoPosition && (
                <ButtonSelectInput
                    label={t`Automatic progress bar vertical position`}
                    value={progressBarPositionAutoVertical}
                    values={PROGRESS_BAR_AUTO_VERTICAL_POSITION_VALUES}
                    setValue={(position) => updateSetting('progressBarPositionAutoVertical', position)}
                    valueToDisplayData={VALUE_TO_DISPLAY_DATA}
                />
            )}
        </>
    );
};
