"use client";
import { API_RouteNotifikasi } from "@/app/lib/api_user_router/route_api_notifikasi";
import { gs_count_ntf, gs_user_ntf } from "@/app/lib/global_state";
import { RouterProfile } from "@/app/lib/router_hipmi/router_katalog";
import { RouterNotifikasi } from "@/app/lib/router_hipmi/router_notifikasi";
import { RouterUserSearch } from "@/app/lib/router_hipmi/router_user_search";
import { ActionIcon, Indicator, Text } from "@mantine/core";
import { useShallowEffect } from "@mantine/hooks";
import { IconBell, IconUserSearch } from "@tabler/icons-react";
import { useAtom } from "jotai";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { MainColor } from "../_global/color";
import UIGlobal_LayoutHeaderTamplate from "../_global/ui/ui_header_tamplate";
import UIGlobal_LayoutTamplate from "../_global/ui/ui_layout_tamplate";
import { gs_notifikasi_kategori_app } from "../notifikasi/lib";
import BodyHome from "./component/body_home";
import FooterHome from "./component/footer_home";
import { apiGetDataHome } from "./fun/get/api_home";
import { clientLogger } from "@/util/clientLogger";
import CustomSkeleton from "../components/CustomSkeleton";

export default function HomeViewNew() {
  const [countNtf, setCountNtf] = useAtom(gs_count_ntf);
  const [newUserNtf, setNewUserNtf] = useAtom(gs_user_ntf);
  const [dataUser, setDataUser] = useState<any | null>(null);
  const [categoryPage, setCategoryPage] = useAtom(gs_notifikasi_kategori_app);
  const router = useRouter();

  useShallowEffect(() => {
    onLoadNotifikasi();
  }, []);

  useShallowEffect(() => {
    if (countNtf != null) {
      setCountNtf(countNtf + newUserNtf);
      setNewUserNtf(0);
    }
  }, [newUserNtf, countNtf]);

  async function onLoadNotifikasi() {
    const loadNotif = await fetch(API_RouteNotifikasi.get_count_by_id());
    const data = await loadNotif.json().then((res) => res.data);
    setCountNtf(data);
  }

  useShallowEffect(() => {
    cekUserLogin();
  }, []);

  async function cekUserLogin() {
    try {
      const response = await apiGetDataHome({
        path: "?cat=cek_profile",
      });
      if (response) {
        setDataUser(response.data);
      }
    } catch (error) {
      clientLogger.error("Error get data home", error);
    }
  }

  return (
    <>
      <UIGlobal_LayoutTamplate
        header={
          <UIGlobal_LayoutHeaderTamplate
            title="HIPMI"
            customButtonLeft={
              dataUser == null ? (
                <CustomSkeleton width={20} height={20} circle />
              ) : (
                <ActionIcon
                  radius={"xl"}
                  disabled={countNtf == null}
                  variant={"transparent"}
                  onClick={() => {
                    if (
                      dataUser.profile != undefined ||
                      dataUser?.profile != null
                    ) {
                      router.push(RouterUserSearch.main, { scroll: false });
                    } else {
                      router.push(RouterProfile.create, { scroll: false });
                    }
                  }}
                >
                  <IconUserSearch color={MainColor.white} />
                </ActionIcon>
              )
            }
            customButtonRight={
              dataUser == null ? (
                <CustomSkeleton width={20} height={20} circle />
              ) : (
                <ActionIcon
                  variant="transparent"
                  disabled={countNtf == null}
                  onClick={() => {
                    if (
                      dataUser.profile != undefined ||
                      dataUser?.profile != null
                    ) {
                      setCategoryPage("Semua");
                      router.push(
                        RouterNotifikasi.categoryApp({ name: "semua" }),
                        {
                          scroll: false,
                        }
                      );
                    } else {
                      router.push(RouterProfile.create, { scroll: false });
                    }
                  }}
                >
                  {countNtf != null && countNtf > 0 ? (
                    <Indicator
                      processing
                      color={MainColor.yellow}
                      label={
                        <Text fz={10} c={MainColor.darkblue}>
                          {countNtf > 99 ? "99+" : countNtf}
                        </Text>
                      }
                    >
                      <IconBell color={MainColor.white} />
                    </Indicator>
                  ) : (
                    <IconBell color={MainColor.white} />
                  )}
                </ActionIcon>
              )
            }
          />
        }
        footer={<FooterHome />}
      >
        <BodyHome />
      </UIGlobal_LayoutTamplate>
    </>
  );
}
