/*
 * 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 Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import TextField from '@mui/material/TextField';
import DialogActions from '@mui/material/DialogActions';
import { useEffect, useMemo, useState } from 'react';
import { useLingui } from '@lingui/react/macro';
import { PasswordTextField } from '@/base/components/inputs/PasswordTextField.tsx';

export type TextSettingDialogProps = {
    settingName: string;
    dialogTitle?: string;
    dialogDescription?: string;
    value?: string;
    handleChange: (value: string) => void;
    isPassword?: boolean;
    placeholder?: string;
    isDialogOpen: boolean;
    setIsDialogOpen: (open: boolean) => void;
    validate?: (value: string) => boolean;
    onExitComplete?: () => void;
    onDismiss?: () => void;
    disabled?: boolean;
};

export const TextSettingDialog = ({
    settingName,
    dialogTitle = settingName,
    dialogDescription,
    value,
    handleChange,
    isPassword = false,
    placeholder = '',
    isDialogOpen,
    setIsDialogOpen,
    validate = () => true,
    onExitComplete,
    onDismiss,
    disabled,
}: TextSettingDialogProps) => {
    const { t } = useLingui();

    const [dialogValue, setDialogValue] = useState(value ?? '');
    const [isValidValue, setIsValidValue] = useState(true);

    const error = !isValidValue && !!dialogValue.length;

    const TextFieldComponent = useMemo(() => (isPassword ? PasswordTextField : TextField), [isPassword]);

    useEffect(() => {
        if (!value) {
            return;
        }

        setDialogValue(value);
    }, [value]);

    const closeDialog = (resetValue: boolean = true) => {
        if (disabled) {
            return;
        }

        if (resetValue) {
            setDialogValue(value ?? '');
            setIsValidValue(true);
        }

        setIsDialogOpen(false);

        const rejected = resetValue;
        if (rejected) {
            onDismiss?.();
        }
    };

    const updateSetting = () => {
        closeDialog(false);
        handleChange(dialogValue);
    };

    return (
        <Dialog open={isDialogOpen} onClose={() => closeDialog()} fullWidth onTransitionExited={onExitComplete}>
            <DialogTitle>{dialogTitle}</DialogTitle>
            <DialogContent>
                {!!dialogDescription && (
                    <DialogContentText sx={{ paddingBottom: '10px' }}>{dialogDescription}</DialogContentText>
                )}
                <TextFieldComponent
                    sx={{
                        width: '100%',
                        margin: 'auto',
                    }}
                    autoFocus
                    placeholder={placeholder}
                    value={dialogValue}
                    error={error}
                    helperText={error ? t`Invalid input` : ''}
                    onChange={(e) => {
                        const newValue = e.target.value;

                        setIsValidValue(validate(newValue));
                        setDialogValue(newValue);
                    }}
                />
            </DialogContent>
            <DialogActions>
                <Button disabled={disabled} onClick={() => closeDialog()} color="primary">
                    {t`Cancel`}
                </Button>
                <Button onClick={() => updateSetting()} disabled={disabled || !isValidValue} color="primary">
                    {t`Ok`}
                </Button>
            </DialogActions>
        </Dialog>
    );
};
