import { useState, useEffect } from 'react'
import { Outlet, Link, useNavigate, useLocation } from 'react-router-dom'
import { useAuthStore } from '@/store/auth'
import { useShopStore } from '@/store/shop'
import { Gamepad2, LogOut, Trophy, User, Menu, X, Shield, ShoppingBag, Coins, Backpack } from 'lucide-react'
import { clsx } from 'clsx'

export function Layout() {
  const { user, isAuthenticated, logout } = useAuthStore()
  const { balance, loadBalance } = useShopStore()
  const navigate = useNavigate()
  const location = useLocation()
  const [isScrolled, setIsScrolled] = useState(false)
  const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false)

  useEffect(() => {
    const handleScroll = () => {
      setIsScrolled(window.scrollY > 10)
    }
    window.addEventListener('scroll', handleScroll)
    return () => window.removeEventListener('scroll', handleScroll)
  }, [])

  // Load balance when authenticated
  useEffect(() => {
    if (isAuthenticated) {
      loadBalance()
    }
  }, [isAuthenticated, loadBalance])

  // Close mobile menu on route change
  useEffect(() => {
    setIsMobileMenuOpen(false)
  }, [location])

  const handleLogout = () => {
    logout()
    navigate('/login')
  }

  const isActiveLink = (path: string) => location.pathname === path

  return (
    <div className="min-h-screen flex flex-col">
      {/* Header */}
      <header
        className={clsx(
          'fixed top-0 left-0 right-0 z-50 transition-all duration-300',
          isScrolled
            ? 'bg-dark-900/80 backdrop-blur-lg border-b border-dark-600/50 shadow-lg'
            : 'bg-transparent'
        )}
      >
        <div className="container mx-auto px-4 py-4 flex items-center justify-between">
          {/* Logo */}
          <Link
            to="/"
            className="flex items-center gap-3 group"
          >
            <div className="relative">
              <Gamepad2 className="w-8 h-8 text-neon-500 transition-all duration-300 group-hover:text-neon-400 group-hover:drop-shadow-[0_0_8px_rgba(34,211,238,0.6)]" />
            </div>
            <span className="text-xl font-bold text-white font-display tracking-wider glitch-hover">
              МАРАФОН
            </span>
          </Link>

          {/* Desktop Navigation */}
          <nav className="hidden md:flex items-center gap-6">
            {isAuthenticated ? (
              <>
                <Link
                  to="/marathons"
                  className={clsx(
                    'flex items-center gap-2 px-3 py-2 rounded-lg transition-all duration-200',
                    isActiveLink('/marathons')
                      ? 'text-neon-400 bg-neon-500/10'
                      : 'text-gray-300 hover:text-white hover:bg-dark-700'
                  )}
                >
                  <Trophy className="w-5 h-5" />
                  <span>Марафоны</span>
                </Link>

                <Link
                  to="/shop"
                  className={clsx(
                    'flex items-center gap-2 px-3 py-2 rounded-lg transition-all duration-200',
                    isActiveLink('/shop')
                      ? 'text-yellow-400 bg-yellow-500/10'
                      : 'text-gray-300 hover:text-white hover:bg-dark-700'
                  )}
                >
                  <ShoppingBag className="w-5 h-5" />
                  <span>Магазин</span>
                </Link>

                {user?.role === 'admin' && (
                  <Link
                    to="/admin"
                    className={clsx(
                      'flex items-center gap-2 px-3 py-2 rounded-lg transition-all duration-200',
                      location.pathname.startsWith('/admin')
                        ? 'text-purple-400 bg-purple-500/10'
                        : 'text-gray-300 hover:text-white hover:bg-dark-700'
                    )}
                  >
                    <Shield className="w-5 h-5" />
                    <span>Админка</span>
                  </Link>
                )}

                <div className="flex items-center gap-2 ml-2 pl-4 border-l border-dark-600">
                  <Link
                    to="/profile"
                    className={clsx(
                      'flex items-center gap-2 px-3 py-2 rounded-lg transition-all duration-200',
                      isActiveLink('/profile')
                        ? 'text-neon-400 bg-neon-500/10'
                        : 'text-gray-300 hover:text-white hover:bg-dark-700'
                    )}
                  >
                    <User className="w-5 h-5" />
                    <span>{user?.nickname}</span>
                    <span className="flex items-center gap-1 text-yellow-400 ml-1">
                      <Coins className="w-4 h-4" />
                      <span className="font-medium">{balance}</span>
                    </span>
                  </Link>

                  <Link
                    to="/inventory"
                    className={clsx(
                      'p-2 rounded-lg transition-all duration-200',
                      isActiveLink('/inventory')
                        ? 'text-yellow-400 bg-yellow-500/10'
                        : 'text-gray-400 hover:text-yellow-400 hover:bg-yellow-500/10'
                    )}
                    title="Инвентарь"
                  >
                    <Backpack className="w-5 h-5" />
                  </Link>

                  <button
                    onClick={handleLogout}
                    className="p-2 text-gray-400 hover:text-red-400 hover:bg-red-500/10 rounded-lg transition-all duration-200"
                    title="Выйти"
                  >
                    <LogOut className="w-5 h-5" />
                  </button>
                </div>
              </>
            ) : (
              <>
                <Link
                  to="/login"
                  className="text-gray-300 hover:text-white transition-colors px-4 py-2"
                >
                  Войти
                </Link>
                <Link
                  to="/register"
                  className="px-4 py-2 bg-neon-500 hover:bg-neon-400 text-dark-900 font-semibold rounded-lg transition-all duration-200 shadow-[0_0_10px_rgba(34,211,238,0.25)] hover:shadow-[0_0_16px_rgba(34,211,238,0.4)]"
                >
                  Регистрация
                </Link>
              </>
            )}
          </nav>

          {/* Mobile Menu Button */}
          <button
            onClick={() => setIsMobileMenuOpen(!isMobileMenuOpen)}
            className="md:hidden p-2 text-gray-300 hover:text-white rounded-lg hover:bg-dark-700 transition-colors"
          >
            {isMobileMenuOpen ? <X className="w-6 h-6" /> : <Menu className="w-6 h-6" />}
          </button>
        </div>

        {/* Mobile Menu */}
        {isMobileMenuOpen && (
          <div className="md:hidden bg-dark-800/95 backdrop-blur-lg border-t border-dark-600 animate-slide-in-down">
            <div className="container mx-auto px-4 py-4 space-y-2">
              {isAuthenticated ? (
                <>
                  <Link
                    to="/marathons"
                    className={clsx(
                      'flex items-center gap-3 px-4 py-3 rounded-lg transition-all',
                      isActiveLink('/marathons')
                        ? 'text-neon-400 bg-neon-500/10'
                        : 'text-gray-300 hover:text-white hover:bg-dark-700'
                    )}
                  >
                    <Trophy className="w-5 h-5" />
                    <span>Марафоны</span>
                  </Link>
                  <Link
                    to="/shop"
                    className={clsx(
                      'flex items-center gap-3 px-4 py-3 rounded-lg transition-all',
                      isActiveLink('/shop')
                        ? 'text-yellow-400 bg-yellow-500/10'
                        : 'text-gray-300 hover:text-white hover:bg-dark-700'
                    )}
                  >
                    <ShoppingBag className="w-5 h-5" />
                    <span>Магазин</span>
                  </Link>
                  {user?.role === 'admin' && (
                    <Link
                      to="/admin"
                      className={clsx(
                        'flex items-center gap-3 px-4 py-3 rounded-lg transition-all',
                        location.pathname.startsWith('/admin')
                          ? 'text-purple-400 bg-purple-500/10'
                          : 'text-gray-300 hover:text-white hover:bg-dark-700'
                      )}
                    >
                      <Shield className="w-5 h-5" />
                      <span>Админка</span>
                    </Link>
                  )}
                  <Link
                    to="/profile"
                    className={clsx(
                      'flex items-center gap-3 px-4 py-3 rounded-lg transition-all',
                      isActiveLink('/profile')
                        ? 'text-neon-400 bg-neon-500/10'
                        : 'text-gray-300 hover:text-white hover:bg-dark-700'
                    )}
                  >
                    <User className="w-5 h-5" />
                    <span>{user?.nickname}</span>
                    <span className="flex items-center gap-1 text-yellow-400 ml-auto">
                      <Coins className="w-4 h-4" />
                      <span className="font-medium">{balance}</span>
                    </span>
                  </Link>
                  <Link
                    to="/inventory"
                    className={clsx(
                      'flex items-center gap-3 px-4 py-3 rounded-lg transition-all',
                      isActiveLink('/inventory')
                        ? 'text-yellow-400 bg-yellow-500/10'
                        : 'text-gray-300 hover:text-white hover:bg-dark-700'
                    )}
                  >
                    <Backpack className="w-5 h-5" />
                    <span>Инвентарь</span>
                  </Link>
                  <div className="pt-2 border-t border-dark-600">
                    <button
                      onClick={handleLogout}
                      className="flex items-center gap-3 w-full px-4 py-3 text-red-400 hover:bg-red-500/10 rounded-lg transition-all"
                    >
                      <LogOut className="w-5 h-5" />
                      <span>Выйти</span>
                    </button>
                  </div>
                </>
              ) : (
                <>
                  <Link
                    to="/login"
                    className="block px-4 py-3 text-gray-300 hover:text-white hover:bg-dark-700 rounded-lg transition-all"
                  >
                    Войти
                  </Link>
                  <Link
                    to="/register"
                    className="block px-4 py-3 text-center bg-neon-500 hover:bg-neon-400 text-dark-900 font-semibold rounded-lg transition-all"
                  >
                    Регистрация
                  </Link>
                </>
              )}
            </div>
          </div>
        )}
      </header>

      {/* Spacer for fixed header */}
      <div className="h-[72px]" />

      {/* Main content */}
      <main className="flex-1 container mx-auto px-4 py-8">
        <Outlet />
      </main>

      {/* Footer */}
      <footer className="bg-dark-800/50 border-t border-dark-600/50 py-6">
        <div className="container mx-auto px-4">
          <div className="flex flex-col md:flex-row items-center justify-between gap-4">
            <div className="flex items-center gap-2 text-gray-500">
              <Gamepad2 className="w-5 h-5 text-neon-500/50" />
              <span className="text-sm">
                Игровой Марафон &copy; {new Date().getFullYear()}
              </span>
            </div>
            <div className="flex items-center gap-6 text-sm">
              <Link to="/terms" className="text-gray-500 hover:text-gray-300 transition-colors">
                Правила
              </Link>
              <Link to="/privacy" className="text-gray-500 hover:text-gray-300 transition-colors">
                Конфиденциальность
              </Link>
              <span className="text-neon-500/50">v1.0</span>
            </div>
          </div>
        </div>
      </footer>
    </div>
  )
}
