import { Button, Form, Input, message } from "antd";
import { CouponBottomSheet } from "components/CustomBottomSheet/CouponBottomSheet.tsx";
import { CouponDialog } from "components/CustomBottomSheet/CouponDialog.tsx";
import CouponHeartIcon from "components/Icons/cart/CouponHeart.tsx";
import DonateIcon from "components/Icons/cart/DonateIcon.tsx";
import ProInputCard from "components/ProInputCard/ProInputCard.tsx";
import ProText from "components/ProText.tsx";
import { selectCart, updateCoupon } from "features/order/orderSlice.ts";
import useBreakPoint from "hooks/useBreakPoint.ts";
import styles from "pages/cart/cart.module.css";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { useAppDispatch, useAppSelector } from "redux/hooks.ts";
import { colors } from "ThemeConstants.ts";

export default function CouponCard() {
  const { t } = useTranslation();
  const dispatch = useAppDispatch();
  const { coupon } = useAppSelector(selectCart);
  const { isDesktop } = useBreakPoint();

  const [isCouponOpen, setIsCouponOpen] = useState(false);

  const handleCouponSave = (value: string) => {
    dispatch(updateCoupon(value));
    message.success(t("cart.coupon") + " " + t("updatedSuccessfully"));
  };

  const handleCouponClose = () => {
    setIsCouponOpen(false);
  };

  return (
    <>
      <ProInputCard
        title={t("cart.couponCode")}
        titleRight={
          <div
            style={{
              display: "flex",
              flexDirection: "row",
              alignItems: "center",
              gap: 10,
            }}
            onClick={() => setIsCouponOpen(true)}
          >
            <ProText
              style={{
                color: colors.primary,
                fontSize: 14,
                cursor: "pointer",
              }}
            >
              {t("cart.viewOffers")}
            </ProText>
            <DonateIcon />
          </div>
        }
        dividerStyle={{ margin: "5px 0 0 0" }}
      >
        <Form.Item
          label={t("cart.couponCode")}
          name="coupon"
          style={{ position: "relative", top: -5 }}
        >
          <Input
            placeholder={t("cart.couponCode")}
            size="large"
            autoFocus={false}
            style={{ padding: "7px 11px", height: 50 }}
            suffix={
              <Button
                style={{
                  width: 100,
                  height: 32,
                  borderRadius: 100,
                  backgroundColor: "black",
                  color: "white",
                }}
              >
                {t("cart.apply")}
                <CouponHeartIcon className={styles.couponApplyIcon} />
              </Button>
            }
          />
        </Form.Item>
      </ProInputCard>
      {isDesktop ? (
        <CouponDialog
          isOpen={isCouponOpen}
          onClose={handleCouponClose}
          initialValue={coupon}
          onSave={handleCouponSave}
        />
      ) : (
        <CouponBottomSheet
          isOpen={isCouponOpen}
          onClose={handleCouponClose}
          initialValue={coupon}
          onSave={handleCouponSave}
        />
      )}
    </>
  );
}
