import { define } from '../../src'
import { ButtonExamplesContent } from '../button'
import { ProfileExamplesContent } from '../profile'
import { NavigationExamplesContent } from '../navigation'
import { FormExamplesContent } from '../form'

export const Main = define('SpaMain', {
  base: 'div',

  minHeight: '100%',
  padding: '40px 20px',
  fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif",
  background: '#f3f4f6',
})

export const Container = define('SpaContainer', {
  base: 'div',

  maxWidth: 1200,
  margin: '0 auto'
})

// Simple client-side router
const Link = define('Link', {
  base: 'a',

  color: '#3b82f6',
  textDecoration: 'none',
  fontWeight: 500,

  states: {
    hover: {
      textDecoration: 'underline'
    }
  },

  selectors: {
    '&[aria-current]': {
      color: '#1e40af',
      fontWeight: 600,
      textDecoration: 'underline'
    }
  },

  render({ props, parts: { Root } }) {
    const handleClick = (e: Event) => {
      e.preventDefault()
      window.history.pushState({}, '', props.href)
      window.dispatchEvent(new Event('routechange'))
    }

    return (
      <Root {...props} onclick={handleClick}>
        {props.children}
      </Root>
    )
  }
})

const Nav = define('Nav', {
  base: 'nav',

  display: 'flex',
  gap: 20,
  marginBottom: 40,
  padding: 20,
  background: 'white',
  borderRadius: 8,
  boxShadow: '0 1px 3px rgba(0,0,0,0.1)'
})

const P = define('P', {
  color: '#6b7280',
  fontSize: 18,
  marginBottom: 48,
})

const ExamplesGrid = define('ExamplesGrid', {
  display: 'grid',
  gap: 20,
  gridTemplateColumns: 'repeat(auto-fill, minmax(300px, 1fr))'
})

const ExampleCard = define('ExampleCard', {
  base: 'a',

  background: 'white',
  padding: 24,
  borderRadius: 12,
  boxShadow: '0 1px 3px rgba(0,0,0,0.1)',
  textDecoration: 'none',
  transition: 'all 0.2s ease',
  display: 'block',

  states: {
    hover: {
      transform: 'translateY(-2px)',
      boxShadow: '0 4px 12px rgba(0,0,0,0.15)'
    }
  },

  parts: {
    H2: {
      color: '#111827',
      margin: '0 0 8px 0',
      fontSize: 20,
    },
    P: {
      color: '#6b7280',
      margin: 0,
      fontSize: 14,
    }
  },

  render({ props: { title, desc, ...props }, parts: { Root, H2, P } }) {
    const handleClick = (e: Event) => {
      e.preventDefault()
      window.history.pushState({}, '', props.href)
      window.dispatchEvent(new Event('routechange'))
    }

    return (
      <Root {...props} onclick={handleClick}>
        <H2>{title}</H2>
        <P>{desc}</P>
      </Root>
    )
  }
})

const HomePage = () => (
  <>
    <P>Explore component examples built with Forge - Client-side SPA version</P>

    <ExamplesGrid>
      <ExampleCard href="/spa/profile"
        title="Profile Card"
        desc="User profile component with variants for size, theme, and verified status"
      />

      <ExampleCard href="/spa/buttons"
        title="Buttons"
        desc="Button component with intent, size, and disabled variants"
      />

      <ExampleCard href="/spa/navigation"
        title="Navigation"
        desc="Navigation patterns including tabs, pills, vertical nav, and breadcrumbs"
      />

      <ExampleCard href="/spa/form"
        title="Forms"
        desc="Form inputs with validation states, checkboxes, textareas, and buttons"
      />
    </ExamplesGrid>
  </>
)

const ProfilePage = () => <ProfileExamplesContent />
const ButtonsPage = () => <ButtonExamplesContent />
const NavigationPage = () => <NavigationExamplesContent />
const FormPage = () => <FormExamplesContent />

export function route(path: string) {
  switch (path) {
    case '/spa':
    case '/spa/':
      return <HomePage />
    case '/spa/profile':
      return <ProfilePage />
    case '/spa/buttons':
      return <ButtonsPage />
    case '/spa/navigation':
      return <NavigationPage />
    case '/spa/form':
      return <FormPage />
    default:
      return <P>404 Not Found</P>
  }
}

export function App() {
  const path = window.location.pathname

  return (
    <Main>
      <Container>
        <Nav>
          <a href="/" style="color: #3b82f6; text-decoration: none; font-weight: 500;">Home</a>
          <Link href="/spa" aria-current={path === '/spa' || path === '/spa/' ? 'page' : undefined}>SPA Examples</Link>
          <Link href="/spa/profile" aria-current={path === '/spa/profile' ? 'page' : undefined}>Profile</Link>
          <Link href="/spa/buttons" aria-current={path === '/spa/buttons' ? 'page' : undefined}>Buttons</Link>
          <Link href="/spa/navigation" aria-current={path === '/spa/navigation' ? 'page' : undefined}>Navigation</Link>
          <Link href="/spa/form" aria-current={path === '/spa/form' ? 'page' : undefined}>Forms</Link>
        </Nav>
        <div id="content">
          {route(path)}
        </div>
      </Container>
    </Main>
  )
}
