import {useDispatch, useSelector} from "react-redux";
import {getCachedInfo} from "../../../Utils/cachedInfoUtils.js";
import {
    closeModal,
    openModal,
    selectIssue,
    setCurrentPage,
    setEndFilterDate,
    setPageSize,
    setSearchText,
    setStartFilterDate,
    setViewMode
} from "../../../Redux/Slices/lensIssuesSlice.js";
import {useEffect, useMemo} from "react";
import dayjs from "dayjs";


const useIssuesUI = (issues) => {
    const dispatch = useDispatch();
    const {
        searchText,
        currentPage,
        pageSize,
        selectedIssue,
        isModalVisible,
        viewMode,
        startFilterDate,
        endFilterDate,
    } = useSelector(state => state.lensIssuesUI);


    useEffect(() => {
        document.title = "Выдача линз";
        const cachedViewMode = getCachedInfo("viewModeIssues");
        if (cachedViewMode) dispatch(setViewMode(cachedViewMode));
    }, [dispatch])

    const startFilterDateConverted = startFilterDate ? dayjs(startFilterDate) : null;
    const endFilterDateConverted = endFilterDate ? dayjs(endFilterDate) : null;
    const filterDates = [startFilterDateConverted, endFilterDateConverted];
    const isFilterDates = startFilterDate && endFilterDate;

    const containerStyle = { padding: 20 };
    const filterBarStyle = { marginBottom: 20 };
    const formItemStyle = { width: "100%" };
    const viewModIconStyle = { marginRight: 8 };
    const advancedSearchCardStyle = {
        marginBottom: 20,
        boxShadow: "0 1px 6px rgba(0, 0, 0, 0.15)",
        borderRadius: 8
    };

    const handleSetSearchText = (value) => dispatch(setSearchText(value));
    const handleSetViewMode = (mode) => dispatch(setViewMode(mode));
    const handleSetCurrentPage = (page) => dispatch(setCurrentPage(page));
    const handleSetPageSize = (size) => dispatch(setPageSize(size));
    const handleCloseModal = () => dispatch(closeModal());
    const handleSelectIssue = (issue) => dispatch(selectIssue(issue));
    const resetSelectedIssue = () => dispatch(selectIssue(null));

    const handleAddIssue = () => {
        dispatch(selectIssue(null));
        dispatch(openModal());
    };

    const handlePaginationChange = (page, pageSize) => {
        handleSetCurrentPage(page);
        handleSetPageSize(pageSize);
    };

    const handleFilterDateChange = (dates) => {
        if (dates) {
            const [start, end] = dates;
            dispatch(setStartFilterDate(start.toISOString()));
            dispatch(setEndFilterDate(end.toISOString()));
        }
    };

    const handleResetFilterDate = () => {
        dispatch(setStartFilterDate(null));
        dispatch(setEndFilterDate(null));
    };

    const filteredIssues = useMemo(() => {
        return issues.filter(issue => {
            let dateFilter = true;

            if (startFilterDateConverted && endFilterDateConverted) {
                const issueDate = dayjs(issue.issue_date);

                dateFilter = issueDate.isAfter(startFilterDateConverted) && issueDate.isBefore(endFilterDateConverted);
            }

            return (
                (
                    issue.patient.last_name.toLowerCase().includes(searchText) ||
                    issue.patient.first_name.toLowerCase().includes(searchText) ||
                    issue.doctor.last_name.toLowerCase().includes(searchText) ||
                    issue.doctor.first_name.toLowerCase().includes(searchText)
                ) &&
                dateFilter
            )
        });
    }, [issues, searchText, startFilterDateConverted, endFilterDateConverted]);

    const pagination = {
        current: currentPage,
        pageSize: pageSize,
        showSizeChanger: true,
        pageSizeOptions: ["5", "10", "20", "50"],
        onChange: (page, newPageSize) => {
            setCurrentPage(page);
            setPageSize(newPageSize);
        },
    };

    return {
        filteredIssues,
        pagination,
        searchText,
        selectedIssue,
        isModalVisible,
        viewMode,
        currentPage,
        pageSize,
        filterDates,
        isFilterDates,
        containerStyle,
        filterBarStyle,
        formItemStyle,
        viewModIconStyle,
        advancedSearchCardStyle,
        handleAddIssue,
        handlePaginationChange,
        handleSetSearchText,
        handleSetViewMode,
        handleSetCurrentPage,
        handleSetPageSize,
        handleCloseModal,
        handleFilterDateChange,
        resetSelectedIssue,
        handleSelectIssue,
        handleResetFilterDate,
    };
};

export default useIssuesUI;