import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { useForm } from 'react-hook-form';
import { useQuery } from '@apollo/client';
import InputSearch from '../input-search/InputSearch';
import { createNamespace } from '../../tools/namingTools';
import { getLocalizionString } from '../../tools/stringTools';
import { imageUploaderShape } from '../../constants/schema/prop-types/image-uploader';
import withGraphQLGeneric from '../../components/withGraphQLGeneric';
import { PUBLISHED_PRODUCT_SEARCH_SUGGEST } from './query/querySuggestions';
import useDebounce from '../../tools/hooks/useDebounce';
// IMPORTANT: (s)css should be imported after other atoms/components
import './styles/SearchBarV2.scss';

const SearchBarV2 = ({
    n,
    searchValue,
    inputName,
    label,
    onSearch,
    height,
    submitLabel,
    hiddenLabel,
    searchPlaceholderTextColor,
    searchBackgroundColor,
    searchBorderColor,
    searchCornerRadius,
    searchBorderVisible,
    submitIconOnly,
    searchIconColor,
    searchButtonColor,
    searchIcon,
    searchPlaceholder,
    searchBarMinWidth,
    searchBarMaxWidth,
    hasBoxShadow,
    extraClass,
    parentTestId,
    locale
}) => {
    const { handleSubmit, register } = useForm();
    const [searchQuery, setSearchQuery] = useState();
    const debouncedSearchTerm = useDebounce(searchQuery, 250);

    const styleVariables = {
        'min-width': searchBarMinWidth && `${searchBarMinWidth}px`,
        'max-width': searchBarMaxWidth && `${searchBarMaxWidth}px`
    };

    const { error, data } = useQuery(PUBLISHED_PRODUCT_SEARCH_SUGGEST, {
        variables: {
            locale,
            queryString: debouncedSearchTerm
        },
        skip: !debouncedSearchTerm
    });

    const onChangeHandler = query => {
        setSearchQuery(query.target.value);
    };

    return (
        <div {...n('wrapper')
            .withVariables(styleVariables)
            .withClass(extraClass)
            .withTestId(parentTestId)
            .props}
        >
            <form {...n('form').props} onSubmit={handleSubmit(onSearch)}>
                <InputSearch
                    name={inputName}
                    label={label}
                    placeholder={searchPlaceholder}
                    hiddenLabel={hiddenLabel}
                    withBorder={searchBorderVisible}
                    borderRadius={searchCornerRadius}
                    borderColor={searchBorderColor}
                    backgroundColor={searchBackgroundColor}
                    searchPlaceholderTextColor={searchPlaceholderTextColor}
                    inputRef={register({})}
                    submitIconOnly={submitIconOnly}
                    submitIconColor={searchIconColor}
                    submitIconUrl={searchIcon?.url}
                    submitIconAlt={getLocalizionString(searchIcon?.alt) || ''}
                    submitBgColor={searchButtonColor}
                    submitLabel={submitLabel}
                    height={height}
                    boxShadow={hasBoxShadow}
                    defaultValue={searchValue}
                    required={false}
                    onChange={onChangeHandler}
                    suggestionsData={{
                        data,
                        error
                    }}
                    locale={locale}
                />
            </form>
        </div>
    );
};

SearchBarV2.propTypes = {
    n: PropTypes.func,
    inputName: PropTypes.string,
    label: PropTypes.string,
    searchValue: PropTypes.string,
    onSearch: PropTypes.func,
    height: PropTypes.number,
    submitLabel: PropTypes.string,
    hiddenLabel: PropTypes.bool,
    searchPlaceholderTextColor: PropTypes.string,
    searchBackgroundColor: PropTypes.string,
    searchBorderColor: PropTypes.string,
    searchCornerRadius: PropTypes.number,
    searchBorderVisible: PropTypes.bool,
    submitIconOnly: PropTypes.bool,
    searchIconColor: PropTypes.string,
    searchButtonColor: PropTypes.string,
    searchIcon: PropTypes.shape(imageUploaderShape),
    searchPlaceholder: PropTypes.string,
    searchBarMinWidth: PropTypes.number,
    searchBarMaxWidth: PropTypes.number,
    hasBoxShadow: PropTypes.bool,
    extraClass: PropTypes.string,
    parentTestId: PropTypes.string,
    locale: PropTypes.string
};

SearchBarV2.defaultProps = {
    n: createNamespace('SearchBarV2'),
    inputName: 'inputSearch',
    label: 'Search',
    searchValue: null,
    onSearch: () => { },
    height: 40,
    submitLabel: 'Submit',
    hiddenLabel: true,
    searchPlaceholderTextColor: null,
    searchBackgroundColor: '#ffffff',
    searchBorderColor: '#cbcbcb',
    searchCornerRadius: 24,
    searchBorderVisible: true,
    submitIconOnly: true,
    searchIconColor: '#ffffff',
    searchButtonColor: '#0076ce',
    searchIcon: {},
    searchPlaceholder: 'Search',
    searchBarMinWidth: 225,
    searchBarMaxWidth: 500,
    hasBoxShadow: false,
    extraClass: null,
    parentTestId: null,
    locale: 'en-US'
};

export default withGraphQLGeneric(SearchBarV2);
