import React from "react";
import PropTypes from "prop-types";
import {
  Details,
  OfferDescriptionText,
  OfferDescriptionTitle,
  OfferImage,
  OfferLittleDetails,
  OfferTitle,
  ScrollerHorizontal,
  ScrollerVertical,
  OfferInfoContainer,
  DesciprtionPostDate,
} from "./OfferDetails.styled";
import { useTranslation } from "react-i18next";
import { formatDateLocale } from "../../../../util/helpers/dateHelpers";
import useIsMobile from "../../../../hooks/useIsMobile";
import { getImageUrl, variants } from "../../../../util/helpers/imageUrlGetter";
import { useEffect } from "react";
import { useState } from "react";
import ImagesCarousel from "../ImagesCarousel/ImagesCarousel";

const OfferDetails = (props) => {
  const offer = props.offer;
  const { t } = useTranslation();
  const { isMobile } = useIsMobile();
  const [images, setImages] = useState([]);
  const [imagesCarouselModal, setImagesCarouselModal] = useState(false);

  useEffect(() => {
    if (props?.offer?.offer?.images) {
      props.offer.offer.images.map((file) => {
        if (file) {
          if (typeof file !== "string") {
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function () {
              setImages((prevImages) => [...prevImages, reader.result]);
            };
            reader.onerror = function (error) {
              console.log("Error: ", error);
            };
          } else {
            setImages((prevImages) => [
              ...prevImages,
              getImageUrl(file, variants.offerCard, isMobile),
            ]);
          }
        }
      });
    }
  }, [props?.offer?.offer?.images]);
  const date = formatDateLocale(new Date(offer?.offer?._created));
  const onModalClose = () => {
    setImagesCarouselModal(false);
  };
  return (
    <>
      <Details
        hasScrollBar={!props.showPublishButton}
        exchange={props.showExchangeButton}
        singleOffer={props.singleOffer}
        previewCard={props.previewCard}
      >
        {!isMobile && props.singleOffer && (
          <ScrollerVertical>
            {props?.offer?.offer?.images.map((item, index) => (
              <OfferImage
                src={
                  props.createOffer
                    ? images[index]
                    : getImageUrl(item, variants.offerCard, isMobile)
                }
                alt={t("offer.imageAlt")}
                key={item}
                previewCard={props.previewCard}
                onClick={() =>
                  !props.previewCard && setImagesCarouselModal(true)
                }
              />
            ))}
          </ScrollerVertical>
        )}
        <OfferInfoContainer
          singleOffer={props.singleOffer}
          previewCard={props.previewCard}
        >
          <OfferTitle singleOffer={props.singleOffer}>
            {offer?.offer?.name}
          </OfferTitle>
          {isMobile && (
            <ScrollerHorizontal>
              {props?.offer?.offer?.images.map((item, index) => {
                if (!item) return;
                return (
                  <OfferImage
                    src={
                      props.createOffer
                        ? images[index]
                        : getImageUrl(item, variants.offerCard, isMobile)
                    }
                    key={item}
                    previewCard={props.previewCard}
                    onClick={() =>
                      !props.previewCard && setImagesCarouselModal(true)
                    }
                  />
                );
              })}
            </ScrollerHorizontal>
          )}
          <OfferLittleDetails
            singleOffer={props.singleOffer}
            previewCard={props.previewCard}
          >
            <OfferDescriptionTitle>
              {t("itemDetailsCard.description")}
            </OfferDescriptionTitle>
            <OfferDescriptionText showBarterButton={props.showExchangeButton}>
              {offer?.offer?.description}
            </OfferDescriptionText>
            <DesciprtionPostDate previewCard={props.previewCard}>
              {date}
            </DesciprtionPostDate>
          </OfferLittleDetails>
        </OfferInfoContainer>
      </Details>
      {imagesCarouselModal && (
        <ImagesCarousel offer={props.offer} onModalClose={onModalClose} />
      )}
    </>
  );
};

OfferDetails.propTypes = {
  offer: PropTypes.any,
  showExchangeButton: PropTypes.bool,
  showPublishButton: PropTypes.bool,
  singleOffer: PropTypes.bool,
  previewCard: PropTypes.bool,
  createOffer: PropTypes.bool,
};

export default OfferDetails;
