/* global React, Wordmark, useTweaks, BlogList, BlogPost */
const { useState, useEffect } = React;

function BlogNav() {
  return (
    <nav className="nav">
      <div className="wrap nav-in">
        <a href="/" style={{ textDecoration: "none", color: "inherit" }}>
          <Wordmark size={26} variant="gradient" color="var(--fg)" />
        </a>
        <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
          <a className="ghost" href="https://github.com/SparkIOme" target="_blank" rel="noopener">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true"><path d="M8 0C3.58 0 0 3.58 0 8a8 8 0 005.47 7.59c.4.07.55-.17.55-.38v-1.34c-2.23.48-2.7-1.07-2.7-1.07-.36-.93-.89-1.18-.89-1.18-.73-.5.05-.49.05-.49.8.06 1.23.83 1.23.83.72 1.23 1.87.87 2.33.66.07-.52.28-.87.5-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.83-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82a7.6 7.6 0 014 0c1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.52.56.83 1.28.83 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48v2.2c0 .21.15.46.55.38A8 8 0 0016 8c0-4.42-3.58-8-8-8z"/></svg>
            SparkIOme
          </a>
          {/* Back link for mobile */}
        </div>
      </div>
    </nav>
  );
}

function BlogFooter() {
  return (
    <footer>
      <div className="wrap" style={{ padding: "56px 32px", display: "flex", justifyContent: "space-between",
        alignItems: "flex-end", flexWrap: "wrap", gap: 28 }}>
        <div style={{ display: "flex", flexDirection: "column", gap: 12, maxWidth: 320 }}>
          <Wordmark size={24} variant="gradient" color="var(--fg)" />
          <span style={{ color: "var(--fg-2)", fontSize: 14.5, lineHeight: 1.5 }}>Building products, experiments and ideas.</span>
        </div>
        <div style={{ display: "flex", gap: 48, flexWrap: "wrap" }}>
          <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
            <span className="mono" style={{ fontSize: 11, color: "var(--fg-3)", textTransform: "uppercase", letterSpacing: ".1em" }}>Site</span>
            <a href="https://sparkio.me" target="_blank" rel="noopener" style={{ color: "var(--fg)", textDecoration: "none", fontSize: 14.5 }}>sparkio.me</a>
            <a href="https://github.com/SparkIOme" target="_blank" rel="noopener" style={{ color: "var(--fg)", textDecoration: "none", fontSize: 14.5 }}>GitHub · SparkIOme</a>
            <a href="https://medium.com/@diegoezce" target="_blank" rel="noopener" style={{ color: "var(--fg)", textDecoration: "none", fontSize: 14.5 }}>Medium · @diegoezce</a>
            <a href="https://www.linkedin.com/in/diego-cervera" target="_blank" rel="noopener" style={{ color: "var(--fg)", textDecoration: "none", fontSize: 14.5 }}>LinkedIn · Diego Cervera</a>
          </div>
        </div>
      </div>
      <div className="wrap" style={{ padding: "20px 32px 40px", borderTop: "1px solid var(--line)",
        display: "flex", justifyContent: "space-between", color: "var(--fg-3)", fontSize: 13, flexWrap: "wrap", gap: 8 }}>
        <span>© {new Date().getFullYear()} Sparkio</span>
        <span className="mono">build · learn · launch · repeat</span>
      </div>
    </footer>
  );
}

function BlogRouter() {
  const [view, setView] = useState('list');
  const [slug, setSlug] = useState(null);
  const [t] = useTweaks({});

  useEffect(() => {
    const hash = window.location.hash.slice(1);
    if (hash) {
      setSlug(hash);
      setView('post');
    } else {
      setView('list');
    }

    const handleHashChange = () => {
      const newHash = window.location.hash.slice(1);
      if (newHash) {
        setSlug(newHash);
        setView('post');
      } else {
        setView('list');
      }
      window.scrollTo(0, 0);
    };

    window.addEventListener('hashchange', handleHashChange);
    return () => window.removeEventListener('hashchange', handleHashChange);
  }, []);

  const handleBack = () => {
    window.location.hash = '';
    setView('list');
  };

  return (
    <div className="blog-container">
      <BlogNav />
      {view === 'list' && <BlogList />}
      {view === 'post' && slug && <BlogPost slug={slug} onBack={handleBack} />}
      <BlogFooter />
    </div>
  );
}

window.BlogRouter = BlogRouter;
