import CategorySection from "@/components/home/CategorySection";
import NewlyAddedProducts from "@/components/home/NewlyAddedProducts";
import FeaturedProducts from "@/components/home/FeaturedProducts";
import RecomandedProducts from "@/components/home/RecommandedProducts";
import React from "react";
import { notification } from "antd";
import Image from "next/image";
import axios from "axios";
import { useRouter } from "next/router";
import { useDispatch, useSelector } from "react-redux";
import { RootState } from "@/Redux/store";
import { setFilteredProducts, setProducts } from "@/Redux/productFiltersSlice";
import { incrementNotificationCount } from "../Redux/notificationSlice";
import { initializeApp } from "firebase/app";
import { getMessaging, onMessage } from "firebase/messaging";
import ImageCarousel from "@/components/home/ImageCarousel";
import Link from "next/link";
import style from "@/styles/common/banner.module.css";
import Head from "next/head";
import { userActivity } from "@/components/commonFunctions/userActivity";
import ReviewCarousel from "@/components/home/Review";

const firebaseApiKey = process.env.FIREBASE_API_KEY;

const firebaseConfig = {
  apiKey: firebaseApiKey,
  authDomain: "jazzagain1.firebaseapp.com",
  projectId: "jazzagain1",
  storageBucket: "jazzagain1.appspot.com",
  messagingSenderId: "927463586136",
  appId: "1:927463586136:web:41358cf68645cbe762cd2d",
};

const app = initializeApp(firebaseConfig);

export default function Home() {
  const _loggedIn_user = useSelector((state: RootState) => state.user.user);

  const Home = () => {
    userActivity(
      _loggedIn_user?._id,
      "website",
      "Home ",
      {
        pageVisit: `/`,
      },
      navigator.userAgent
    );
  };

  const category = () => {
    userActivity(
      _loggedIn_user?._id,
      "website",
      "category  ",
      {
        pageVisit: `/`,
      },
      navigator.userAgent
    );
  };
  const Banner = () => {
    userActivity(
      _loggedIn_user?._id,
      "website",
      "Banner ",
      {
        pageVisit: `/`,
      },
      navigator.userAgent
    );
  };

  const Downloadapp = () => {
    userActivity(
      _loggedIn_user?._id,
      "website",
      "Download app   ",
      {
        pageVisit: `https://play.google.com/store/search?q=jazzagain&c=apps&hl=en`,
      },
      navigator.userAgent
    );
  };
  const Downloadios = () => {
    userActivity(
      _loggedIn_user?._id,
      "website",
      "Download ios   ",
      {
        pageVisit: `/https://apps.apple.com/in/app/jazzagain/id6476974335`,
      },
      navigator.userAgent
    );
  };

  const router = useRouter();
  const dispatch = useDispatch();
  const [data, setData] = React.useState<any[]>([]);

  React.useEffect(() => {
    const getLatestData = async () => {
      try {
        const response = await axios.get(
          "https://api.jazzagain.com/public/index.php/api/getLatestProducts"
        );
        setData(response.data.products.data);
      } catch (error) {
        console.error("Error:", error);
      }
    };
    getLatestData();
  }, []);

  React.useEffect(() => {
    const messaging = getMessaging(app);
    onMessage(messaging, (payload) => {
      console.log("Message received. ", payload);
      openNotification(
        payload?.notification?.title,
        payload?.notification?.body
      );
    });
  }, []);

  const openNotification = (title: any, body: any) => {
    notification.open({
      message: title,
      description: body,
      placement: "bottomRight",
    });
  };

  return (
    <>
      <Head>
        <script
          id='my-custom-script'
          dangerouslySetInnerHTML={{
            __html: `
        !function(f,b,e,v,n,t,s)
        {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
        n.callMethod.apply(n,arguments):n.queue.push(arguments)};
        if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
        n.queue=[];t=b.createElement(e);t.async=!0;
        t.src=v;s=b.getElementsByTagName(e)[0];
        s.parentNode.insertBefore(t,s)}(window, document,'script',
        'https://connect.facebook.net/en_US/fbevents.js');
        fbq('init', '408374148405242');
        fbq('track', 'PageView');
      `,
          }}
        />
        <noscript>
          <img
            height="1"
            width="1"
            style={{ display: "none" }}
            src="https://www.facebook.com/tr?id=408374148405242&ev=PageView&noscript=1"
            alt="facebook pixel"
          />
        </noscript>
        <title>
          Online Shop | Buy affordable Clothes | Sustainable Fashion
        </title>
        <meta
          name="description"
          content="Shop affordable second-hand clothes, vintage second-hand clothing, and gently used branded clothes. Find pre-loved clothes for sale online."
        />
        <meta
          name="keywords"
          content="Pre-owned clothes online, Buy second-hand clothes, Resale store online, Affordable used clothing, Sustainable fashion, Online Resale shop, Vintage second-hand clothing, Pre-loved clothes for sale, Second-hand branded clothes, Best places to buy used clothes online, Affordable second-hand designer clothes, Where to find vintage clothing online, Second-hand clothes in Nigeria, Pre-owned clothing stores near me"
        />
        <link rel="canonical" href="https://jazzagain.com" />
        <meta name="robots" content="index, follow" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta property="og:title" content="Online Resale Shop | JazzAgain" />
        <meta
          property="og:description"
          content="Shop affordable second-hand clothes, vintage second-hand clothing, and gently used branded clothes. Find sustainable fashion and pre-loved clothes for sale online."
        />
        <meta property="og:image" content="/images/jazzagian-logo.png" />
        <meta property="og:url" content="https://jazzagain.com" />
        <meta property="og:type" content="website" />
        <meta name="twitter:card" content="summary_large_image" />
        <meta name="twitter:title" content="Online Resale Shop | JazzAgain" />
        <meta
          name="twitter:description"
          content="Shop affordable second-hand clothes, vintage second-hand clothing, and gently used branded clothes. Find sustainable fashion and pre-loved clothes for sale online."
        />
        <meta name="twitter:image" content="/images/jazzagian-logo.png" />
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{
            __html: `
              {
                "@context": "https://schema.org",
                "@type": "OnlineStore",
                "name": "JazzAgain",
                "description": "Shop affordable second-hand clothes, vintage second-hand clothing, and gently used branded clothes online.",
                "url": "https://jazzagain.com",
                "logo": "https://jazzagain.com/images/jazzagian-logo.png",
                "sameAs": [
                  "https://instagram.com/jazzagain_ng",
                  "https://facebook.com/people/JazzAgain/61550762826091"
                ]
              }
            `,
          }}
        />
      </Head>

      <header>
        <h1 className="hidden">
          Jazz Again | Online Resale Shop | Buy Pre-owned Clothes | Sustainable
          Fashion
        </h1>

        <div className="bg-[#fff]">
          <div onClick={category} className=" flex ">
            <CategorySection />
            <Image
              src="/images/CustomerProtection.png"
              className="mr-[7rem] my-[1rem] hidden  lg:block"
              alt=""
              width={100}
              height={100}
            />
          </div>
          <div onClick={Banner}>
            <ImageCarousel />
          </div>
        </div>
      </header>
      {/*  */}

      <main>
        <section id="viewall-products">
          <div className="bg-[#fff] flex w-full justify-center items-center">
            <Link href={"/product/viewall"}>
              <button className="relative flex items-center px-[6rem] my-[1rem] py-3 overflow-hidden  transition-all bg-[#383736] rounded-md group">
                <span className="absolute top-0 right-0 inline-block w-4 h-4 transition-all duration-500 ease-in-out bg-[#FE342B]  rounded group-hover:-mr-4 group-hover:-mt-4">
                  <span className="absolute top-0 right-0 w-5 h-5 rotate-45 translate-x-1/2 -translate-y-1/2 bg-white"></span>
                </span>
                <span className="absolute bottom-0 rotate-180 left-0 inline-block w-4 h-4 transition-all duration-500 ease-in-out bg-[#FE342B] rounded group-hover:-ml-4 group-hover:-mb-4">
                  <span className="absolute top-0 right-0 w-5 h-5 rotate-45 translate-x-1/2 -translate-y-1/2 bg-white"></span>
                </span>
                <span className="absolute bottom-0 left-0 w-full h-full transition-all duration-500 ease-in-out delay-200 -translate-x-full bg-[#50504c] rounded-md group-hover:translate-x-0"></span>
                <span className="relative w-full text-left  transition-colors duration-200 ease-in-out group-hover:text-white hover:underline text-[#fff] text-xl font-semibold tracking-wide">
                  View All Products
                </span>
              </button>
            </Link>
          </div>
        </section>

        <hr className="bg-[#fff] border-y-4 border-white" />
        <section id="featured-products">
          <div className="bg-[#fff]">
            <div className="w-[97%] sm:w-[85%] mx-auto pt-[.2rem]">
              <FeaturedProducts />
            </div>
          </div>
        </section>

        <hr className="bg-[#fff] border-y-4 border-white" />

        <section id="newly-added-products">
          <div className="mx-auto py-[1.5rem] w-full bg-[#383736] mt-[1rem]">
            <NewlyAddedProducts />
          </div>
        </section>
        <hr className="bg-[#fff] border-y-4  border-white" />

        <section id="recomanded-products">
          <div className="w-[97%] sm:w-[85%] mx-auto mt-[3rem]">
            <RecomandedProducts />
          </div>
        </section>
        <section id="customer-review">
          <ReviewCarousel />
        </section>
      </main>

      <footer>
        <div className="mt-[1rem] relative">
          <Image
            src={"/images/downloadStoreBanner.jpg"}
            height={300}
            width={300}
            className="w-full cursor-pointer hidden lg:block"
            alt="hero banner image"
          />
          <Image
            src={"/images/downloadStoreBannerSM.jpg"}
            height={300}
            width={300}
            className="w-full cursor-pointer lg:hidden"
            alt="hero banner image"
          />

          <div className="flex gap-2 absolute right-[17%] bottom-[10%] ">
            <Link
              href={
                "https://play.google.com/store/search?q=jazzagain&c=apps&hl=en"
              }
              onClick={Downloadapp}
            >
              <button className={style.appstorebutton}>
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  fill="currentColor"
                  viewBox="0 0 512 512"
                >
                  <path d="M99.617 8.057a50.191 50.191 0 00-38.815-6.713l230.932 230.933 74.846-74.846L99.617 8.057zM32.139 20.116c-6.441 8.563-10.148 19.077-10.148 30.199v411.358c0 11.123 3.708 21.636 10.148 30.199l235.877-235.877L32.139 20.116zM464.261 212.087l-67.266-37.637-81.544 81.544 81.548 81.548 67.273-37.64c16.117-9.03 25.738-25.442 25.738-43.908s-9.621-34.877-25.749-43.907zM291.733 279.711L60.815 510.629c3.786.891 7.639 1.371 11.492 1.371a50.275 50.275 0 0027.31-8.07l266.965-149.372-74.849-74.847z"></path>
                </svg>
                <span className={style.buttontext}>
                  <span className={style.toptext}>Download on the</span>
                  <span className={style.bottomtext}> Google Play</span>
                </span>
              </button>
            </Link>
            <Link
              href={"https://apps.apple.com/in/app/jazzagain/id6476974335"}
              onClick={Downloadios}
            >
              <button className={style.appstorebutton}>
                <svg viewBox="0 0 24 24">
                  <g id="SVGRepo_bgCarrier" strokeWidth="0"></g>
                  <g
                    id="SVGRepo_tracerCarrier"
                    strokeLinecap="round"
                    strokeLinejoin="round"
                  ></g>
                  <g id="SVGRepo_iconCarrier">
                    <path d="M18.71 19.5C17.88 20.74 17 21.95 15.66 21.97C14.32 22 13.89 21.18 12.37 21.18C10.84 21.18 10.37 21.95 9.09997 22C7.78997 22.05 6.79997 20.68 5.95997 19.47C4.24997 17 2.93997 12.45 4.69997 9.39C5.56997 7.87 7.12997 6.91 8.81997 6.88C10.1 6.86 11.32 7.75 12.11 7.75C12.89 7.75 14.37 6.68 15.92 6.84C16.57 6.87 18.39 7.1 19.56 8.82C19.47 8.88 17.39 10.1 17.41 12.63C17.44 15.65 20.06 16.66 20.09 16.67C20.06 16.74 19.67 18.11 18.71 19.5ZM13 3.5C13.73 2.67 14.94 2.04 15.94 2C16.07 3.17 15.6 4.35 14.9 5.19C14.21 6.04 13.07 6.7 11.95 6.61C11.8 5.46 12.36 4.26 13 3.5Z"></path>
                  </g>
                </svg>
                <span className={style.buttontext}>
                  <span className={style.toptext}>Download on the</span>
                  <span className={style.bottomtext}>App Store</span>
                </span>
              </button>
            </Link>
          </div>
        </div>
      </footer>
    </>
  );
}
