import React, { useEffect } from 'react';
import AnimatedSVG, { ICONS } from "Shared/AnimatedSVG/AnimatedSVG";
import FilterList from 'Shared/Filters/FilterList';
import FilterSelection from 'Shared/Filters/FilterSelection';
import SaveFilterButton from 'Shared/SaveFilterButton';
import { connect } from 'react-redux';
import { FilterKey } from 'Types/filter/filterType';
import { addOptionsToFilter } from 'Types/filter/newFilter';
import { Button, Loader } from 'UI';
import { edit, addFilter, fetchSessions, updateFilter } from 'Duck/search';
import { observer } from 'mobx-react-lite';
import { useStore } from 'App/mstore';
import { debounce } from 'App/utils';
import useSessionSearchQueryHandler from 'App/hooks/useSessionSearchQueryHandler';
import { refreshFilterOptions } from 'Duck/search';

let debounceFetch: any = () => {};

interface Props {
  appliedFilter: any;
  edit: typeof edit;
  addFilter: typeof addFilter;
  saveRequestPayloads: boolean;
  metaLoading?: boolean;
  fetchSessions: typeof fetchSessions;
  updateFilter: typeof updateFilter;
  refreshFilterOptions: typeof refreshFilterOptions;
}

function SessionSearch(props: Props) {
  const { tagWatchStore, aiFiltersStore, projectsStore } = useStore();
  const saveRequestPayloads = projectsStore.instance?.saveRequestPayloads ?? false
  const { appliedFilter, metaLoading = false } = props;
  const hasEvents = appliedFilter.filters.filter((i: any) => i.isEvent).size > 0;
  const hasFilters = appliedFilter.filters.filter((i: any) => !i.isEvent).size > 0;

  useSessionSearchQueryHandler({
    appliedFilter,
    applyFilter: props.updateFilter,
    loading: metaLoading,
    onBeforeLoad: async () => {
      const tags = await tagWatchStore.getTags();
      if (tags) {
        addOptionsToFilter(
          FilterKey.TAGGED_ELEMENT,
          tags.map((tag) => ({
            label: tag.name,
            value: tag.tagId.toString(),
          }))
        );
        props.refreshFilterOptions();
      }
    },
  });

  useEffect(() => {
    debounceFetch = debounce(() => props.fetchSessions(), 500);
  }, []);

  const onAddFilter = (filter: any) => {
    props.addFilter(filter);
  };

  const onUpdateFilter = (filterIndex: any, filter: any) => {
    const newFilters = appliedFilter.filters.map((_filter: any, i: any) => {
      if (i === filterIndex) {
        return filter;
      } else {
        return _filter;
      }
    });

    props.updateFilter({
      ...appliedFilter,
      filters: newFilters,
    });

    debounceFetch();
  };

  const onFilterMove = (newFilters: any) => {
    props.updateFilter({
      ...appliedFilter,
      filters: newFilters,
    });

    debounceFetch();
  }

  const onRemoveFilter = (filterIndex: any) => {
    const newFilters = appliedFilter.filters.filter((_filter: any, i: any) => {
      return i !== filterIndex;
    });

    props.updateFilter({
      filters: newFilters,
    });

    debounceFetch();
  };

  const onChangeEventsOrder = (e: any, { value }: any) => {
    props.updateFilter({
      eventsOrder: value,
    });

    debounceFetch();
  };

  const showPanel = hasEvents || hasFilters || aiFiltersStore.isLoading;
  return !metaLoading ? (
    <>
      {showPanel ? (
        <div className="border bg-white rounded-lg mt-4">
          <div className="p-5">
            {aiFiltersStore.isLoading ? (
              <div className={'font-semibold flex items-center gap-2 mb-2'}>
                <AnimatedSVG name={ICONS.LOADER} size={18} />
                <span>Translating your query into search steps...</span>
              </div>
            ) : null}
            {hasEvents || hasFilters ? (
              <FilterList
                filter={appliedFilter}
                onUpdateFilter={onUpdateFilter}
                onRemoveFilter={onRemoveFilter}
                onChangeEventsOrder={onChangeEventsOrder}
                onFilterMove={onFilterMove}
                saveRequestPayloads={saveRequestPayloads}
              />
            ) : null}
          </div>

          {hasEvents || hasFilters ? (
            <div className="border-t px-5 py-1 flex items-center -mx-2">
              <div>
                <FilterSelection filter={undefined} onFilterClick={onAddFilter}>
                  <Button variant="text-primary" className="mr-2" icon="plus">
                    ADD STEP
                  </Button>
                </FilterSelection>
              </div>
              <div className="ml-auto flex items-center">
                <SaveFilterButton />
              </div>
            </div>
          ) : null}
        </div>
      ) : (
        <></>
      )}
    </>
  ) : null;
}

export default connect(
  (state: any) => ({
    appliedFilter: state.getIn(['search', 'instance']),
    metaLoading: state.getIn(['customFields', 'fetchRequestActive', 'loading']),
  }),
  { edit, addFilter, fetchSessions, updateFilter, refreshFilterOptions }
)(observer(SessionSearch));
