import * as React from 'react'
import * as cx from 'classnames'
import { StylableComponent } from '../../theme'
import { TrayItemProps } from '../interfaces'
import { Flex, Row } from '../../flex'
import { Icon } from '../../icon'

const styles = require('../../../../src/components/tray/web/tray-item.scss')

export class TrayItem extends Flex<TrayItemProps>  {

    public static defaultProps: TrayItemProps = {
        ...Row.defaultProps,
        fullWidth: false,
        justify: 'center',
    }

    public container: HTMLDivElement

    public setContainerRef = (e: HTMLDivElement) => this.container = e

    public render(): JSX.Element {
        const {
            id,
            style,
            children,
            icon,
            iconSize,
            text,
            onClick,
            tooltip,
            tooltipPos,
            input,
            selected,
        } = this.props;

        return (
            <div
                id={id}
                ref={this.setContainerRef}
                className={this.classNames()}
                style={style}
                onClick={onClick}
                data-tooltip={tooltip}
                data-tooltip-pos={tooltipPos}>
                {icon !== undefined
                    ? <Icon icon={icon} size={iconSize || (input ? 14 : 18)} />
                    : null}
                {text !== undefined ? <a>{text}</a> : null}
                {children}
            </div>
        )
    }

    protected classNames(): string {
        return cx(super.classNames(), styles.item, {
            [styles.primary]: this.props.primary,
            [styles.secondary]: this.props.secondary,
            [styles.active]: this.props.active,
            [styles.stroke]: this.props.stroke,
            [styles.onClick]: typeof this.props.onClick === 'function',
            [styles.disabled]: this.props.disabled,
            [styles.input]: this.props.input,
            [styles.dynamic]: this.props.dynamic,
            [styles.selected]: this.props.selected,
            [styles.disabledStroke]: this.props.disabled && this.props.stroke,
        })
    }
}
