/*
 * 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 Stack from '@mui/material/Stack';
import { useLingui } from '@lingui/react/macro';
import type { IReaderSettings } from '@/features/reader/Reader.types.ts';
import { CheckboxInput } from '@/base/components/inputs/CheckboxInput.tsx';
import type { ReaderService } from '@/features/reader/services/ReaderService.ts';
import { SliderInput } from '@/base/components/inputs/SliderInput.tsx';
import { DEFAULT_READER_SETTINGS } from '@/features/reader/settings/ReaderSettings.constants.tsx';
import { isReaderWidthEditable } from '@/features/reader/settings/ReaderSettings.utils.tsx';
import { ResetButton } from '@/base/components/buttons/ResetButton.tsx';
import type { SelectButtonDefaultableProps } from '@/base/components/buttons/SelectButton.tsx';

export const ReaderSettingWidth = ({
    readerWidth,
    pageScaleMode,
    isDefaultable,
    onDefault,
    updateSetting,
    setTransparent,
}: Pick<IReaderSettings, 'readerWidth' | 'pageScaleMode'> &
    Pick<SelectButtonDefaultableProps<IReaderSettings['readerWidth']['value']>, 'isDefaultable' | 'onDefault'> & {
        updateSetting: (...args: Parameters<typeof ReaderService.updateSetting>) => void;
        setTransparent?: (transparent: boolean) => void;
    }) => {
    const { t } = useLingui();

    if (!isReaderWidthEditable(pageScaleMode)) {
        return null;
    }

    return (
        <Stack>
            <Stack sx={{ flexDirection: 'row', justifyContent: 'space-between' }}>
                <CheckboxInput
                    label={t`Limit reader width`}
                    checked={readerWidth.enabled}
                    onChange={(_, checked) => updateSetting('readerWidth', { ...readerWidth, enabled: checked }, true)}
                />
                {isDefaultable && <ResetButton onClick={onDefault} variant="outlined" />}
            </Stack>
            {readerWidth.enabled && (
                <SliderInput
                    label={t`Reader width`}
                    value={`${readerWidth.value}%`}
                    slotProps={{
                        slider: {
                            defaultValue: DEFAULT_READER_SETTINGS.readerWidth.value,
                            value: readerWidth.value,
                            step: 1,
                            min: 10,
                            max: 100,
                            onChange: (_, value) => {
                                setTransparent?.(true);
                                updateSetting('readerWidth', { ...readerWidth, value: value as number }, false);
                            },
                            onChangeCommitted: (_, value) => {
                                setTransparent?.(false);
                                updateSetting('readerWidth', { ...readerWidth, value: value as number }, true);
                            },
                        },
                    }}
                />
            )}
        </Stack>
    );
};
