import React, { useState } from 'react';
import { Button } from '../ui/button';
import { X, Repeat, MessageSquare, Link, Mail } from 'lucide-react';
import { Post } from '../../types';
import { useToast } from '../../context/ToastContext';

interface SharePostModalProps {
  post: Post;
  onClose: () => void;
  onConfirm: (type: 'repost' | 'quote', text?: string) => void;
}

export const SharePostModal: React.FC<SharePostModalProps> = ({
  post,
  onClose,
  onConfirm,
}) => {
  const { addToast } = useToast();
  const [mode, setMode] = useState<'options' | 'quote'>('options');
  const [quoteText, setQuoteText] = useState('');

  const handleCopyLink = () => {
    navigator.clipboard.writeText(`https://veza.io/post/${post.id}`);
    addToast('Link copied to clipboard', 'success');
    onClose();
  };

  if (mode === 'quote') {
    return (
      <div className="fixed inset-0 z-[100] flex items-center justify-center p-4">
        <div
          className="absolute inset-0 bg-kodo-void/90 backdrop-blur-sm"
          onClick={onClose}
        ></div>
        <div className="relative w-full max-w-lg bg-kodo-graphite border border-kodo-steel rounded-xl shadow-2xl animate-scaleIn overflow-hidden">
          <div className="p-4 border-b border-kodo-steel bg-kodo-ink flex justify-between items-center">
            <h3 className="font-bold text-white">Quote Post</h3>
            <button onClick={onClose}>
              <X className="w-5 h-5 text-gray-400 hover:text-white" />
            </button>
          </div>
          <div className="p-4">
            <textarea
              className="w-full bg-transparent border-none text-white placeholder-gray-500 focus:ring-0 resize-none h-24 mb-4"
              placeholder="Add a comment..."
              value={quoteText}
              onChange={(e) => setQuoteText(e.target.value)}
              autoFocus
            />
            <div className="border border-kodo-steel rounded-lg p-3 bg-kodo-ink/30 opacity-70">
              <div className="flex items-center gap-2 mb-2">
                <img
                  src={post.author.avatar}
                  className="w-5 h-5 rounded-full"
                />
                <span className="font-bold text-xs text-white">
                  {post.author.name}
                </span>
              </div>
              <p className="text-xs text-gray-400 line-clamp-2">
                {post.content}
              </p>
            </div>
          </div>
          <div className="p-4 border-t border-kodo-steel bg-kodo-ink flex justify-end gap-2">
            <Button variant="ghost" onClick={() => setMode('options')}>
              Back
            </Button>
            <Button
              variant="primary"
              onClick={() => {
                onConfirm('quote', quoteText);
                onClose();
              }}
            >
              Quote
            </Button>
          </div>
        </div>
      </div>
    );
  }

  return (
    <div className="fixed inset-0 z-[100] flex items-center justify-center p-4">
      <div
        className="absolute inset-0 bg-kodo-void/90 backdrop-blur-sm"
        onClick={onClose}
      ></div>
      <div className="relative w-full max-w-sm bg-kodo-graphite border border-kodo-steel rounded-xl shadow-2xl animate-scaleIn overflow-hidden">
        <div className="p-4 border-b border-kodo-steel bg-kodo-ink flex justify-between items-center">
          <h3 className="font-bold text-white">Share Post</h3>
          <button onClick={onClose}>
            <X className="w-5 h-5 text-gray-400 hover:text-white" />
          </button>
        </div>

        <div className="p-2">
          <button
            className="w-full flex items-center gap-4 p-3 hover:bg-white/5 rounded-lg transition-colors group"
            onClick={() => {
              onConfirm('repost');
              onClose();
            }}
          >
            <div className="w-10 h-10 rounded-full bg-kodo-lime/10 flex items-center justify-center text-kodo-lime group-hover:bg-kodo-lime/20">
              <Repeat className="w-5 h-5" />
            </div>
            <div className="text-left">
              <div className="text-white font-bold">Repost</div>
              <div className="text-xs text-gray-400">
                Instantly share with your followers
              </div>
            </div>
          </button>

          <button
            className="w-full flex items-center gap-4 p-3 hover:bg-white/5 rounded-lg transition-colors group"
            onClick={() => setMode('quote')}
          >
            <div className="w-10 h-10 rounded-full bg-kodo-cyan/10 flex items-center justify-center text-kodo-cyan group-hover:bg-kodo-cyan/20">
              <MessageSquare className="w-5 h-5" />
            </div>
            <div className="text-left">
              <div className="text-white font-bold">Quote</div>
              <div className="text-xs text-gray-400">
                Repost with your own thoughts
              </div>
            </div>
          </button>

          <div className="h-px bg-kodo-steel/50 my-2 mx-3"></div>

          <button
            className="w-full flex items-center gap-4 p-3 hover:bg-white/5 rounded-lg transition-colors group"
            onClick={handleCopyLink}
          >
            <div className="w-10 h-10 rounded-full bg-gray-800 flex items-center justify-center text-gray-400 group-hover:text-white">
              <Link className="w-5 h-5" />
            </div>
            <div className="text-left">
              <div className="text-white font-bold">Copy Link</div>
            </div>
          </button>
          <button
            className="w-full flex items-center gap-4 p-3 hover:bg-white/5 rounded-lg transition-colors group"
            onClick={() => {
              addToast('Sent via DM');
              onClose();
            }}
          >
            <div className="w-10 h-10 rounded-full bg-gray-800 flex items-center justify-center text-gray-400 group-hover:text-white">
              <Mail className="w-5 h-5" />
            </div>
            <div className="text-left">
              <div className="text-white font-bold">
                Send via Direct Message
              </div>
            </div>
          </button>
        </div>
      </div>
    </div>
  );
};
