import { useState, useRef, useEffect } from 'react';
import { Mail, Users, MessageCircle, ChevronDown, Check, Heart, X } from 'lucide-react';

export default function Navbar() {
  const [showContact, setShowContact] = useState(false);
  const [showReward, setShowReward] = useState(false);
  const [copied, setCopied] = useState(false);
  const dropdownRef = useRef<HTMLDivElement>(null);

  const handleCopyQQ = async () => {
    await navigator.clipboard.writeText('1018282100');
    setCopied(true);
    setTimeout(() => setCopied(false), 2000);
  };

  // Close dropdown when clicking outside
  useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
        setShowContact(false);
      }
    };

    document.addEventListener('mousedown', handleClickOutside);
    return () => document.removeEventListener('mousedown', handleClickOutside);
  }, []);

  return (
    <div className="h-16 bg-white border-b border-gray-200 flex items-center justify-between px-6 flex-shrink-0 z-[60] relative">
      {/* Left: Logo */}
      <div className="flex items-center gap-2">
        <span className="w-8 h-8 bg-blue-600 rounded-lg flex items-center justify-center text-white font-serif italic text-lg shadow-blue-600/30 shadow-md">
          T
        </span>
        <span className="text-xl font-bold text-gray-900 tracking-tight">
          TexPixel
        </span>
      </div>

      {/* Right: Reward & Contact Buttons */}
      <div className="flex items-center gap-3">
        {/* Reward Button */}
        <div className="relative">
          <button
            onClick={() => setShowReward(!showReward)}
            className="flex items-center gap-2 px-4 py-2 bg-gradient-to-r from-rose-500 to-pink-500 hover:from-rose-600 hover:to-pink-600 rounded-lg text-white text-sm font-medium transition-all shadow-sm hover:shadow-md"
          >
            <Heart size={14} className="fill-white" />
            <span>赞赏</span>
          </button>

          {/* Reward Modal */}
          {showReward && (
            <div
              className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-[70] p-4"
              onClick={() => setShowReward(false)}
            >
              <div
                className="bg-white rounded-xl shadow-xl max-w-sm w-full p-6 animate-in fade-in zoom-in-95 duration-200"
                onClick={e => e.stopPropagation()}
              >
                <div className="flex items-center justify-between mb-4">
                  <span className="text-lg font-bold text-gray-900">微信赞赏码</span>
                  <button
                    onClick={() => setShowReward(false)}
                    className="p-2 hover:bg-gray-100 rounded-lg transition-colors"
                  >
                    <X size={20} className="text-gray-500" />
                  </button>
                </div>
                <div className="flex flex-col items-center">
                  <img
                    src="https://cdn.texpixel.com/public/rewardcode.png"
                    alt="微信赞赏码"
                    className="w-64 h-64 object-contain rounded-lg shadow-sm"
                  />
                  <p className="text-sm text-gray-500 text-center mt-4">
                    感谢您的支持与鼓励 ❤️<br />
                    <span className="text-xs text-gray-400 mt-1 block">您的支持是我们持续更新的动力</span>
                  </p>
                </div>
              </div>
            </div>
          )}
        </div>

        {/* Contact Button with Dropdown */}
        <div className="relative" ref={dropdownRef}>
          <button
            onClick={() => setShowContact(!showContact)}
            className="flex items-center gap-2 px-4 py-2 bg-gray-100 hover:bg-gray-200 rounded-lg text-gray-700 text-sm font-medium transition-colors"
          >
            <MessageCircle size={14} />
            <span>Contact Us</span>
            <ChevronDown
              size={14}
              className={`transition-transform duration-200 ${showContact ? 'rotate-180' : ''}`}
            />
          </button>

          {/* Contact Dropdown List */}
          {showContact && (
            <div className="absolute right-0 top-full mt-2 w-64 bg-white rounded-xl shadow-lg border border-gray-200 py-2 z-50 animate-in fade-in slide-in-from-top-2 duration-200">
              <a
                href="mailto:yogecoder@gmail.com"
                className="flex items-center gap-3 px-4 py-3 hover:bg-gray-50 transition-colors"
              >
                <div className="w-8 h-8 bg-blue-100 rounded-lg flex items-center justify-center">
                  <Mail size={16} className="text-blue-600" />
                </div>
                <div>
                  <div className="text-xs text-gray-500">Email</div>
                  <div className="text-sm font-medium text-gray-900">yogecoder@gmail.com</div>
                </div>
              </a>
              <div
                className={`flex items-center gap-3 px-4 py-3 hover:bg-gray-50 transition-all cursor-pointer ${copied ? 'bg-green-50' : ''}`}
                onClick={handleCopyQQ}
              >
                <div className={`w-8 h-8 rounded-lg flex items-center justify-center transition-colors ${copied ? 'bg-green-500' : 'bg-green-100'}`}>
                  {copied ? (
                    <Check size={16} className="text-white" />
                  ) : (
                    <Users size={16} className="text-green-600" />
                  )}
                </div>
                <div>
                  <div className={`text-xs transition-colors ${copied ? 'text-green-600 font-medium' : 'text-gray-500'}`}>
                    {copied ? 'Copied!' : 'QQ Group (Click to Copy)'}
                  </div>
                  <div className="text-sm font-medium text-gray-900">1018282100</div>
                </div>
              </div>
            </div>
          )}
        </div>
      </div>
    </div>
  );
}











