import { useEffect } from "react";
import { Helmet, HelmetProvider } from "react-helmet-async";
import { useMoralis } from "react-moralis";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import { createBrowserHistory } from "history";
import { Layout } from "antd";
import Homepage from "./pages/Homepage/Homepage";
import StoreFront from "./pages/store/StoreFront";
import HeaderComponent from "components/Header";
import ReactGA from "react-ga4";

import { useSubdomain } from "./hooks/useSubdomain";

import "antd/dist/antd.css";
import "./style.css";

ReactGA.initialize(process.env.REACT_APP_GOOGLE_TRACKING_ID);
ReactGA.send({
  hitType: "pageview",
  page: window.location.pathname + window.location.search,
});

const { Footer } = Layout;
const styles = {
  content: {
    display: "flex",
    justifyContent: "center",
    flex: "1",
    fontFamily: "Roboto, sans-serif",
    color: "#041836",
    marginTop: "130px",
    padding: "10px",
  },
  headerRight: {
    display: "flex",
    gap: "20px",
    alignItems: "center",
    fontSize: "15px",
    fontWeight: "600",
  },
};

const history = createBrowserHistory();
const App = () => {
  const { isWeb3Enabled, enableWeb3, isAuthenticated, isWeb3EnableLoading } =
    useMoralis();
  const vanitySubdomain = useSubdomain();

  useEffect(() => {
    const connectorId = window.localStorage.getItem("connectorId");
    if (isAuthenticated && !isWeb3Enabled && !isWeb3EnableLoading)
      enableWeb3({ provider: connectorId });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isAuthenticated, isWeb3Enabled]);

  return (
    <HelmetProvider>
      <Helmet>
        <title>{["Hoji Store", vanitySubdomain || "Home"].join(" - ")}</title>
      </Helmet>
      <Layout style={{ height: "100vh", overflow: "auto" }}>
        <Router history={history}>
          <HeaderComponent />
          <div style={styles.content}>
            <Switch>
              <Route path="/store">
                <StoreFront />
              </Route>
              <Route path="/">
                <Homepage />
              </Route>
            </Switch>
          </div>
        </Router>
        <Footer
          style={{
            textAlign: "center",
            backgroundColor: "#006169",
            marginTop: 70,
          }}
        ></Footer>
      </Layout>
    </HelmetProvider>
  );
};

export default App;
