import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { modifierCSS } from '../../../helpers';
import { Icon } from '../icon/Icon';
import { Tooltip } from '../tooltip/Tooltip';

const AVAILABLE = 'available';
const DEPRECATED = 'deprecated';
const DISABLED = 'disabled';
const MISSING_ELEMENTS = 'missing-elements';

const propTypes = {
  /**
   * The TechnologyBadge content
   */
  children: PropTypes.node,

  /**
   * The className property allows you to add more classes to the component.
   */
  className: PropTypes.string,

  /**
   * The component default class.
   */
  defaultClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),

  /**
   * The TechnologyBadge icon name.
   */
  icon: PropTypes.string,

  /**
   * The TechnologyBadge icon url.
   */
  iconUrl: PropTypes.string,

  /**
   * The TechnologyBadge label.
   */
  label: PropTypes.string,

  /**
   * The TechnologyBadge size
   */
  size: PropTypes.oneOf(['', 'sm', 'lg', 'xl']),

  /**
   * The TechnologyBadge elevation.
   */
  shadow: PropTypes.oneOf(['none', '100', '200', '300', '400', '500', '600', 100, 200, 300, 400, 500, 600]),

  /**
   * The TechnologyBadge display status.
   */
  status: PropTypes.oneOf([AVAILABLE, DEPRECATED, DISABLED, MISSING_ELEMENTS]),

  /**
   * The TechnologyBadge padding around the icon.
   */
  isPadded: PropTypes.bool,

  /**
   * The component used for the root node.
   * Either a string to use a DOM element or a component.
   */
  tag: PropTypes.elementType,
};

const defaultProps = {
  children: '',
  className: '',
  defaultClassName: 'sui-a-technology-badge',
  icon: '',
  iconUrl: null,
  label: '',
  size: '',
  shadow: '100',
  isPadded: false,
  status: 'available',
  tag: 'div',
};

export const TechnologyBadge = React.forwardRef(({
  children,
  className,
  defaultClassName,
  icon,
  iconUrl,
  label,
  size,
  shadow,
  status,
  isPadded,
  tag: Tag,
  ...attributes
}, ref) => {
  const statusToClass = () => {
    switch (status) {
    case DEPRECATED:
      return 'as--deprecated';
    case DISABLED:
      return 'as--disabled';
    case MISSING_ELEMENTS:
      return 'as--missing-elements';
    case AVAILABLE:
    default:
      return '';
    }
  };

  const classes = classnames(
    defaultClassName,
    modifierCSS(size),
    modifierCSS(shadow, 'shadow'),
    icon.startsWith('fa-') ? 'as--fa' : '',
    isPadded ? 'as--padded' : '',
    className,
    statusToClass()
  );

  const renderComponent = () => {
    if (iconUrl) {
      return <img src={iconUrl} alt={label} className={status !== AVAILABLE ? 'sui-a-technology-badge__disabled' : ''} />;
    }

    if (icon) {
      return <Icon name={icon} isSvgColor description={label} className={status !== AVAILABLE ? 'sui-a-technology-badge__disabled' : ''} />;
    }

    return children;
  };

  return (
    <Tag {...attributes} className={classes} ref={ref}>

      {
        status === DEPRECATED ? (
          <Tooltip defaultPlacement="top" className="stc-a-tooltip" label="This technology is deprecated.">
            {renderComponent()}
          </Tooltip>
        ) : (
          renderComponent()
        )
      }

    </Tag>
  );
});

TechnologyBadge.propTypes = propTypes;
TechnologyBadge.defaultProps = defaultProps;
