import styles from "./SearchBar.module.css"
import {Link} from "react-router";
import {useEffect, useState} from "react";
import searchEvents from "../../utils/searchEvents.js";
import searchUsers from "../../utils/searchUsers.js";
import { useLocation } from "react-router";


function SearchBar() {
    const [searchQuery, setSearchQuery] = useState('');
    const [searchResults, setSearchResults] = useState([]);
    const location = useLocation();

    useEffect(() => {
        const performSearch = async (query) => {
            if (query.trim() === '') {
                setSearchResults([]);
                return;
            }

            let data;

            if (location.pathname.includes("events")) {
                data = await searchEvents(query);
            } else if (location.pathname.includes("volunteers")) {
                data = await searchUsers(query);
            }


            if (data) {
                setSearchResults(data);
            } else {
                setSearchResults([]);
            }
        };


        const handler = setTimeout(() => {
            performSearch(searchQuery);
        }, 300);

        return () => {
            clearTimeout(handler);
        };
    }, [searchQuery]);

    return (
        <div className={`${styles.searchBarContainer} glassCard`}>
            <form className={styles.searchBarForm} onSubmit={(element) => element.preventDefault()}>
                <img src="search.svg" alt="Rechercher"/>
                <input
                    type="text"
                    placeholder="Rechercher un événement"
                    value={searchQuery}
                    onChange={(element) => setSearchQuery(element.target.value)}
                />
            </form>

            <div className={styles.lineContainer}>
                {searchResults.length > 0 ? (
                    <div className={styles.line}></div>
                ) : null}
            </div>

            <div className={styles.elementList}>
                {searchResults.map((result, i) => (
                    (location.pathname.includes("events")) ? (
                        <Link to={`/event/` + result.id}>
                            <div className={styles.searchedEvent} key={i}>
                                <div className={`${styles.eventImageDiv}`}>
                                    {result.image == null ? (
                                        <img src="empty.png" className={styles.eventImage} alt="Pas de photo de l'événement" />
                                    ) : (
                                        <img src={result.image + `.png`} alt="Photo de l'événement" />
                                    )}
                                </div>
                                <p>{result.name}</p>
                            </div>
                        </Link>
                    ) : location.pathname.includes("volunteers") ? (
                        <Link to={`/profile/` + result.id}>
                            <div className={styles.searchedEvent} key={i}>
                                <div className={`${styles.eventImageDiv}`}>
                                    {result.image == null ? (
                                        <img src="empty.png" className={styles.eventImage} alt="Pas de photo de l'événement" />
                                    ) : (
                                        <img src={result.image + `.png`} alt="Photo de l'événement" />
                                    )}
                                </div>
                                <p>{result.name} {result.lastname}</p>
                            </div>
                        </Link>
                    ) : null
                ))}
            </div>
        </div>
    );
}

export default SearchBar;