import { useState, useEffect, useRef } from "react";
import SkillCard from "../SkillCard/SkillCard.jsx";
import "./SingleProject.css";

function SingleProject({ image, title, description, skills, id, nbImage, school, beginningYear, endYear }) {
    const [imageID, setImageID] = useState(1);
    const [isFading, setIsFading] = useState(true);
    const intervalRef = useRef(null);
    const color = ["blue", "green", "purple", "red", "yellow"]

    const handleChangeImage = (direction) => {
        if (nbImage <= 1) return;

        setIsFading(false);

        setTimeout(() => {
            setImageID((prevID) =>
                direction === 1
                    ? (prevID % nbImage) + 1
                    : prevID === 1 ? nbImage : prevID - 1
            );
            setIsFading(true);
        }, 300);

        clearInterval(intervalRef.current);
        startAutoSlide();
    };

    const startAutoSlide = () => {
        intervalRef.current = setInterval(() => {
            handleChangeImage(1);
        }, 5000);
    };

    useEffect(() => {
        startAutoSlide();
        return () => clearInterval(intervalRef.current);
    }, []);

    return (
        <div className="single-project">
            <div className="single-project-left">
                <button onClick={() => handleChangeImage(-1)} className="arrow preview">{'<'}</button>

                <img
                    src={`/assets/images/${image}/${image}_${imageID}.png`}
                    alt={image}
                    className={isFading ? 'fade-in' : 'fade-out'}
                />

                <button onClick={() => handleChangeImage(1)} className="arrow next">{'>'}</button>
            </div>

            <div className="single-project-right">
                <div className="single-project-right-top">
                    <div className="single-project-right-top-title">
                        <div className={`single-project-line color-${color[(id-1)%color.length]}`}></div>
                        <h3 className="single-project-title">
                            {title}
                            {school ? (
                                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640" fill="#D95F46" width="1.25em" height="1.25em">
                                    <path d="M80 259.8L289.2 345.9C299 349.9 309.4 352 320 352C330.6 352 341 349.9 350.8 345.9L593.2 246.1C602.2 242.4 608 233.7 608 224C608 214.3 602.2 205.6 593.2 201.9L350.8 102.1C341 98.1 330.6 96 320 96C309.4 96 299 98.1 289.2 102.1L46.8 201.9C37.8 205.6 32 214.3 32 224L32 520C32 533.3 42.7 544 56 544C69.3 544 80 533.3 80 520L80 259.8zM128 331.5L128 448C128 501 214 544 320 544C426 544 512 501 512 448L512 331.4L369.1 390.3C353.5 396.7 336.9 400 320 400C303.1 400 286.5 396.7 270.9 390.3L128 331.4z"/>
                                </svg>
                            ) : null}
                        </h3>
                    </div>
                    {endYear === null ? (
                        <p className="single-project-right-top-date">{beginningYear + ' – in progress'}</p>
                    ) : beginningYear === endYear ? (
                            <p className="single-project-right-top-date">{beginningYear}</p>
                    ) : (
                        <p className="single-project-right-top-date">{beginningYear + ' – ' + endYear}</p>
                    )}
                </div>
                <div className="single-project-right-bottom">
                    <p className="single-project-description" style={{ whiteSpace: "pre-line" }}>
                        {description}
                    </p>

                    <ul className="single-project-skills-list">
                        {skills.map((skill) => (
                            <li key={skill}>
                                <SkillCard text={skill} />
                            </li>
                        ))}
                    </ul>

                    <p className="single-project-link">Learn more</p>
                </div>
            </div>
        </div>
    );
}

export default SingleProject;
