/* SiliconPop — Shared UI Components */
/* Exports: BottomNav, TopBar, SPCard, SPBadge, SPButton, SPAvatar, SPSearch, HScroll, GradientText, PageShell, SPTabs */

const spUI = {};

/* ── Bottom Navigation ── */
spUI.BottomNav = function BottomNav({ active, onNav }) {
  const tabs = [
    { id: 'home', label: '首页', icon: 'M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-4 0a1 1 0 01-1-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 01-1 1' },
    { id: 'market', label: '市场', icon: 'M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10' },
    { id: 'create', label: '创作', icon: 'M12 4v16m8-8H4', isCenter: true },
    { id: 'community', label: '社区', icon: 'M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z' },
    { id: 'profile', label: '我的', icon: 'M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z' },
  ];
  return (
    <nav className="sp-bottom-nav">
      {tabs.map(t => (
        <button key={t.id} className={`sp-nav-item ${active === t.id ? 'active' : ''} ${t.isCenter ? 'center' : ''}`} onClick={() => onNav(t.id)}>
          {t.isCenter ? (
            <span className="sp-nav-center-btn">
              <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round"><path d={t.icon} /></svg>
            </span>
          ) : (
            <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d={t.icon} /></svg>
          )}
          <span className="sp-nav-label">{t.label}</span>
        </button>
      ))}
    </nav>
  );
};

/* ── Desktop Sidebar ── */
spUI.DesktopSidebar = function DesktopSidebar({ active, onNav, user }) {
  const tabs = [
    { id: 'home', label: '首页', icon: 'M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-4 0a1 1 0 01-1-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 01-1 1' },
    { id: 'market', label: '市场', icon: 'M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10' },
    { id: 'create', label: '创作', icon: 'M12 4v16m8-8H4' },
    { id: 'community', label: '社区', icon: 'M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z' },
    { id: 'profile', label: '我的', icon: 'M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z' },
  ];
  return (
    <aside className="sp-sidebar">
      <div className="sp-sidebar-logo">
        <div className="sp-logo-mark">SP</div>
        <div className="sp-logo-text">
          <strong>SiliconPop</strong>
          <span>硅基潮流</span>
        </div>
      </div>
      <div className="sp-sidebar-nav">
        {tabs.map(t => (
          <button key={t.id} className={`sp-sidebar-item ${active === t.id ? 'active' : ''}`} onClick={() => onNav(t.id)}>
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d={t.icon} /></svg>
            <span>{t.label}</span>
          </button>
        ))}
      </div>
      <div className="sp-sidebar-footer">
        <div className="sp-sidebar-user">
          <div className="sp-avatar-sm" style={{background:'linear-gradient(135deg,#6366F1,#EC4899)'}}>{(user?.name || 'N')[0]}</div>
          <div>
            <div style={{fontWeight:600,fontSize:13}}>{user?.name || '游客'}</div>
            <div style={{fontSize:11,opacity:.5}}>{user?.email || '未登录'}</div>
          </div>
        </div>
      </div>
    </aside>
  );
};

/* ── Top Bar (mobile) ── */
spUI.TopBar = function TopBar({ title, subtitle, showLogo, rightAction }) {
  return (
    <header className="sp-topbar">
      {showLogo ? (
        <div className="sp-topbar-brand">
          <div className="sp-logo-mark-sm">SP</div>
          <div className="sp-topbar-title">
            <strong>SiliconPop</strong>
            {subtitle && <span>{subtitle}</span>}
          </div>
        </div>
      ) : (
        <h1 className="sp-topbar-heading">{title}</h1>
      )}
      {rightAction && <div className="sp-topbar-actions">{rightAction}</div>}
    </header>
  );
};

/* ── Gradient Text ── */
spUI.GradientText = function GradientText({ children, className, style }) {
  return <span className={`sp-gradient-text ${className || ''}`} style={style}>{children}</span>;
};

/* ── Card ── */
spUI.SPCard = function SPCard({ children, className, style, onClick, glow }) {
  return (
    <div className={`sp-card ${glow ? 'sp-card-glow' : ''} ${className || ''}`} style={style} onClick={onClick}>
      {children}
    </div>
  );
};

/* ── Badge ── */
spUI.SPBadge = function SPBadge({ children, variant, style }) {
  return <span className={`sp-badge ${variant || ''}`} style={style}>{children}</span>;
};

/* ── Button ── */
spUI.SPButton = function SPButton({ children, variant, onClick, style, className }) {
  return (
    <button className={`sp-btn ${variant || ''} ${className || ''}`} onClick={onClick} style={style}>{children}</button>
  );
};

/* ── Search ── */
spUI.SPSearch = function SPSearch({ placeholder }) {
  return (
    <div className="sp-search">
      <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><circle cx="11" cy="11" r="8" /><path d="M21 21l-4.35-4.35" /></svg>
      <input type="text" placeholder={placeholder || '搜索 IP、Agent、内容...'} />
    </div>
  );
};

/* ── Horizontal Scroll ── */
spUI.HScroll = function HScroll({ children, className }) {
  return <div className={`sp-hscroll ${className || ''}`}>{children}</div>;
};

/* ── Section Header ── */
spUI.SectionHead = function SectionHead({ title, action, actionLabel }) {
  return (
    <div className="sp-section-head">
      <h2>{title}</h2>
      {actionLabel && <button className="sp-section-action" onClick={action}>{actionLabel}</button>}
    </div>
  );
};

/* ── Tabs ── */
spUI.SPTabs = function SPTabs({ tabs, active, onChange }) {
  return (
    <div className="sp-tabs">
      {tabs.map(t => (
        <button key={t.id} className={`sp-tab ${active === t.id ? 'active' : ''}`} onClick={() => onChange(t.id)}>
          {t.label}
        </button>
      ))}
    </div>
  );
};

/* ── Stat Card ── */
spUI.StatCard = function StatCard({ label, value, accent }) {
  return (
    <div className="sp-stat-card">
      <span className="sp-stat-label">{label}</span>
      <strong className="sp-stat-value" style={accent ? {color: accent} : {}}>{value}</strong>
    </div>
  );
};

/* ── Page Shell ── */
spUI.PageShell = function PageShell({ children, className }) {
  return <div className={`sp-page ${className || ''}`}>{children}</div>;
};

/* ── IP Card (reusable) ── */
spUI.IPCard = function IPCard({ name, sub, tags, price, gradient, size }) {
  const g = gradient || 'linear-gradient(135deg,#6366F1 0%,#00D4FF 100%)';
  const isCompact = size === 'sm';
  return (
    <div className={`sp-ip-card ${isCompact ? 'compact' : ''}`}>
      <div className="sp-ip-thumb" style={{background: g}}>
        <div className="sp-ip-avatar">{name?.[0]}</div>
      </div>
      <div className="sp-ip-info">
        <strong>{name}</strong>
        <span className="sp-ip-sub">{sub}</span>
        {tags && <div className="sp-ip-tags">{tags.map((t,i) => <span key={i} className="sp-mini-tag">{t}</span>)}</div>}
      </div>
      {price && <div className="sp-ip-price">{price}</div>}
    </div>
  );
};

/* ── Agent Card (reusable) ── */
spUI.AgentCard = function AgentCard({ name, desc, capability, price, gradient }) {
  const g = gradient || 'linear-gradient(135deg,#EC4899,#F59E0B)';
  return (
    <div className="sp-agent-card">
      <div className="sp-agent-icon" style={{background: g}}>
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#fff" strokeWidth="2" strokeLinecap="round"><path d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" /></svg>
      </div>
      <div className="sp-agent-body">
        <strong>{name}</strong>
        <p>{desc}</p>
        {capability && <div className="sp-mini-tag" style={{marginTop:6}}>{capability}</div>}
      </div>
      {price && <div className="sp-agent-price">{price}</div>}
    </div>
  );
};

/* ── Empty State ── */
spUI.EmptyState = function EmptyState({ icon, title, desc }) {
  return (
    <div className="sp-empty">
      <div className="sp-empty-icon">{icon || '✦'}</div>
      <strong>{title}</strong>
      <p>{desc}</p>
    </div>
  );
};

Object.assign(window, spUI);
