/*
 * 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 List from '@mui/material/List';
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogActions from '@mui/material/DialogActions';
import Button from '@mui/material/Button';
import ListItem from '@mui/material/ListItem';
import { Link } from 'react-router-dom';
import Stack from '@mui/material/Stack';
import type { AwaitableComponentProps } from 'awaitable-component';
import { useLingui } from '@lingui/react/macro';
import { BrowseTab } from '@/features/browse/Browse.types.ts';
import { AppRoutes } from '@/base/AppRoute.constants.ts';
import type { ValidateBackupResult } from '@/lib/graphql/generated/graphql-base.types.ts';

export const BackupValidationDialog = ({
    validationResult,
    onDismiss,
    onSubmit,
    isVisible,
    onExitComplete,
}: AwaitableComponentProps & { validationResult: ValidateBackupResult }) => {
    const { t } = useLingui();

    return (
        <Dialog open={isVisible} onTransitionExited={onExitComplete} onClose={onDismiss}>
            <DialogTitle>{t`Backup validation`}</DialogTitle>
            <DialogContent dividers>
                {!!validationResult?.missingSources.length && (
                    <List
                        sx={{ listStyleType: 'initial', listStylePosition: 'inside' }}
                        subheader={t`The following sources are not installed:`}
                    >
                        {validationResult?.missingSources.map(({ id, name }) => (
                            <ListItem sx={{ display: 'list-item' }} key={id}>
                                {`${name} (${id})`}
                            </ListItem>
                        ))}
                    </List>
                )}
                {!!validationResult?.missingTrackers.length && (
                    <List
                        sx={{ listStyleType: 'initial', listStylePosition: 'inside' }}
                        subheader={t`The following trackers are not logged in:`}
                    >
                        {validationResult?.missingTrackers.map(({ name }) => (
                            <ListItem sx={{ display: 'list-item' }} key={name}>
                                {`${name}`}
                            </ListItem>
                        ))}
                    </List>
                )}
            </DialogContent>
            <DialogActions>
                <Stack
                    direction="row"
                    sx={{
                        justifyContent: 'space-between',
                        width: '100%',
                    }}
                >
                    {!!validationResult?.missingSources.length && (
                        <Button
                            onClick={onDismiss}
                            component={Link}
                            to={AppRoutes.browse.path(BrowseTab.EXTENSIONS)}
                            autoFocus={!!validationResult?.missingSources.length}
                            variant={validationResult?.missingSources.length ? 'contained' : 'text'}
                        >
                            {t`Install`}
                        </Button>
                    )}
                    {!!validationResult?.missingTrackers.length && (
                        <Button
                            onClick={onDismiss}
                            component={Link}
                            to={AppRoutes.settings.children.tracking.path}
                            autoFocus={!!validationResult?.missingTrackers.length}
                            variant={validationResult?.missingTrackers.length ? 'contained' : 'text'}
                        >
                            {t`Log in`}
                        </Button>
                    )}
                    <Stack direction="row">
                        <Button onClick={onDismiss}>{t`Cancel`}</Button>
                        <Button
                            onClick={onSubmit}
                            autoFocus={
                                !validationResult?.missingSources.length && !validationResult?.missingTrackers.length
                            }
                            variant={
                                !validationResult?.missingSources.length && !validationResult?.missingTrackers.length
                                    ? 'contained'
                                    : 'text'
                            }
                        >
                            {t`Restore`}
                        </Button>
                    </Stack>
                </Stack>
            </DialogActions>
        </Dialog>
    );
};
