import React, { useState } from "react";
import "./WordBundleBasicLearning.css";

const WordBundleBasicLearning = ({ contents }) => {
  const [isPlaying, setIsPlaying] = useState(false);

  const toggleAudio = () => {
    const audio = document.getElementById("song-audio");
    if (isPlaying) {
      audio.pause();
    } else {
      audio.play();
    }
    setIsPlaying(!isPlaying);
  };

  if (!contents) {
    return <div>Loading...</div>;
  }
  return (
    <>
      <img
        src={contents.image}
        alt="이미지"
        className="wordBundle-basic-learning-img"
        onClick={toggleAudio}
      />

      <div className="wordBundle-basic-learning-word">
        <h1>{contents.word}</h1>
        <h3>{contents.wordMeaning}</h3>
      </div>
      <audio id="song-audio" controls style={{ display: "none" }}>
            <source src={contents.file} type="audio/mpeg" />
          </audio>
    </>
  );
};

export default WordBundleBasicLearning;
