import React, { useCallback, useRef, useState } from "react";

import classNames from "classnames";
import compose from "ramda/src/compose";

import { Job } from "@enact/core/util";
import { Marquee, MarqueeController } from "@enact/sandstone/Marquee";
import Spottable from "@enact/spotlight/Spottable";

import css from "./TabItem.module.less";

const SpottableComponent = Spottable("div");
const TabItemBase = ({
  icons,
  title,
  expanded = false,
  mainSelected = false,
  selected = false,
  index = 0,
  target,
  deActivateTab,
  onClick,
  isSubItem,
  onFocus,
  showSubTab = false,
  mainSelectedIndex,
  ...rest
}) => {
  const [focused, setFocused] = useState(false);
  const clearPressedJob = useRef(
    new Job((func) => {
      setTimeout(func, 100);
    }, 100)
  );
  const _onClick = useCallback(
    (ev) => {
      clearPressedJob.current.start(() => {
        if (onClick) {
          onClick({ index, target });
        }
      });
    },
    [target, index, onClick]
  );

  const _onFocus = useCallback(() => {
    setFocused(true);
    if (onFocus) {
      onFocus(index);
    }
  }, [index, showSubTab, onFocus]);

  const _onBlur = useCallback(() => {
    setFocused(false);
  }, [showSubTab]);

  const onKeyDown = useCallback(
    (event) => {
      if (event.key === "ArrowRight") {
        if (!showSubTab) {
          _onClick();
        }
      }
    },
    [deActivateTab]
  );

  const renderIcon = useCallback(() => {
    if (!icons) {
      return null;
    }

    const Component = icons;
    let iconType = "normal";

    if (expanded && index === mainSelectedIndex) {
      iconType = "focused";
    } else if (selected) {
      iconType = "selected";
    } else if (mainSelected) {
      iconType = "selected";
    } else if (focused) {
      iconType = "focused";
    } else if (expanded) {
      iconType = "expanded";
    }

    return <Component iconType={iconType} />;
  }, [
    focused,
    expanded,
    mainSelected,
    mainSelectedIndex,
    index,
    icons,
    selected,
  ]);

  delete rest.hasChildren;
  delete rest.getChildren;
  return (
    <SpottableComponent
      className={classNames(
        css.tabItem,
        (focused || index === mainSelectedIndex) && css.focused,
        !isSubItem &&
          (focused || index === mainSelectedIndex) &&
          showSubTab &&
          css.arrow,
        mainSelected && css.selected,
        selected && css.selected
      )}
      onKeyDown={onKeyDown}
      onFocus={_onFocus}
      onBlur={_onBlur}
      onClick={_onClick}
    >
      <div
        className={classNames(
          css.itemWrap,
          (focused || index === mainSelectedIndex) && css.focused
        )}
      >
        {icons && <div className={css.icon}>{renderIcon()}</div>}
        {expanded && title && (
          <Marquee
            marqueeDisabled={!focused}
            className={classNames(css.text, isSubItem && css.subItem)}
            marqueeOn={"focus"}
          >
            {title}
          </Marquee>
        )}
      </div>
    </SpottableComponent>
  );
};
const ItemDecorator = compose(MarqueeController({ marqueeOnFocus: true }));
const TabItem = ItemDecorator(TabItemBase);

export default TabItem;
