import React, { useCallback, useEffect, useRef, useState } from "react";

import { useDispatch, useSelector } from "react-redux";

import { Job } from "@enact/core/util";
import Spotlight from "@enact/spotlight";

import {
  getCheckoutTotalAmt,
  resetCheckoutData,
} from "../../actions/checkoutActions";
import { setHidePopup, setShowPopup } from "../../actions/commonActions";
import { getShoptimeTerms } from "../../actions/empActions";
import {
  sendLogCheckOutBtnClick,
  sendLogGNB,
  sendLogMyInfoEdit,
  sendLogPaymentEntry,
  sendLogTotalRecommend,
} from "../../actions/logActions";
import { popPanel } from "../../actions/panelActions";
import TBody from "../../components/TBody/TBody";
import TButton from "../../components/TButton/TButton";
import TButtonScroller from "../../components/TButtonScroller/TButtonScroller";
import TButtonTab from "../../components/TButtonTab/TButtonTab";
import TFullPopup from "../../components/TFullPopup/TFullPopup";
import THeader from "../../components/THeader/THeader";
import TPanel from "../../components/TPanel/TPanel";
import TPopUp from "../../components/TPopUp/TPopUp";
import TQRCode from "../../components/TQRCode/TQRCode";
import useScrollTo from "../../hooks/useScrollTo";
import * as Config from "../../utils/Config";
import { $L, scaleH, scaleW } from "../../utils/helperMethods";
import { SpotlightIds } from "../../utils/SpotlightIds";
import css from "./CheckOutPanel.module.less";
import PinCodeInput from "./components/PinCodeInput";
import FixedSideBar from "./container/FixedSideBar";
import InformationContainer from "./container/InformationContainer";
import OrderItemsSideBar from "./container/OrderItemsSideBar";
import SummaryContainer from "./container/SummaryCotainer";

export default function CheckOutPanel({ panelInfo }) {
  const dispatch = useDispatch();
  const panels = useSelector((state) => state.panels.panels);
  const { userNumber } = useSelector(
    (state) => state.common.appStatus.loginUserData
  );
  const productData = useSelector(
    (state) => state.checkout?.checkoutData.productList
  );
  const infoForCheckoutData = useSelector(
    (state) => state.checkout?.infoForCheckoutData
  );
  const selectedCoupons = useSelector(
    (state) => state.checkout.selectedCoupons
  );
  const empTermsData = useSelector((state) => state.emp.empTermsData.terms);
  const { popupVisible, activePopup } = useSelector(
    (state) => state.common.popup
  );
  const popup = useSelector((state) => state.common.popup);
  const { currSign, currSignLoc } = productData?.[0] || {};

  const [orderSideBarOpen, setOrderSideBarOpen] = useState(false);
  const [offerSideBarOpen, setOfferSideBarOpen] = useState(false);
  const [placeOrderPopup, setPlaceOrderPopup] = useState(false);
  const [currentTerms, setCurrentTerms] = useState(null);
  const [tabList, setTabList] = useState([]);
  const [selectedTabIndex, setSelectedTabIndex] = useState(0);
  const [resetScroll, setResetScroll] = useState(false);
  const [spotlightDisalbed, setSpotlightDisabled] = useState(false);
  const [isOrderSuccessful, setIsOrderSuccessful] = useState(false);

  const isMounted = useRef(true);

  const { getScrollTo: getScrollToBody, scrollTop: scrollTopBody } =
    useScrollTo();

  const spotJob = useRef(new Job((func) => func(), 0));

  useEffect(() => {
    let nowMenu;

    if (isOrderSuccessful) {
      return;
    }
    //
    else if (!placeOrderPopup || popupVisible) {
      nowMenu = Config.LOG_MENU.CHECKOUT;
    }
    //
    else if (placeOrderPopup) {
      nowMenu = Config.LOG_MENU.CHECKOUT_PIN_CODE;
    }

    dispatch(sendLogGNB(nowMenu));
  }, [isOrderSuccessful, placeOrderPopup, popupVisible]);

  useEffect(() => {
    isMounted.current = true;

    return () => {
      isMounted.current = false;
    };
  }, []);

  useEffect(() => {
    dispatch(getShoptimeTerms());
  }, [dispatch]);

  function totalAmtValidate(response) {
    if (response) {
      if (response.retCode !== 0) {
        if (response.retMsg) {
          dispatch(
            setShowPopup(Config.ACTIVE_POPUP.errorPopup, {
              text: response.retMsg,
            })
          );
        }
      }
    }
  }

  useEffect(() => {
    if (infoForCheckoutData && productData) {
      const orderProductCoupontUse = Object.keys(selectedCoupons).map(
        (productId) => {
          const { cpnCdSeq, cpnSno, prdtId } = selectedCoupons[productId];
          const product = productData.find((prod) => prod.prdtId === prdtId);

          return {
            cpnCdSeq,
            cpnSno,
            prdtId,
            prodSno: product?.prodSno,
            patnrId: product?.patnrId,
          };
        }
      );

      if (
        infoForCheckoutData &&
        infoForCheckoutData.dlvrAddrSno &&
        infoForCheckoutData.bilAddrSno
      ) {
        dispatch(
          getCheckoutTotalAmt(
            {
              mbrNo: userNumber,
              dirPurcSelYn: "Y",
              bilAddrSno: infoForCheckoutData.bilAddrSno,
              dlvrAddrSno: infoForCheckoutData.dlvrAddrSno,
              orderProductCoupontUse,
            },
            totalAmtValidate
          )
        );
      }
    }
  }, [dispatch, infoForCheckoutData, productData, userNumber, selectedCoupons]);

  useEffect(() => {
    const newTabList = [];

    if (empTermsData) {
      empTermsData.forEach((term) => {
        newTabList.push(term.termsTypeName);
      });

      setTabList(newTabList);
    }
  }, [empTermsData]);

  useEffect(() => {
    return () => {
      dispatch(resetCheckoutData());
    };
  }, [dispatch]);

  useEffect(() => {
    const spotJobValue = spotJob.current;

    spotJobValue.start(() => {
      if (!activePopup) {
        Spotlight.focus("spotlightId_placeOrderBtn");
      } else if (activePopup === Config.ACTIVE_POPUP.errorPopup) {
        Spotlight.focus(SpotlightIds.TPOPUP);
      }
    });

    return () => {
      spotJobValue.stop();
    };
  }, [activePopup]);

  const onBackClick = useCallback(() => {
    dispatch(popPanel());
  }, [dispatch]);

  const toggleOrderSideBar = useCallback(() => {
    if (!orderSideBarOpen) {
      dispatch(sendLogCheckOutBtnClick({ btnNm: "ORDER ITEMS" }));
      dispatch(
        sendLogTotalRecommend({
          buttonTitle: "ORDER ITEMS",
          buttonId: "checkout_order_items_view",
          contextName: Config.LOG_CONTEXT_NAME.CHECKOUT,
          messageId: Config.LOG_MESSAGE_ID.BUTTONCLICK,
        })
      );
      console.log("oder items");
    }

    setOrderSideBarOpen((prev) => !prev);
    setTimeout(() => Spotlight.focus(), 0);
  }, [orderSideBarOpen]);

  const toggleOfferSideBar = useCallback(() => {
    if (!offerSideBarOpen) {
      dispatch(sendLogCheckOutBtnClick({ btnNm: "OFFERS & PROMOTION" }));
      dispatch(
        sendLogTotalRecommend({
          buttonTitle: "OFFERS & PROMOTION",
          buttonId: "checkout_offers_promotion_view",
          contextName: Config.LOG_CONTEXT_NAME.CHECKOUT,
          messageId: Config.LOG_MESSAGE_ID.BUTTONCLICK,
        })
      );
    }

    setOfferSideBarOpen((prev) => !prev);
    setTimeout(() => Spotlight.focus(), 0);
  }, [offerSideBarOpen]);

  const onClosePopup = useCallback(() => {
    setPlaceOrderPopup(false);
    setTimeout(() => Spotlight.focus(), 0);
  }, []);

  const onCloseTermsPopup = useCallback(() => {
    dispatch(setHidePopup());
  }, [dispatch]);

  const handlePopPanel = useCallback(() => {
    dispatch(setHidePopup());
    dispatch(popPanel());
  }, [dispatch]);

  const handleTermsClick = useCallback(
    (termsID) => {
      if (empTermsData) {
        const selectedTerms = empTermsData.find(
          (term) => term.termsID === termsID
        );

        dispatch(
          sendLogTotalRecommend({
            buttonTitle: selectedTerms.termsTypeName,
            buttonId: `checkout_terms_${selectedTerms.termsTypeName
              .toLowerCase()
              .replace(/\s+/g, "_")}`,
            contextName: Config.LOG_CONTEXT_NAME.CHECKOUT,
            messageId: Config.LOG_MESSAGE_ID.BUTTONCLICK,
          })
        );
        setCurrentTerms(selectedTerms);
        dispatch(setShowPopup(Config.ACTIVE_POPUP.termsPopup));
        Spotlight.focus("spotlightId_TbuttonScrollerDown");
        const selectedIndex = empTermsData.findIndex(
          (term) => term.termsID === termsID
        );

        setSelectedTabIndex(selectedIndex);
        setResetScroll(true);
      }
    },
    [empTermsData, dispatch]
  );

  const handleTabClick = useCallback(({ index }) => {
    setSelectedTabIndex(index);
    setResetScroll(true);
  }, []);

  useEffect(() => {
    if (empTermsData && empTermsData[selectedTabIndex]) {
      setCurrentTerms(empTermsData[selectedTabIndex]);
      setResetScroll(true);
    }
  }, [selectedTabIndex, empTermsData]);

  useEffect(() => {
    if (resetScroll) {
      setResetScroll(false);
    }
  }, [resetScroll]);

  const onCancelCheckoutPanel = useCallback(
    (e) => {
      if (orderSideBarOpen) {
        setOrderSideBarOpen(false);
        setTimeout(() => Spotlight.focus(), 0);

        return;
      }

      if (offerSideBarOpen) {
        setOfferSideBarOpen(false);
        setTimeout(() => Spotlight.focus(), 0);

        return;
      }

      if (!orderSideBarOpen && !offerSideBarOpen) {
        dispatch(popPanel());
        e.stopPropagation();
      }
    },
    [orderSideBarOpen, offerSideBarOpen, dispatch]
  );

  const doSendLogPaymentEntry = useCallback(() => {
    if (panelInfo?.logInfo) {
      dispatch(sendLogPaymentEntry(panelInfo.logInfo));
    }
  }, [panelInfo?.logInfo]);

  const doSendLogMyInfoEdit = useCallback((btnNm) => {
    dispatch(sendLogMyInfoEdit({ btnNm }));
  }, []);

  return (
    <>
      <TPanel
        isTabActivated={false}
        spotlightDisabled={
          orderSideBarOpen || offerSideBarOpen || placeOrderPopup
        }
        handleCancel={onCancelCheckoutPanel}
      >
        <TBody className={css.tbody} cbScrollTo={getScrollToBody}>
          <THeader
            className={css.theader}
            title="CHECKOUT"
            onBackButton
            onClick={onBackClick}
          />
          <div className={css.Wrap}>
            <SummaryContainer
              setPlaceOrderPopup={setPlaceOrderPopup}
              empTermsData={empTermsData}
              handleTermsClick={handleTermsClick}
              currSign={currSign}
              currSignLoc={currSignLoc}
              doSendLogPaymentEntry={doSendLogPaymentEntry}
            />
            <InformationContainer
              toggleOrderSideBar={toggleOrderSideBar}
              toggleOfferSideBar={toggleOfferSideBar}
              scrollTopBody={scrollTopBody}
              doSendLogMyInfoEdit={doSendLogMyInfoEdit}
            />
          </div>
        </TBody>
      </TPanel>

      {orderSideBarOpen && (
        <OrderItemsSideBar closeSideBar={toggleOrderSideBar} />
      )}
      {offerSideBarOpen && (
        <FixedSideBar
          closeSideBar={toggleOfferSideBar}
          offerSideBarOpen={offerSideBarOpen}
        />
      )}

      {activePopup === Config.ACTIVE_POPUP.termsPopup && (
        <TPopUp
          kind="introTermsPopup"
          open={popupVisible}
          onClose={onCloseTermsPopup}
          hasButton
          button1Text={$L("OK")}
        >
          {currentTerms && (
            <div className={css.termsConts}>
              <TButtonTab
                className={css.tab}
                selectedIndex={selectedTabIndex}
                onItemClick={handleTabClick}
                contents={tabList}
                spotlightDisabled={true}
                role="button"
              />
              <TButtonScroller
                boxHeight={scaleH(300)}
                width={scaleW(980)}
                className={css.termsDescription}
                resetScrollPosition={resetScroll}
                forcedFocus={false}
              >
                <div
                  className={css.termsDesc}
                  dangerouslySetInnerHTML={{
                    __html: currentTerms && currentTerms.termContent,
                  }}
                ></div>
              </TButtonScroller>
            </div>
          )}
        </TPopUp>
      )}

      {/* 
      {activePopup === Config.ACTIVE_POPUP.errorPopup && (
        <TPopUp
          kind="errorPopup"
          hasText
          open={popupVisible}
          text={popup.text ? popup.text : "unknown error"}
          hasButton
          button1Text={$L("CLOSE")}
          onClick={handlePopPanel}
        />
      )} */}

      <TFullPopup
        open={placeOrderPopup}
        className={css.pinCodePopup}
        onClose={onClosePopup}
        noAnimation={true}
        spotlightId="pinCodeFullPopup"
      >
        <PinCodeInput
          setPlaceOrderPopup={setPlaceOrderPopup}
          setIsOrderSuccessful={setIsOrderSuccessful}
        />
      </TFullPopup>
    </>
  );
}
