import { Card, Checkbox, Divider, Space } from "antd";
import ArabicPrice from "components/ArabicPrice";
import {
  selectCart,
  selectCartTotal,
  selectGrandTotal,
  selectHighestPricedLoyaltyItem,
  selectLoyaltyValidation,
  selectTaxAmount,
  updateUseLoyaltyPoints,
} from "features/order/orderSlice";
import { OrderType } from "pages/checkout/hooks/types";
import { useTranslation } from "react-i18next";
import { useGetRestaurantDetailsQuery } from "redux/api/others";
import { useAppDispatch, useAppSelector } from "redux/hooks";
import ProText from "../ProText";
import ProTitle from "../ProTitle";
import styles from "./OrderSummary.module.css";

export default function OrderSummary() {
  const { t } = useTranslation();
  const { useLoyaltyPoints } = useAppSelector(selectCart);
  const { data: restaurant } = useGetRestaurantDetailsQuery("595");
  const { orderType } = useAppSelector(selectCart);
  const dispatch = useAppDispatch();
  const subtotal = useAppSelector(selectCartTotal);
  const loyaltyValidation = useAppSelector(selectLoyaltyValidation);
  const highestLoyaltyItem = useAppSelector(selectHighestPricedLoyaltyItem);
  const taxAmount = useAppSelector(selectTaxAmount);
  const grandTotal = useAppSelector(selectGrandTotal);

  const isHasLoyaltyGift =
    (restaurant?.loyalty_stamps ?? 0) -
      (restaurant?.customer_loyalty_points ?? 0) <=
    0;

  return (
    <>
      <Card className={`${styles.orderSummary}`}>
        <ProTitle style={{ fontSize: 18 }}>{t("cart.orderSummary")}</ProTitle>
        <Divider style={{ margin: "15px 0 15px 0" }} />
        <Space direction="vertical" style={{ width: "100%" }}>
          <div className={styles.summaryRow}>
            <ProText type="secondary">{t("cart.basketTotal")}</ProText>
            <ArabicPrice price={subtotal} />
          </div>
          {orderType != OrderType.DineIn && <div className={styles.summaryRow}>
            <ProText type="secondary">{t("cart.deliveryFee")}</ProText>
            <ArabicPrice price={Number(restaurant?.delivery_fees || 0)} />
          </div>}
          <div className={styles.summaryRow}>
            <ProText type="secondary">{t("cart.discount")}</ProText>
            <ArabicPrice price={highestLoyaltyItem?.price || 0} />
          </div>
          <div className={styles.summaryRow}>
            <ProText type="secondary">{t("cart.tax")}</ProText>
            <ArabicPrice price={taxAmount || 0} />
          </div>
          <Divider className={styles.summaryDivider} />
          <div className={`${styles.summaryRow} ${styles.totalRow}`}>
            <ProText strong type="secondary">
              {t("cart.totalAmount")}
            </ProText>
            <ArabicPrice price={grandTotal} />
          </div>
        </Space>

        {isHasLoyaltyGift && (
          <>
            <br />
            <br />
            <Checkbox
              checked={useLoyaltyPoints}
              onChange={(value) => {
                dispatch(updateUseLoyaltyPoints(value.target.checked));
              }}
            >
              {t("cart.useLoyaltyPoints")}
            </Checkbox>
          </>
        )}

        {isHasLoyaltyGift && loyaltyValidation.errorMessage && (
          <div style={{ marginTop: 8, color: "red", fontSize: "12px" }}>
            {t(loyaltyValidation.errorMessage)}
          </div>
        )}

        {isHasLoyaltyGift && useLoyaltyPoints && highestLoyaltyItem && (
          <div style={{ marginTop: 8, color: "green", fontSize: "12px" }}>
            {t("cart.loyaltyDiscountApplied", {
              itemName: highestLoyaltyItem.name,
              amount: Math.round(highestLoyaltyItem.price || 0).toFixed(2),
            })}
          </div>
        )}
      </Card>
    </>
  );
}
