/** *******************************************************************************************************************
  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  
  Licensed under the Apache License, Version 2.0 (the "License").
  You may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.                                                                              *
 ******************************************************************************************************************** */

import React, { FunctionComponent, useMemo } from 'react';
import { makeStyles } from '@material-ui/core/styles';
import CircularProgress, { CircularProgressProps } from '@material-ui/core/CircularProgress';
import Typography from '@material-ui/core/Typography';
import LinearProgress, { LinearProgressProps } from '@material-ui/core/LinearProgress';
import Box from '../../layouts/Box';
import Grid from '../../layouts/Grid';
import StatusIndicator from '../StatusIndicator';
import Button from '../Button';
import Heading from '../Heading';
import Text from '../Text';

const useStyles = makeStyles((theme) => ({
    colorPrimary: {
        marginTop: '8px',
    },
    circularColorPrimary: {
        color: theme.palette.info.dark,
        position: 'absolute',
        left: 0,
    },
    circularColorBottom: {
        color: theme.palette.grey[theme.palette.type === 'light' ? 200 : 700],
    },
    barColorPrimary: {
        backgroundColor: theme.palette.info.dark,
    },
    label: {
        color: theme.palette.text.primary,
    },
    description: {
        padding: theme.spacing(0.3, 0),
        color: theme.palette.text.secondary,
    },
    body: {
        display: 'inline-flex',
    },
    icon: {
        paddingTop: '4px',
    },
    resultButton: {
        marginLeft: '10px',
    },
}));

export interface ProgressBarProps {
    /** Percentage value of the progress */
    value?: number;
    /**
     * Use "in-progress" to display a progress bar.
     * "success" and "error" are result states and replace the progress element with a status indicator,
     * resultText and, if set, with a button containing resultButtonText.
     * */
    status?: 'in-progress' | 'success' | 'error';
    /**
     * Text for the button that gets displayed when status is set to "error" or "success".
     * If resultButtonText is empty, no button will be displayed. */
    resultButtonText?: string;
    /** Short information summarizing the operation */
    label?: string;
    /** More detailed information about the operation, rendered between the progress bar and label. */
    description?: string;
    /** Information that is displayed below the progress bar. */
    additionalInfo?: string;
    /** Content that is displayed whenever status is set to "error" or "success". */
    resultText?: string;
    /** Indicate whether to display value on the right end */
    displayValue?: boolean;
    /** Fired when the user triggers the result state button. */
    resultButtonClick?: () => void;
    /** Indicate what type of progress bar to render */
    variant?: 'linear' | 'circular';
}

interface ProgressBarComponentProps {
    value: number;
    displayValue: boolean;
}

const statusMapping: { [key in 'error' | 'success']: 'negative' | 'positive' } = {
    error: 'negative',
    success: 'positive',
};

const LinearProgressComponent: React.FunctionComponent<
    ProgressBarComponentProps & Omit<LinearProgressProps, 'variant'>
> = ({ value, displayValue, ...props }) => {
    const classes = useStyles();

    return (
        <Grid container spacing={3}>
            <Grid item xs={value && displayValue ? 11 : 12}>
                <LinearProgress
                    variant="determinate"
                    value={value}
                    classes={{
                        colorPrimary: classes.colorPrimary,
                        barColorPrimary: classes.barColorPrimary,
                    }}
                    {...props}
                />
            </Grid>
            {displayValue && value && (
                <Grid item xs={1}>
                    <Text>{value}%</Text>
                </Grid>
            )}
        </Grid>
    );
};

const CircularProgressWithLabel: React.FunctionComponent<
    ProgressBarComponentProps & Omit<CircularProgressProps, 'variant'>
> = ({ value, displayValue, ...props }) => {
    const classes = useStyles();

    return (
        <Box position="relative" display="inline-flex">
            <CircularProgress
                value={100}
                variant="determinate"
                classes={{ colorPrimary: classes.circularColorBottom }}
                {...props}
            />
            <CircularProgress
                variant="determinate"
                value={value}
                classes={{ colorPrimary: classes.circularColorPrimary }}
                {...props}
            />
            {displayValue && (
                <Box
                    display="flex"
                    top={0}
                    bottom={0}
                    right={0}
                    left={0}
                    position="absolute"
                    alignItems="center"
                    justifyContent="center"
                >
                    <Typography variant="caption" component="div" color="textSecondary">{`${Math.round(
                        value
                    )}%`}</Typography>
                </Box>
            )}
        </Box>
    );
};

/**
 * A progress bar is a horizontal progress-bar for indicating progress and activity.
 */
const ProgressBar: FunctionComponent<
    ProgressBarProps & Omit<LinearProgressProps & CircularProgressProps, 'variant'>
> = ({
    value,
    displayValue = true,
    status = 'in-progress',
    variant = 'linear',
    label,
    description,
    additionalInfo,
    resultText,
    resultButtonText,
    resultButtonClick,
    ...props
}) => {
    const classes = useStyles();

    const progressBody = (
        ProgressBarComponent: React.FunctionComponent<ProgressBarComponentProps>
    ): React.ReactNode => {
        if (status === 'error' || status === 'success') {
            return (
                <div className={classes.body}>
                    <div className={classes.icon}>
                        <StatusIndicator statusType={statusMapping[status]}>{resultText}</StatusIndicator>
                    </div>
                    {resultButtonText && (
                        <div className={classes.resultButton}>
                            <Button onClick={resultButtonClick}>{resultButtonText}</Button>
                        </div>
                    )}
                </div>
            );
        }

        return (
            <ProgressBarComponent
                value={value || (value === 0 ? 0 : 100)}
                displayValue={displayValue}
                {...props}
                data-testid={`${testId}-indicator`}
            />
        );
    };

    const ProgressBarComponent = useMemo(
        () => (variant === 'linear' ? LinearProgressComponent : CircularProgressWithLabel),
        [variant]
    );

    const testId = props['data-testid'] || 'progress-bar';

    return (
        <div data-testid={testId}>
            {label && <Heading variant="h3">{label}</Heading>}
            {description && (
                <Typography className={classes.description} variant="body1">
                    {description}
                </Typography>
            )}
            {progressBody(ProgressBarComponent)}
            {additionalInfo && (
                <Typography className={classes.description} variant="body1">
                    {additionalInfo}
                </Typography>
            )}
        </div>
    );
};

export default ProgressBar;
