'use client'

import Link from 'next/link'
import Image from 'next/image'
import { Menu, X } from 'lucide-react'
import { useState, useEffect, useRef } from 'react'
import { useTerminal } from '@/lib/TerminalContext'
import { useLanguage } from '@/lib/LanguageContext'
import { CIStatusIndicator } from './CIStatusIndicator'

export default function Header() {
  const [open, setOpen] = useState(false)

  const [skillsOpen, setSkillsOpen] = useState(false)
  const [contactOpen, setContactOpen] = useState(false)
  const [educationOpen, setEducationOpen] = useState(false)
  const { isOpen: terminalOpen, setIsOpen: setTerminalOpen } = useTerminal()
  const { lang, setLang, t } = useLanguage()
  const [copied, setCopied] = useState<string | null>(null)

  const handleCopy = (text: string, label: string) => {
    navigator.clipboard.writeText(text)
    setCopied(label)
    setTimeout(() => setCopied(null), 2000)
  }

  const toggleLang = () => {
    setLang(lang === 'en' ? 'zh' : 'en')
  }

  const skillsRef = useRef<HTMLDivElement>(null)
  const contactRef = useRef<HTMLDivElement>(null)
  const educationRef = useRef<HTMLDivElement>(null)

  useEffect(() => {
    const handleClickOutside = (e: MouseEvent) => {
      if (skillsRef.current && !skillsRef.current.contains(e.target as Node)) setSkillsOpen(false)
      if (contactRef.current && !contactRef.current.contains(e.target as Node)) setContactOpen(false)
      if (educationRef.current && !educationRef.current.contains(e.target as Node)) setEducationOpen(false)
    }
    document.addEventListener('mousedown', handleClickOutside)
    return () => document.removeEventListener('mousedown', handleClickOutside)
  }, [])

  return (
    <header className="sticky top-0 z-50 border-b border-gray-200 dark:border-gray-800 bg-white/95 dark:bg-gray-950/95 backdrop-blur">
      <div className="max-w-6xl mx-auto px-6 py-4 flex justify-between items-center">
        <Link href="/" className="flex items-center gap-2">
          <Image
            src="/avatar.jpeg"
            alt="Profile"
            width={32}
            height={32}
            className="rounded-full object-cover"
          />
          <div>
            <div className="text-sm font-bold text-gray-900 dark:text-white">{t.header.title}</div>
            <div className="text-xs text-gray-600 dark:text-gray-400">{t.header.subtitle}</div>
          </div>
        </Link>

        <nav className="hidden md:flex gap-8 items-center">
          <button onClick={() => window.scrollTo({ top: 0, behavior: 'smooth' })} className="text-sm hover:text-blue-600 dark:hover:text-blue-400">{t.header.nav.home}</button>
          <button 
            onClick={() => setTerminalOpen(!terminalOpen)}
            className="text-sm hover:text-blue-600 dark:hover:text-blue-400"
          >
            {t.header.nav.askPoimen}
          </button>
          <div className="relative" ref={skillsRef}>
            <button 
              onClick={() => setSkillsOpen(!skillsOpen)}
              className="text-sm hover:text-blue-600 dark:hover:text-blue-400"
            >
              {t.header.nav.skills}
            </button>
            {skillsOpen && (
              <div className="absolute right-0 pt-2 w-80">
                <div className="bg-white dark:bg-gray-900 border border-gray-200 dark:border-gray-800 rounded-lg shadow-lg p-4">
                <div className="grid grid-cols-2 gap-3">
                  {Object.entries(t.header.skills).map(([key, category]) => (
                    <div key={key}>
                      <div className="text-xs font-semibold text-gray-500 dark:text-gray-400 mb-1">{(category as {title: string}).title}</div>
                      <div className="flex flex-wrap gap-1">
                        {(category as {items: string[]}).items.map((skill) => (
                          <span key={skill} className="text-xs px-1.5 py-0.5 bg-blue-100 dark:bg-blue-900 text-blue-700 dark:text-blue-300 rounded">
                            {skill}
                          </span>
                        ))}
                      </div>
                    </div>
                  ))}
                </div>
              </div>
                </div>
            )}
          </div>
          <div className="relative" ref={contactRef}>
            <button 
              onClick={() => setContactOpen(!contactOpen)}
              className="text-sm hover:text-blue-600 dark:hover:text-blue-400"
            >
              {t.header.nav.contact}
            </button>
            {contactOpen && (
              <div className="absolute right-0 mt-2 w-64 bg-white dark:bg-gray-900 border border-gray-200 dark:border-gray-800 rounded-lg shadow-lg p-4 space-y-3">
                <div className="flex items-center justify-between border-b border-gray-200 dark:border-gray-700 pb-3">
                  <span className="text-sm text-gray-700 dark:text-gray-300 flex-1">
                    {t.header.contact.portfolio}
                  </span>
                  <button
                    onClick={() => handleCopy('https://portfolio.riotpiao.com', 'Portfolio')}
                    className="text-xs px-2 py-1 rounded bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 hover:bg-blue-100 dark:hover:bg-blue-900"
                  >
                    {copied === 'Portfolio' ? '✓' : t.header.contact.copy}
                  </button>
                </div>
                <div className="flex items-center justify-between">
                  <a 
                    href="https://github.com/rockliang" 
                    target="_blank" 
                    rel="noopener noreferrer"
                    className="text-sm text-gray-700 dark:text-gray-300 hover:text-blue-600 dark:hover:text-blue-400 flex-1"
                  >
                    {t.header.contact.github}
                  </a>
                  <button
                    onClick={() => handleCopy('https://github.com/rockliang', 'GitHub')}
                    className="text-xs px-2 py-1 rounded bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 hover:bg-blue-100 dark:hover:bg-blue-900"
                  >
                    {copied === 'GitHub' ? '✓' : t.header.contact.copy}
                  </button>
                </div>
                <div className="flex items-center justify-between">
                  <a 
                    href="https://github.com/rockliang" 
                    target="_blank" 
                    rel="noopener noreferrer"
                    className="text-sm text-gray-700 dark:text-gray-300 hover:text-blue-600 dark:hover:text-blue-400 flex-1"
                  >
                    {t.header.contact.privateGithub}
                  </a>
                  <button
                    onClick={() => handleCopy('https://github.com/rockliang', 'PrivateGithub')}
                    className="text-xs px-2 py-1 rounded bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 hover:bg-blue-100 dark:hover:bg-blue-900"
                  >
                    {copied === 'PrivateGithub' ? '✓' : t.header.contact.copy}
                  </button>
                </div>
                <div className="flex items-center justify-between">
                  <a 
                    href="https://www.linkedin.com/in/rockliang" 
                    target="_blank" 
                    rel="noopener noreferrer"
                    className="text-sm text-gray-700 dark:text-gray-300 hover:text-blue-600 dark:hover:text-blue-400 flex-1"
                  >
                    {t.header.contact.linkedin}
                  </a>
                  <button
                    onClick={() => handleCopy('https://www.linkedin.com/in/rockliang', 'LinkedIn')}
                    className="text-xs px-2 py-1 rounded bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 hover:bg-blue-100 dark:hover:bg-blue-900"
                  >
                    {copied === 'LinkedIn' ? '✓' : t.header.contact.copy}
                  </button>
                </div>
                <div className="flex items-center justify-between">
                  <a 
                    href="mailto:locartrock@gmail.com"
                    className="text-sm text-gray-700 dark:text-gray-300 hover:text-blue-600 dark:hover:text-blue-400 flex-1"
                  >
                    {t.header.contact.email}
                  </a>
                  <button
                    onClick={() => handleCopy('locartrock@gmail.com', 'Email')}
                    className="text-xs px-2 py-1 rounded bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 hover:bg-blue-100 dark:hover:bg-blue-900"
                  >
                    {copied === 'Email' ? '✓' : t.header.contact.copy}
                  </button>
                </div>
              </div>
            )}
          </div>
          <div className="border-l border-gray-300 dark:border-gray-700 pl-8 flex gap-6 items-center">
            <button
              onClick={() => document.getElementById('experience')?.scrollIntoView({ behavior: 'smooth' })}
              className="text-xs text-left cursor-pointer hover:text-blue-600 dark:hover:text-blue-400"
            >
              <div className="text-gray-600 dark:text-gray-400">{t.header.experience}</div>
              <div className="font-semibold text-gray-900 dark:text-white">{t.header.experienceValue}</div>
            </button>
            <div className="relative" ref={educationRef}>
              <button 
                onClick={() => setEducationOpen(!educationOpen)}
                className="text-xs cursor-pointer hover:text-blue-600 dark:hover:text-blue-400"
              >
                <div className="text-gray-600 dark:text-gray-400">{t.header.education}</div>
                <div className="font-semibold text-gray-900 dark:text-white">{t.header.educationValue}</div>
              </button>
              {educationOpen && (
                <div className="absolute right-0 mt-2 w-72 bg-white dark:bg-gray-900 border border-gray-200 dark:border-gray-800 rounded-lg shadow-lg p-4 space-y-4">
                  <div className="border-b border-gray-200 dark:border-gray-700 pb-4">
                    <h3 className="font-semibold text-gray-900 dark:text-white">{t.header.educationDetails.bachelor.school}</h3>
                    <p className="text-sm text-gray-600 dark:text-gray-400 mt-1">{t.header.educationDetails.bachelor.degree}</p>
                    <p className="text-xs text-gray-500 dark:text-gray-500 mt-2">{t.header.educationDetails.bachelor.period}</p>
                  </div>
                  <div>
                    <h3 className="font-semibold text-gray-900 dark:text-white">{t.header.educationDetails.master.school}</h3>
                    <p className="text-sm text-gray-600 dark:text-gray-400 mt-1">{t.header.educationDetails.master.degree}</p>
                    <p className="text-xs text-gray-500 dark:text-gray-500 mt-2">{t.header.educationDetails.master.period}</p>
                    {(t.header.educationDetails.master as { publication?: { title: string; venue: string; url: string } }).publication && (
                      <a
                        href={(t.header.educationDetails.master as { publication: { url: string } }).publication.url}
                        target="_blank"
                        rel="noopener noreferrer"
                        className="mt-3 block p-3 rounded-lg border border-blue-200 dark:border-blue-800 bg-blue-50 dark:bg-blue-950 hover:border-blue-400 dark:hover:border-blue-600 transition-colors"
                      >
                        <div className="flex items-start gap-2">
                          <span className="text-blue-500 mt-0.5">📄</span>
                          <div>
                            <p className="text-xs font-semibold text-blue-700 dark:text-blue-300">
                              {(t.header.educationDetails.master as { publication: { title: string } }).publication.title}
                            </p>
                            <p className="text-xs text-blue-500 dark:text-blue-400 mt-1">
                              {(t.header.educationDetails.master as { publication: { venue: string } }).publication.venue} →
                            </p>
                          </div>
                        </div>
                      </a>
                    )}
                  </div>
                </div>
              )}
            </div>
            {/* Language Toggle */}
            <button 
              onClick={toggleLang} 
              className="px-3 py-1 text-sm font-semibold rounded border border-gray-300 dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-800"
            >
              {lang === 'en' ? '中文' : 'EN'}
            </button>
            {/* CI Status */}
            <CIStatusIndicator />
          </div>
        </nav>

        <div className="flex items-center gap-2 md:hidden">
          {/* Mobile Language Toggle */}
          <button 
            onClick={toggleLang} 
            className="px-2 py-1 text-xs font-semibold rounded border border-gray-300 dark:border-gray-600"
          >
            {lang === 'en' ? '中文' : 'EN'}
          </button>
          <button
            onClick={() => setOpen(!open)}
            className="p-2"
          >
            {open ? <X size={24} /> : <Menu size={24} />}
          </button>
        </div>
      </div>

      {open && (
        <div className="md:hidden border-t border-gray-200 dark:border-gray-800 p-4 space-y-4">
          <button onClick={() => { setOpen(false); window.scrollTo({ top: 0, behavior: 'smooth' }); }} className="block py-2 text-sm hover:text-blue-600">{t.header.nav.home}</button>
          <button 
            onClick={() => setTerminalOpen(!terminalOpen)}
            className="block py-2 text-sm hover:text-blue-600"
          >
            {t.header.nav.askPoimen}
          </button>
          <button 
            onClick={() => setSkillsOpen(!skillsOpen)}
            className="block py-2 text-sm hover:text-blue-600"
          >
            {t.header.nav.skills}
          </button>
          {skillsOpen && (
            <div className="bg-gray-50 dark:bg-gray-800 rounded p-3 mt-2">
              <div className="grid grid-cols-2 gap-3">
                {Object.entries(t.header.skills).map(([key, category]) => (
                  <div key={key}>
                    <div className="text-xs font-semibold text-gray-500 dark:text-gray-400 mb-1">{(category as {title: string}).title}</div>
                    <div className="flex flex-wrap gap-1">
                      {(category as {items: string[]}).items.map((skill) => (
                        <span key={skill} className="text-xs px-1.5 py-0.5 bg-blue-100 dark:bg-blue-900 text-blue-700 dark:text-blue-300 rounded">
                          {skill}
                        </span>
                      ))}
                    </div>
                  </div>
                ))}
              </div>
            </div>
          )}
          <button 
            onClick={() => setContactOpen(!contactOpen)}
            className="block py-2 text-sm hover:text-blue-600"
          >
            {t.header.nav.contact}
          </button>
          {contactOpen && (
            <div className="bg-gray-50 dark:bg-gray-800 rounded p-3 mt-2 space-y-3">
              <div className="flex items-center justify-between border-b border-gray-300 dark:border-gray-700 pb-3">
                <span className="text-sm text-gray-700 dark:text-gray-300 flex-1">
                  {t.header.contact.portfolio}
                </span>
                <button
                  onClick={() => handleCopy('https://portfolio.riotpiao.com', 'Portfolio')}
                  className="text-xs px-2 py-1 rounded bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400 hover:bg-blue-100 dark:hover:bg-blue-900"
                >
                  {copied === 'Portfolio' ? '✓' : t.header.contact.copy}
                </button>
              </div>
              <div className="flex items-center justify-between">
                <a 
                  href="https://github.com/rockliang" 
                  target="_blank" 
                  rel="noopener noreferrer"
                  className="text-sm text-gray-700 dark:text-gray-300 hover:text-blue-600 flex-1"
                >
                  {t.header.contact.github}
                </a>
                <button
                  onClick={() => handleCopy('https://github.com/rockliang', 'GitHub')}
                  className="text-xs px-2 py-1 rounded bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400 hover:bg-blue-100 dark:hover:bg-blue-900"
                >
                  {copied === 'GitHub' ? '✓' : t.header.contact.copy}
                </button>
              </div>
              <div className="flex items-center justify-between">
                <a 
                  href="https://github.com/rockliang" 
                  target="_blank" 
                  rel="noopener noreferrer"
                  className="text-sm text-gray-700 dark:text-gray-300 hover:text-blue-600 flex-1"
                >
                  {t.header.contact.privateGithub}
                </a>
                <button
                  onClick={() => handleCopy('https://github.com/rockliang', 'PrivateGithub')}
                  className="text-xs px-2 py-1 rounded bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400 hover:bg-blue-100 dark:hover:bg-blue-900"
                >
                  {copied === 'PrivateGithub' ? '✓' : t.header.contact.copy}
                </button>
              </div>
              <div className="flex items-center justify-between">
                <a 
                  href="https://www.linkedin.com/in/rockliang" 
                  target="_blank" 
                  rel="noopener noreferrer"
                  className="text-sm text-gray-700 dark:text-gray-300 hover:text-blue-600 flex-1"
                >
                  {t.header.contact.linkedin}
                </a>
                <button
                  onClick={() => handleCopy('https://www.linkedin.com/in/rockliang', 'LinkedIn')}
                  className="text-xs px-2 py-1 rounded bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400 hover:bg-blue-100 dark:hover:bg-blue-900"
                >
                  {copied === 'LinkedIn' ? '✓' : t.header.contact.copy}
                </button>
              </div>
              <div className="flex items-center justify-between">
                <a 
                  href="mailto:locartrock@gmail.com"
                  className="text-sm text-gray-700 dark:text-gray-300 hover:text-blue-600 flex-1"
                >
                  {t.header.contact.email}
                </a>
                <button
                  onClick={() => handleCopy('locartrock@gmail.com', 'Email')}
                  className="text-xs px-2 py-1 rounded bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400 hover:bg-blue-100 dark:hover:bg-blue-900"
                >
                  {copied === 'Email' ? '✓' : t.header.contact.copy}
                </button>
              </div>
            </div>
          )}
          <div className="border-t border-gray-200 dark:border-gray-800 pt-4 space-y-4">
            <button
              onClick={() => { setOpen(false); document.getElementById('experience')?.scrollIntoView({ behavior: 'smooth' }); }}
              className="text-xs text-left cursor-pointer hover:text-blue-600 dark:hover:text-blue-400"
            >
              <div className="text-gray-600 dark:text-gray-400">{t.header.experience}</div>
              <div className="font-semibold text-gray-900 dark:text-white">{t.header.experienceValue}</div>
            </button>
            <div>
              <button 
                onClick={() => setEducationOpen(!educationOpen)}
                className="text-xs cursor-pointer hover:text-blue-600 dark:hover:text-blue-400 w-full text-left"
              >
                <div className="text-gray-600 dark:text-gray-400">{t.header.education}</div>
                <div className="font-semibold text-gray-900 dark:text-white">{t.header.educationValue}</div>
              </button>
              {educationOpen && (
                <div className="bg-gray-50 dark:bg-gray-800 rounded p-3 mt-2 space-y-4">
                  <div className="border-b border-gray-300 dark:border-gray-700 pb-4">
                    <h3 className="font-semibold text-gray-900 dark:text-white text-sm">{t.header.educationDetails.bachelor.school}</h3>
                    <p className="text-xs text-gray-600 dark:text-gray-400 mt-1">{t.header.educationDetails.bachelor.degree}</p>
                    <p className="text-xs text-gray-500 dark:text-gray-500 mt-2">{t.header.educationDetails.bachelor.period}</p>
                  </div>
                  <div>
                    <h3 className="font-semibold text-gray-900 dark:text-white text-sm">{t.header.educationDetails.master.school}</h3>
                    <p className="text-xs text-gray-600 dark:text-gray-400 mt-1">{t.header.educationDetails.master.degree}</p>
                    <p className="text-xs text-gray-500 dark:text-gray-500 mt-2">{t.header.educationDetails.master.period}</p>
                  </div>
                </div>
              )}
            </div>
          </div>
        </div>
      )}
    </header>
  )
}
