import { DatePickerInput, DatePickerInputProps } from "@mantine/dates";
import { Checkbox, CheckboxProps, Divider, Stack, Text } from "@mantine/core";
import ClientSelectNew from "../../../../../../components/Selects/ClientSelectNew/ClientSelectNew.tsx";
import {
    BaseMarketplaceSchema,
    BoardSchema,
    ClientSchema,
    ProjectSchema,
    StatusSchema,
    TransactionTagSchema,
    UserSchema,
} from "../../../../../../client";
import { ObjectSelectProps } from "../../../../../../components/ObjectSelect/ObjectSelect.tsx";
import BaseMarketplaceSelect
    from "../../../../../../components/Selects/BaseMarketplaceSelect/BaseMarketplaceSelect.tsx";
import CardStatusSelect from "../../../../../../components/DealStatusSelect/CardStatusSelect.tsx";
import { ProfitTableSegmentedControl } from "../ProfitTableSegmentedControl/ProfitTableSegmentedControl.tsx";
import ManagerSelect from "../../../../../../components/ManagerSelect/ManagerSelect.tsx";
import TransactionTagSelect from "../../../../components/ExpenseTagSelect/TransactionTagSelect.tsx";
import BoardSelect from "../../../../../../components/BoardSelect/BoardSelect.tsx";
import ProjectSelect from "../../../../../../components/ProjectSelect/ProjectSelect.tsx";


type SelectProps<T> = Omit<
    ObjectSelectProps<T>,
    "data" | "getValueFn" | "getLabelFn"
>;

type FiltersProps = {
    datePickerProps?: DatePickerInputProps<"range">;

    clientSelectProps?: Omit<ObjectSelectProps<ClientSchema>, "data">;
    onClientClear?: () => void;

    baseMarketplaceSelectProps?: SelectProps<BaseMarketplaceSchema>;
    onBaseMarketplaceClear?: () => void;

    projectSelectProps?: Omit<ObjectSelectProps<ProjectSchema | null>, "data">;

    boardSelectProps?: Omit<ObjectSelectProps<BoardSchema | null>, "data">;

    statusSelectProps?: Omit<ObjectSelectProps<StatusSchema | null>, "data">;

    managerSelectProps?: SelectProps<UserSchema | null | undefined>;
    onManagerClear?: () => void;

    isCompletedOnlyCheckboxProps?: CheckboxProps;

    expenseTagSelectProps?: SelectProps<TransactionTagSchema | null>;
    onExpenseTagClear?: () => void;

    incomeTagSelectProps?: SelectProps<TransactionTagSchema | null>;
    onIncomeTagClear?: () => void;

    groupTableByProps?: {
        value: string,
        onChange: (value: string) => void,
    };
}

export const Filters = (props: FiltersProps) => {
    const getTransactionTagsSelect = (isIncome: boolean) => {
        const selectProps = isIncome ? props.incomeTagSelectProps : props.expenseTagSelectProps;
        if (!selectProps) return;
        const onClear = isIncome ? props.onIncomeTagClear : props.onExpenseTagClear;
        const label = "Фильтры для " + (isIncome ? "доходов:" : "расходов:");
        return (
            <>
                <Divider />
                <Text>{label}</Text>
                <TransactionTagSelect
                    {...selectProps}
                    onClear={onClear}
                    placeholder={"Выберите тег"}
                    isIncome={isIncome}
                />
            </>
        );
    };

    return (
        <Stack mb={"lg"}>
            <Divider />
            <Text>
                Фильтры для выручки и прибыли:
            </Text>
            {props.datePickerProps &&
                <DatePickerInput
                    {...props.datePickerProps}
                    type="range"
                    placeholder="Выберите даты"
                    maxDate={new Date()}
                    valueFormat={"DD.MM.YYYY"}
                />
            }
            {props.projectSelectProps &&
                <ProjectSelect
                    {...props.projectSelectProps}
                    clearable
                />
            }
            {props.boardSelectProps &&
                <BoardSelect
                    project={props.projectSelectProps?.value ?? null}
                    {...props.boardSelectProps}
                    clearable
                />
            }
            {props.statusSelectProps &&
                <CardStatusSelect
                    board={props.boardSelectProps?.value ?? null}
                    {...props.statusSelectProps}
                    clearable
                />
            }
            {props.clientSelectProps &&
                <ClientSelectNew
                    {...props.clientSelectProps}
                    onClear={props.onClientClear}
                    clearable
                    searchable
                    placeholder={"Выберите клиента"}
                />
            }
            {props.baseMarketplaceSelectProps &&
                <BaseMarketplaceSelect
                    {...props.baseMarketplaceSelectProps}
                    onClear={props.onBaseMarketplaceClear}
                    clearable
                    placeholder={"Выберите маркетплейс"}
                />
            }
            {props.managerSelectProps &&
                <ManagerSelect
                    {...props.managerSelectProps}
                    onClear={props.onManagerClear}
                    placeholder={"Выберите менеджера"}
                />
            }
            <Checkbox
                {...props.isCompletedOnlyCheckboxProps}
                label={"Только завершенные сделки"}
            />
            {getTransactionTagsSelect(false)}
            {getTransactionTagsSelect(true)}
            {props.groupTableByProps &&
                <>
                    <Divider />
                    <Text>Группировка таблицы:</Text>
                    <ProfitTableSegmentedControl
                        selectedProject={props.projectSelectProps?.value ?? null}
                        selectedBoard={props.boardSelectProps?.value ?? null}
                        {...props.groupTableByProps}
                        orientation={"vertical"}
                        size={"md"}
                        w={"100%"}
                    />
                </>
            }
        </Stack>
    );
};