import styles from "./ToolBar.module.css"
import Filter from "../Filter/Filter.jsx";
import {useContext, useEffect, useState} from "react";
import SearchBar from "../SearchBar/SearchBar.jsx";
import CreateEventBtn from "../createEventBtn/CreateEventBtn.jsx";
import Button from "../ui/button/button.jsx";
import {AuthContext} from "../../contexts/auth/AuthContext.js";

function ToolBar({setFilters, filters, showCreate = true}) {
    const { user } = useContext(AuthContext);
    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 && user.isAdmin ? (
                    <CreateEventBtn />
                ) : null}

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

                <div className={`${styles.filterContainer} ${isFilterVisible ? "glassCard" : ""}`}>
                    <Button className={`${isFilterVisible ? "" : styles.mobileFilter} ${styles.sortButton} glassCard`} variant={"default"} 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;