import React from 'react';
import { FormControl, InputGroup, ListGroup } from 'react-bootstrap';
import { deepClone } from '../../../lib/helpers/objectMethods';
import { ISelection } from './interfaces/ISelection';
import { ISelectionItem } from './interfaces/ISelectionItem';

export interface SelectionProps<T> {
    selection: ISelection<T>;
    onItemSelected?: (item: T) => void;
    allowUserSelect?: boolean;
}

export interface LayoutState<T> {
    selected: string | number;
    search: string;
}

class Selection<T> extends React.Component<SelectionProps<T>, LayoutState<T>> {

    private _currentId = 0;

    _generateId(){
        const id = this._currentId;
        this._currentId += 1;
        return id;
    }

    constructor(props) {
        super(props);
        this.state = {
            search: '',
            selected: null
        }
    }

    /**
     * Function to Filter the Selection.
     * @param search The Search Term.
     */
    protected _filter(search: string) {
        this.setState({
            search: search.toLocaleLowerCase(),
            selected: this.state.selected
        });
    }

    /**
     * Function used to Select an item
     * @param item which will be selected 
     */
    protected _select(item: ISelectionItem<T>) {
        console.log('select item',item)
        this.setState({
            search: this.state.search,
            selected: this.props.allowUserSelect ? item.id : -1
        });

        if (this.props.allowUserSelect && typeof this.props.onItemSelected === "function") {
            this.props.onItemSelected(item.template);
        }
    }

    public render() {
        const _this = this;

        // Filter only Valid items.
        // Therefore iterate over the Groups and
        // Filter the Items with the Search String
        // and there Labels / Keywords.
        // Afterwards filter the groups and remove
        // Empty Groups
        const items = deepClone(this.props).selection.map(item => {

            if ((item as {
                groupName: string,
                items: ISelectionItem<T>[]
            }).items !== undefined) {

                // Filter the Items, that match the Selection
                (item as {
                    groupName: string,
                    items: ISelectionItem<T>[]
                }).items = (item as {
                    groupName: string,
                    items: ISelectionItem<T>[]
                }).items.map(item => {
                    item.id = _this._generateId();
                    return item;
                }).filter(value =>
                    // Filter the Keywords
                    value.keywords.toLowerCase().includes(_this.state.search) ||
                    // Filter the Lable.
                    value.label.toLowerCase().includes(_this.state.search)
                );
            } else {
                (item as any).id = _this._generateId();
            }

            // Return the Item
            return item;
        }).filter(
            item => {
                // Filter the Element by the lenght of the contained elements.
                if ((item as {
                    groupName: string,
                    items: ISelectionItem<T>[]
                }).items !== undefined) {
                    // Filter the Keywords
                    return (item as {
                        groupName: string,
                        items: ISelectionItem<T>[]
                    }).items.length > 0;
                } else {
                    // Filter the Keywords
                    return (item as ISelectionItem<T>).keywords.toLowerCase().includes(_this.state.search) ||
                        // Filter the Lable.
                        (item as ISelectionItem<T>).label.toLowerCase().includes(_this.state.search)
                }
            }

        );

        let idx = 0;
        return (<>
            {/* A Search Field. */}
            {this.props.allowUserSelect ?
                <InputGroup size="sm" className="mb-3">
                    <InputGroup.Prepend>
                        <InputGroup.Text id="inputGroup-sizing-sm" >Search</InputGroup.Text>
                    </InputGroup.Prepend>
                    <FormControl aria-label="Small" aria-describedby="inputGroup-sizing-sm" onChange={(event) => {
                        _this._filter(event.currentTarget.value);
                    }} />
                </InputGroup> : ''
            }
            {
                items.length > 0 ?
                    items.map(item => {
                        idx++;

                        if ((item as {
                            groupName: string,
                            items: ISelectionItem<T>[]
                        }).items !== undefined) {
                            return (
                                <div key={idx}>
                                    <hr />
                                    {(item as {
                                        groupName: string,
                                        items: ISelectionItem<T>[]
                                    }).groupName}
                                    <hr />
                                    <ListGroup>
                                        {(item as {
                                            groupName: string,
                                            items: ISelectionItem<T>[]
                                        }).items.map(template => {
                                            // Raise the ID
                                            idx++;
                                            // Store the Item Index
                                            const idxOfItem = idx;

                                            // Based on the selected Item assign the Color
                                            // dark = selected
                                            // light = unselected
                                            const variant = _this.props.allowUserSelect ?
                                                (template.id === _this.state.selected ? "dark" : "light") :
                                                template.color || 'light';

                                            // Return the Template variant={variant}
                                            return (
                                                <ListGroup.Item key={idxOfItem} action variant={variant} onClick={e => {
                                                    _this._select(template)
                                                }}>{template.label}</ListGroup.Item>
                                            );
                                        })}
                                    </ListGroup>
                                </div>
                            );
                        } else {
                            // Store the Item Index
                            const idxOfItem = idx;

                            // Based on the selected Item assign the Color
                            // dark = selected
                            // light = unselected
                            const variant = _this.props.allowUserSelect ?
                                ((item as ISelectionItem<T>).id === _this.state.selected ? "dark" : "light") :
                                (item as ISelectionItem<T>).color || 'light';
                            // Return the Template variant={variant}
                            return (
                                <ListGroup.Item key={idxOfItem} action variant={variant} onClick={e => {
                                    _this._select(item as ISelectionItem<T>)
                                }}>{(item as ISelectionItem<T>).label}</ListGroup.Item>
                            )
                        }
                    }) :
                    <code>Nothing Found</code>
            }
        </>);
    }
}

export default Selection;