import mergeToNew from "@insite/client-framework/Common/mergeToNew";
import StyledWrapper from "@insite/client-framework/Common/StyledWrapper";
import setIsSearchPage from "@insite/client-framework/Store/Pages/ProductList/Handlers/SetIsSearchPage";
import translate from "@insite/client-framework/Translate";
import { AutocompleteItemModel } from "@insite/client-framework/Types/ApiModels";
import Link, { LinkPresentationProps } from "@insite/mobius/Link";
import Typography, { TypographyPresentationProps } from "@insite/mobius/Typography";
import InjectableCss from "@insite/mobius/utilities/InjectableCss";
import React, { Fragment } from "react";
import { connect, ResolveThunks } from "react-redux";
import styled, { css } from "styled-components";

interface OwnProps {
    categories: AutocompleteItemModel[] | null;
    popularCategories?: AutocompleteItemModel[] | null;
    focusedItem?: AutocompleteItemModel;
    goToUrl: (url: string) => void;
    extendedStyles?: AutocompleteCategoriesStyles;
    isRetailSearchCompletionResults?: boolean;
}

const mapDispatchToProps = {
    setIsSearchPage,
};

type Props = OwnProps & ResolveThunks<typeof mapDispatchToProps>;

export interface AutocompleteCategoriesStyles {
    headerText?: TypographyPresentationProps;
    subHeaderText?: TypographyPresentationProps;
    categoryItemText?: TypographyPresentationProps;
    divider?: InjectableCss;
    focusedCategoryItemText?: TypographyPresentationProps;
    link?: LinkPresentationProps;
    focusedLink?: LinkPresentationProps;
    autocompleteCategoriesRowWrapper?: InjectableCss;
    autocompleteCategoriesColumnWrapper?: InjectableCss;
}

export const autocompleteCategoriesStyles: AutocompleteCategoriesStyles = {
    headerText: {
        weight: "bold",
        css: css`
            text-align: left;
            margin: 10px 0 5px 0;
        `,
    },
    subHeaderText: {
        weight: 600,
        css: css`
            text-align: left;
            margin: 0 0 5px 0;
        `,
    },
    categoryItemText: {
        ellipsis: true,
        css: css`
            width: 100%;
            text-align: left;
            cursor: pointer;
        `,
    },
    divider: {
        css: css`
            min-width: 100%;
        `,
    },
    focusedCategoryItemText: {
        ellipsis: true,
        css: css`
            width: 100%;
            text-align: left;
            cursor: pointer;
            background-color: lightgray;
        `,
    },
    link: {
        typographyProps: {
            ellipsis: true,
            css: css`
                width: 100%;
                text-align: left;
                margin: 5px 0;
            `,
        },
        css: css`
            width: auto;
            overflow: hidden;
        `,
    },
    focusedLink: {
        typographyProps: {
            ellipsis: true,
            css: css`
                width: 100%;
                text-align: left;
                margin: 5px 0;
            `,
        },
        css: css`
            width: auto;
            overflow: hidden;
            background-color: lightgray;
        `,
    },
    autocompleteCategoriesRowWrapper: {
        css: css`
            display: flex;
            flex-direction: row;
        `,
    },
    autocompleteCategoriesColumnWrapper: {
        css: css`
            display: flex;
            flex-direction: column;
            width: 375px;
        `,
    },
};

const styles = autocompleteCategoriesStyles;

const Divider = styled.hr`
    ${styles.divider?.css}
`;

class AutocompleteCategories extends React.Component<Props> {
    private readonly styles: AutocompleteCategoriesStyles;

    constructor(props: Props) {
        super(props);

        this.styles = mergeToNew(styles, props.extendedStyles);
    }

    renderCategories = (categories: AutocompleteItemModel[]) => {
        const styles = this.styles;
        return (
            <>
                {categories.map(category => (
                    <Fragment key={`${category.id}`}>
                        {category.subtitle && (
                            <Typography
                                {...(this.props.focusedItem === category
                                    ? styles.focusedCategoryItemText
                                    : styles.categoryItemText)}
                                onClick={() => {
                                    this.props.setIsSearchPage(false);
                                    this.props.goToUrl(category.url);
                                }}
                                className="autocomplete-categories-item-text"
                            >
                                <Link {...styles.link} className="autocomplete-categories-item-link">
                                    {category.displayTitle}
                                </Link>
                                <>{` ${translate("in")}`}</> {category.displaySubtitle}
                            </Typography>
                        )}
                        {!category.subtitle && (
                            <Link
                                {...(this.props.focusedItem === category ? styles.focusedLink : styles.link)}
                                onClick={() => {
                                    this.props.setIsSearchPage(false);
                                    this.props.goToUrl(category.url);
                                }}
                                className="autocomplete-categories-link"
                            >
                                {category.displayTitle}
                            </Link>
                        )}
                    </Fragment>
                ))}
            </>
        );
    };

    render() {
        const { categories, popularCategories, isRetailSearchCompletionResults } = this.props;
        if ((!categories || categories.length === 0) && (!popularCategories || popularCategories.length === 0)) {
            return null;
        }

        const styles = this.styles;
        return (
            <>
                <Typography
                    {...styles.headerText}
                    data-test-selector="autocompleteCategories_Header"
                    className="autocomplete-categories-header"
                >
                    {translate("Categories")}
                </Typography>

                {categories &&
                    categories.length > 0 &&
                    !isRetailSearchCompletionResults &&
                    this.renderCategories(categories)}

                {isRetailSearchCompletionResults && (
                    <>
                        <Divider />
                        <StyledWrapper
                            {...this.styles.autocompleteCategoriesRowWrapper}
                            className="autocomplete-categories-row-wrapper"
                        >
                            {popularCategories && popularCategories.length > 0 && (
                                <StyledWrapper
                                    {...this.styles.autocompleteCategoriesColumnWrapper}
                                    className="autocomplete-categories-column-wrapper"
                                >
                                    <Typography
                                        {...styles.subHeaderText}
                                        data-test-selector="autocompleteCategories_subHeader"
                                        className="autocomplete-categories-subheader"
                                    >
                                        {translate("popular")}
                                    </Typography>
                                    {this.renderCategories(popularCategories)}
                                </StyledWrapper>
                            )}
                            {categories && categories.length > 0 && (
                                <StyledWrapper
                                    {...this.styles.autocompleteCategoriesColumnWrapper}
                                    className="autocomplete-categories-column-wrapper"
                                >
                                    <Typography
                                        {...styles.subHeaderText}
                                        data-test-selector="autocompleteCategories_subHeader"
                                        className="autocomplete-categories-subheader"
                                    >
                                        {translate("suggested")}
                                    </Typography>
                                    {this.renderCategories(categories)}
                                </StyledWrapper>
                            )}
                        </StyledWrapper>
                    </>
                )}
            </>
        );
    }
}

export default connect(null, mapDispatchToProps)(AutocompleteCategories);
