import { useNavigate } from 'react-router-dom';

interface SuccessComponentProps {
  task: any;
}

const SuccessComponent: React.FC<SuccessComponentProps> = ({ task }) => {
  const navigate = useNavigate();

  const handleTryAgain = () => {
    navigate('/home');
  };

  const handleDownload = () => {
    if (task?.outputUrl) {
      // 在H5环境中，创建下载链接
      const link = document.createElement('a');
      link.href = task.outputUrl;
      link.download = `generated_${Date.now()}.${task.outputUrl.includes('.mp4') ? 'mp4' : 'jpg'}`;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  };

  return (
    <div className="result-page success">
      <div className="success-content">
        <div className="result-container">
          {task?.outputUrl ? (
            task.outputUrl.includes('.mp4') ? (
              <video
                className="result-video"
                src={task.outputUrl}
                controls
                autoPlay
                muted
                loop
              />
            ) : (
              <img
                className="result-image"
                src={task.outputUrl}
                alt="Generated result"
              />
            )
          ) : (
            <div className="result-placeholder">
              <div className="placeholder-text">结果生成中...</div>
            </div>
          )}
        </div>

        <div className="success-actions">
          <button className="download-btn" onClick={handleDownload}>
            下载
          </button>
          <button className="try-again-btn" onClick={handleTryAgain}>
            再来一张
          </button>
        </div>
      </div>
    </div>
  );
};

export default SuccessComponent;
