"use client";

import { useState } from "react";
import { Share2, Link2, MessageCircle, Check } from "lucide-react";
import { motion, AnimatePresence } from "framer-motion";

export function ShareMenu({ url, title }: { url: string; title: string }) {
  const [isOpen, setIsOpen] = useState(false);
  const [copied, setCopied] = useState(false);

  const fullUrl = typeof window !== "undefined" ? window.location.origin + url : url;

  const handleShare = async () => {
    if (navigator.share) {
      try {
        await navigator.share({
          title: title,
          url: fullUrl
        });
      } catch (err) {
        setIsOpen(!isOpen);
      }
    } else {
      setIsOpen(!isOpen);
    }
  };

  const copyLink = () => {
    navigator.clipboard.writeText(fullUrl);
    setCopied(true);
    setTimeout(() => {
      setCopied(false);
      setIsOpen(false);
    }, 2000);
  };

  return (
    <div className="relative">
      <button 
        onClick={handleShare}
        className="w-10 h-10 rounded-full bg-surface border border-border flex items-center justify-center text-textSecondary hover:text-primary hover:border-primary transition-all duration-300"
        title="Partager"
      >
        <Share2 size={18} />
      </button>

      <AnimatePresence>
        {isOpen && (
          <>
            <motion.div 
              initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}
              className="fixed inset-0 z-40" onClick={() => setIsOpen(false)}
            />
            <motion.div 
              initial={{ opacity: 0, y: 10, scale: 0.95 }}
              animate={{ opacity: 1, y: 0, scale: 1 }}
              exit={{ opacity: 0, y: 10, scale: 0.95 }}
              className="absolute right-0 top-12 w-64 glass rounded-xl border border-border p-3 shadow-2xl z-50 flex flex-col gap-2"
            >
              <p className="text-xs font-bold text-textMuted uppercase px-2 pb-1 border-b border-border">Partager</p>
              <div className="grid grid-cols-4 gap-2 py-2">
                <a href={`https://twitter.com/intent/tweet?text=${encodeURIComponent(title)}&url=${encodeURIComponent(fullUrl)}`} target="_blank" rel="noreferrer" className="flex flex-col items-center gap-1 hover:text-[#1DA1F2] transition-colors">
                  <div className="w-10 h-10 rounded-full bg-surface flex items-center justify-center">
                    <svg width="18" height="18" fill="currentColor" viewBox="0 0 24 24"><path d="M24 4.557c-.883.392-1.832.656-2.828.775 1.017-.609 1.798-1.574 2.165-2.724-.951.564-2.005.974-3.127 1.195-.897-.957-2.178-1.555-3.594-1.555-3.179 0-5.515 2.966-4.797 6.045-4.091-.205-7.719-2.165-10.148-5.144-1.29 2.213-.669 5.108 1.523 6.574-.806-.026-1.566-.247-2.229-.616-.054 2.281 1.581 4.415 3.949 4.89-.693.188-1.452.232-2.224.084.626 1.956 2.444 3.379 4.6 3.419-2.07 1.623-4.678 2.348-7.29 2.04 2.179 1.397 4.768 2.212 7.548 2.212 9.142 0 14.307-7.721 13.995-14.646.962-.695 1.797-1.562 2.457-2.549z"/></svg>
                  </div>
                  <span className="text-[10px]">Twitter</span>
                </a>
                <a href={`https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(fullUrl)}`} target="_blank" rel="noreferrer" className="flex flex-col items-center gap-1 hover:text-[#4267B2] transition-colors">
                  <div className="w-10 h-10 rounded-full bg-surface flex items-center justify-center">
                    <svg width="18" height="18" fill="currentColor" viewBox="0 0 24 24"><path d="M9 8h-3v4h3v12h5v-12h3.642l.358-4h-4v-1.667c0-.955.192-1.333 1.115-1.333h2.885v-5h-3.808c-3.596 0-5.192 1.583-5.192 4.615v3.385z"/></svg>
                  </div>
                  <span className="text-[10px]">Facebook</span>
                </a>
                <a href={`https://wa.me/?text=${encodeURIComponent(title + " " + fullUrl)}`} target="_blank" rel="noreferrer" className="flex flex-col items-center gap-1 hover:text-[#25D366] transition-colors">
                  <div className="w-10 h-10 rounded-full bg-surface flex items-center justify-center"><MessageCircle size={18} /></div>
                  <span className="text-[10px]">WhatsApp</span>
                </a>
                <button onClick={copyLink} className="flex flex-col items-center gap-1 hover:text-primary transition-colors">
                  <div className="w-10 h-10 rounded-full bg-surface flex items-center justify-center">
                    {copied ? <Check size={18} className="text-green-500" /> : <Link2 size={18} />}
                  </div>
                  <span className="text-[10px]">{copied ? "Copié!" : "Copier"}</span>
                </button>
              </div>
            </motion.div>
          </>
        )}
      </AnimatePresence>
    </div>
  );
}
