import { Breadcrumbs, Divider, Grid, Typography } from '@mui/material';
import { Box } from '@mui/system';
import { useStore, useStoreUpdate } from '../../store/cart-context';
import CartCard from '../cards/cart-card/CartCard';
import OrderSummaryCard from '../cards/order-summary-card/OrderSummaryCard';

const CartContent = () => {
  const { cartStorage, totalPrice, totalQuantity } = useStore();
  const { removeCartValue, updateItemQuantity } = useStoreUpdate();

  const mapProductsToDom = () => {
    if (cartStorage?.length) {
      return cartStorage.map((element, i) => (
        <CartCard
          key={i}
          product={element?.product}
          initialQuantity={element?.quantity}
          remove={removeCartValue}
          updateQuantity={updateItemQuantity}
        ></CartCard>
      ));
    } else {
      return (
        <Typography
          sx={{
            pl: 12,
            mt: 6,
            height: '100%',
            textAlign: 'center',
            fontSize: 45,
          }}
        >
          Your cart is currently empty
        </Typography>
      );
    }
  };
  return (
    <Grid container spacing={2} sx={{ py: 10, height: '100%', width: '100%' }}>
      <Grid item xs={12}>
        <Typography
          variant="h3"
          sx={{ pl: 12, mt: 12, height: '100%', color: 'primary.main' }}
        >
          Items in Your Cart
        </Typography>
      </Grid>
      <Grid item xs={12}>
        <Divider sx={{ backgroundColor: 'primary.main', mx: 12 }} />
      </Grid>
      <Grid item xs={12} sx={{ mt: 4 }}>
        <Breadcrumbs
          aria-label="breadcrumb"
          separator="›"
          sx={{ pl: 12, fontSize: 20 }}
        >
          <Typography color="red">Cart</Typography>
          <Typography></Typography>
        </Breadcrumbs>
      </Grid>
      <Grid item xs={8}>
        {mapProductsToDom()}
      </Grid>
      <Grid item xs={4}>
        <Box sx={{ width: '80%', mt: 2 }}>
          <OrderSummaryCard
            data={{ totalPrice: totalPrice, totalQuantity: totalQuantity }}
          ></OrderSummaryCard>
        </Box>
      </Grid>
    </Grid>
  );
};

export default CartContent;
