'use client';

import { useEffect, useState } from "react";
import { ITestimony } from "../const/testimony";
import "./styles/testimony.style.css";
import { FaChevronLeft, FaChevronRight } from "react-icons/fa";
import Image from "next/image";

interface ITestimonialProps {
    list: ITestimony[];
}

const Testimonials: React.FC<ITestimonialProps> = ({ list }) => {
    const [current, setCurrent] = useState(0);
    const [marginLeft, setMarginLeft] = useState(0);
    const innerWidth = list.length * 100;

    useEffect(() => {
        const newMarginLeft = current * 100 * -1;
        setMarginLeft(newMarginLeft);
    }, [current]);

    const navigateTo = (slide: number) => {
        setCurrent(slide);
    }

    const onPrevHandler = (): void => {
        if (current == 0) return;
        setCurrent((prev) => prev - 1);
    }

    const onNextHandler = (): void => {
        if (current == (list.length - 1)) return;
        setCurrent((prev) => prev + 1);
    }

    return (
        <div className="testimonial-wrapper">
            <div className="testimonial-arrow">
                <button type="button" disabled={current == 0} onClick={onPrevHandler}>
                    <FaChevronLeft />
                </button>
            </div>
            <div className="testimonial-body">
                <div className="testimonial-body-inner" style={{ width: `${innerWidth}%`, marginLeft: `${marginLeft}%` }}>
                    { list.map((item: ITestimony, index: number) => (
                        <div className="testimony-card" key={`testimony-${index}`}>
                            <div className="testimony-card-inner">
                                <Image src={item.imgPath} alt="image testimony" />
                                <div className="testimony-caption">
                                    <h4>{item.name}</h4>
                                    <label>{item.title}</label>
                                    <p>
                                        {item.content}
                                    </p>
                                </div>
                            </div>
                        </div>
                    ))}
                </div>
                <div className="testimonial-dots">
                    {
                        [...Array(list.length)].map((_, index: number) => (
                            <button
                                key={`dots-${index}`} 
                                type="button" 
                                onClick={() => { navigateTo(index) }}
                                className={current === index ? 'active' : ''}
                            ></button>
                        ))
                    }
                </div>
            </div>
            <div className="testimonial-arrow">
                <button type="button" disabled={current === (list.length - 1)} onClick={onNextHandler}>
                    <FaChevronRight />
                </button>
            </div>
        </div>
    )
}

export default Testimonials;