import {CardActions, CardContent, CardMedia, Rating} from "@mui/material";
import {useAlert} from "../../hooks/useAlert";
import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button";
import Grid from "@mui/material/Grid";
import {requests} from "../../requests";
import {CocktailItemStyled} from "./CocktailItemStyled";
import IconButton from "@mui/material/IconButton";
import FavoriteBorderIcon from '@mui/icons-material/FavoriteTwoTone';
import FavoriteIcon from '@mui/icons-material/Favorite';
import {api} from "../../lib/clients/api";
import Box from "@mui/material/Box";
import {useUser} from "../../hooks/useUser";
import {CocktailDescription} from "./CocktailDescription";

function renderFavouriteBadge(handleFavourite, row) {
    const childIcon = row.rating.favourite ? <FavoriteIcon color='error'/> : <FavoriteBorderIcon color={'warning'}/>;
    return (
        <IconButton sx={{position: 'absolute', top: "15px", right: "15px"}} onClick={() => handleFavourite(row)}>
            {childIcon}
        </IconButton>
    )
}

function renderRating(handleChangeRating, row) {
    return (
        <Rating
            sx={{position: 'absolute', top: '310px', right: '85px'}}
            name="simple-controlled"
            size="large"
            value={row.rating.rating}
            onChange={(event, newValue) => handleChangeRating(row, newValue)}
        />
    )
}

export function Cocktail({row, handleFavourite, handleChangeRating, handleSelect, editMenuBlock}) {
    const {createAlert, createError} = useAlert();
    const {session, user} = useUser();

    function pay(cocktailId) {
        api().post(`${requests.bar.pay}cocktail=${cocktailId}`)
            .then(() => createAlert("Ожидайте свой заказ", "success"))
            .catch(() => createError("Ошибка во время создания заказа"))
    }

    return (
        <Grid item sx={{pr: 2}}>
            <CocktailItemStyled>
                <Box sx={{
                    p: '4px 4px',
                    m: 1,
                    width: '320px',
                    position: 'relative',
                }}>
                    <CardMedia
                        sx={{
                            loading: "eager",
                            borderRadius: 2
                        }}
                        onClick={() => handleSelect(row)}
                        component="img"
                        alt={row.name}
                        height="300"

                        image={`${row.image}/preview`}
                    />
                    {renderFavouriteBadge(handleFavourite, row)}
                    {renderRating(handleChangeRating, row)}
                    <CardContent sx={{pb: '4px', pl: 2}}>
                        <Typography variant="h5" minHeight={'50px'} mt={2}>{row.name} </Typography>
                        <CocktailDescription row={row}/>
                    </CardContent>
                    <CardActions>
                        {(row.isAllowed && session.isActive && user.invited) &&
                            <Button variant="contained" onClick={() => pay(row.id)}>Заказать</Button>
                        }
                        {editMenuBlock(row)}
                    </CardActions>
                </Box>
            </CocktailItemStyled>
        </Grid>
    )
}