import React from 'react';

import {Alert, spacing} from '@gravity-ui/uikit';
import {I18n} from 'i18n';
import {useHistory, useLocation} from 'react-router';
import type {DatasetField, DatasetOptions, ObligatoryFilter} from 'shared';
import {FiltersQA} from 'shared';
import type {ApplyData} from 'ui';
import {DatasetTableFieldsSection} from 'ui/units/datasets/components/DatasetTableFieldsSection/DatasetTableFieldsSection';

import type {FilterType} from './types';
import {useFilterSection} from './useFilterSection';

const i18n = I18n.keyset('dataset.filters-tab.modify');
const i18nSharedEntry = I18n.keyset('shared-entry');

interface FilterSectionProps {
    type: FilterType;
    fields: DatasetField[];
    filters: ObligatoryFilter[];
    options: Partial<DatasetOptions>;
    progress: boolean;
    addFilter: (data: ApplyData) => void;
    updateFilter: (data: ApplyData) => void;
    deleteFilter: (filterId: string) => void;
    readonly: boolean;
}

const FilterSection: React.FC<FilterSectionProps> = (props: FilterSectionProps) => {
    const {progress, filters, updateFilter, fields, options, addFilter, deleteFilter, readonly} =
        props;
    const history = useHistory();
    const location = useLocation();
    const {
        headerColumns,
        onFilterItemClick,
        onOpenDialogFilterClick,
        columns,
        preparedFields,
        controlSettings,
        checkIsRowValid,
    } = useFilterSection({
        filters,
        fields,
        options,
        updateFilter,
        addFilter,
        deleteFilter,
        readonly,
    });

    const title = i18n('label_obligatory-filter-title');
    const description = i18n('label_obligatory-filter-description');
    const alertTitle = preparedFields.length > 0 ? undefined : i18n('filters-readonly-alert-title');
    const alertMessage = i18n(
        preparedFields.length > 0
            ? 'filters-readonly-alert-message'
            : 'empty-filters-readonly-alert-message',
    );

    return (
        <DatasetTableFieldsSection
            readonlyNotice={
                progress ? null : (
                    <Alert
                        className={spacing({mb: '3'})}
                        theme="info"
                        title={alertTitle}
                        message={alertMessage}
                        actions={[
                            {
                                text: i18nSharedEntry('workbook-shared-entry-original-link'),
                                handler: () => history.push(location.pathname),
                            },
                        ]}
                        layout="horizontal"
                    />
                )
            }
            readonly={readonly}
            title={title}
            description={description}
            onConfirmClick={onOpenDialogFilterClick}
            confirmButtonText={i18n('label_obligatory-filter-add')}
            onItemClick={onFilterItemClick}
            fields={preparedFields}
            headerColumns={headerColumns}
            columns={columns}
            isLoading={progress}
            controlSettings={controlSettings}
            checkIsRowValid={checkIsRowValid}
            qa={FiltersQA.FiltersTabSection}
        />
    );
};

export default FilterSection;
