import React, { useState } from "react";
import background from "../../assets/images/bg-sky-blue.jpg"; //shape/balance-bg.svg";
import { PriceFormatter } from "../Helpers/PriceFormatter";
import { Link } from "react-router-dom";
import { useSelector } from "react-redux";
import CreditPopup from "./Popup/CreditPopup";

export default function WalletItemCard({ walletItem, payment }) {
  //   const [eth] = useState(90);
  //   const [btc] = useState(85);
  //   const [ltc] = useState(20);

  const { userDetails } = useSelector((state) => state?.userDetails);
  let accountType = userDetails?.account_type == "FAMILY";

  //   Credit popup
  const [creditPopup, setCreditPopup] = useState({ show: false, data: {} });

  const openPopUp = (value) => {
    setCreditPopup({
      show: true,
      data: { ...value },
    });
  };

  const closePopUp = () => {
    setCreditPopup({ show: false, data: {} });
  };

  console.log("walletItem >>", walletItem, payment);

  return (
    <>
      <div
        className={`current-balance-widget w-full h-full rounded-2xl overflow-hidden flex flex-col ${
          accountType ? "justify-start" : "justify-between"
        } px-8 py-9`}
        style={{
          background: `url(${background}) 0% 0% / cover no-repeat`,
        }}
      >
        <div className="wallet flex justify-between">
          <div className="w-[100px] h-[100px] rounded-full bg-[#485199] flex justify-center items-center">
            <div>
              <p className="text-26 font-bold text-white tracking-wide text-center">
                .
              </p>
              <p className="text-lg text-white tracking-wide text-center">
                {walletItem.code}
              </p>
            </div>
          </div>
          <div></div>
        </div>
        <div className="balance">
          <p className="text-lg text-white opacity-[70%] tracking-wide mb-6">
            Current Balance
          </p>
          <p className="text-[44px] font-bold text-white tracking-wide leading-10 mb-2">
            {PriceFormatter(
              walletItem.amount * 0.01,
              walletItem.code,
              undefined,
              "text-[2rem]"
            )}
          </p>
          <p className="text-lg text-white tracking-wide">
            HOLDINGS :{" "}
            <span className="mt-1">
              {PriceFormatter(
                walletItem.escrow * 0.01,
                walletItem.code,
                undefined,
                "text-[2rem]"
              )}
            </span>
            {/*<span className="text-light-green">(11.5%)</span>*/}
          </p>
        </div>
        {!accountType && (
          <div className="counters flex space-x-16">
            <div>
              <Link
                to="transfer-fund"
                className={`${
                  walletItem.action_type != "AC_AD_FD_ONLY" && "invisible"
                } px-2 py-1 flex items-center gap-2 user-balance cursor-pointer h-[40px] rounded-full relative  bg-purple lg:text-xl text-lg font-bold text-white`}
              >
                Transfer
              </Link>
            </div>
            <div>
              <button
                // state={{ currency: walletItem.description }}
                className="px-2 py-1 flex items-center gap-2 user-balance cursor-pointer h-[40px] rounded-full bg-white lg:text-xl text-lg font-bold text-black group relative transition duration-300 stroke-black fill-white"
                onClick={() => {
                  openPopUp({
                    payment: payment,
                    currency: walletItem?.description,
                  });
                }}
              >
                <span className="">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="38"
                    height="38"
                    viewBox="0 0 42 42"
                    fill="none"
                  >
                    <path
                      d="M21 0C16.8466 0 12.7865 1.23163 9.33303 3.53914C5.8796 5.84665 3.18798 9.1264 1.59854 12.9636C0.00909901 16.8009 -0.406771 21.0233 0.403518 25.0969C1.21381 29.1705 3.21386 32.9123 6.15077 35.8492C9.08767 38.7861 12.8295 40.7862 16.9031 41.5965C20.9767 42.4068 25.1991 41.9909 29.0364 40.4015C32.8736 38.812 36.1534 36.1204 38.4609 32.667C40.7684 29.2135 42 25.1534 42 21C41.994 15.4323 39.7796 10.0944 35.8426 6.15741C31.9056 2.22045 26.5677 0.00602189 21 0ZM28 22.75H22.75V28C22.75 28.4641 22.5656 28.9092 22.2374 29.2374C21.9093 29.5656 21.4641 29.75 21 29.75C20.5359 29.75 20.0908 29.5656 19.7626 29.2374C19.4344 28.9092 19.25 28.4641 19.25 28V22.75H14C13.5359 22.75 13.0908 22.5656 12.7626 22.2374C12.4344 21.9092 12.25 21.4641 12.25 21C12.25 20.5359 12.4344 20.0907 12.7626 19.7626C13.0908 19.4344 13.5359 19.25 14 19.25H19.25V14C19.25 13.5359 19.4344 13.0908 19.7626 12.7626C20.0908 12.4344 20.5359 12.25 21 12.25C21.4641 12.25 21.9093 12.4344 22.2374 12.7626C22.5656 13.0908 22.75 13.5359 22.75 14V19.25H28C28.4641 19.25 28.9093 19.4344 29.2374 19.7626C29.5656 20.0907 29.75 20.5359 29.75 21C29.75 21.4641 29.5656 21.9092 29.2374 22.2374C28.9093 22.5656 28.4641 22.75 28 22.75Z"
                      //   fill="white"
                      className="stroke-black fill-white"
                    ></path>
                  </svg>
                </span>
                <span className="">Add Credit</span>
                <span className="absolute inset-0 w-full h-full transition-all duration-300 ease-out bg-gray-300 group-hover:bg-gray-400 opacity-0"></span>
              </button>
            </div>
          </div>
        )}
      </div>
      {creditPopup.show && (
        <CreditPopup
          details={creditPopup.data}
          walletItem={walletItem}
          onClose={closePopUp}
          situation={openPopUp}
        />
      )}
    </>
  );
}
