import styles from "./ToolBar.module.css"
import Filter from "../Filter/Filter.jsx";
import {useEffect, useState} from "react";
import SearchBar from "../SearchBar/SearchBar.jsx";
import CreateEventBtn from "../createEventBtn/CreateEventBtn.jsx";


function ToolBar({setFilters, filters, showCreate = true}) {
    const [isFilterVisible, setIsFilterVisible] = useState(false);
    const [isSearchVisible, setIsSearchVisible] = useState(false);
    const toggleFilters = () => {
        setIsFilterVisible(prev => !prev);
    };


    useEffect(() => {
        if (isSearchVisible) {
            document.documentElement.style.overflow = 'hidden';
        } else {
            document.documentElement.style.overflow = '';
        }

        return () => {
            document.documentElement.style.overflow = '';
        };
    }, [isSearchVisible]);



    return (
        <div>
            {isSearchVisible ? (
                <div className={styles.searchBarContainer}>
                    <div className={styles.searchBarOverlay} onClick={() => setIsSearchVisible(false)}></div>
                    <SearchBar/>
                </div>
            ) : null}
            <div className={styles.eventsButtons}>
                {showCreate ? (
                    <CreateEventBtn />
                ) : null}

                <button className={`${styles.searchButton} glassCard`} onClick={() => setIsSearchVisible(true)}>
                    <img src="search.svg" alt="Rechercher"/>
                </button>

                <div className={`${styles.filterContainer} ${isFilterVisible ? "glassCard" : ""}`}>
                    <button className={`${isFilterVisible ? "" : styles.mobileFilter} ${styles.sortButton} glassCard`} onClick={toggleFilters}>
                        <img src="filter.svg" alt="Filtrer"/>
                    </button>
                    <div className={styles.filterMenu}>
                        <Filter isFilterVisible={isFilterVisible} filters={filters} setFilters={setFilters}/>
                    </div>
                </div>
            </div>
        </div>
    );
}

export default ToolBar;