import React, { ReactChild } from "react";
import { View, StyleSheet } from "react-native";

export type ItemProps = {
  backgroundColor: string;
  focusedBackgroundColor: string;
  selectedBackgroundColor?: string;
  focusectedBackgroundColor?: string;
  borderRadius: number;
  marginBottom: number;
  marginLeft: number;
  marginRight: number;
  marginTop: number;
  paddingTop: number;
  paddingBottom: number;
  paddingRight: number;
  paddingLeft: number;
  focused?: boolean;
  selected?: boolean;
  children?: ReactChild[];
};

const styles = StyleSheet.create({
  container: {
    flexDirection: "row",
    justifyContent: "space-between",
  },
});

function getBackgroundColor({
  selected,
  focused,
  backgroundColor,
  focusedBackgroundColor,
  selectedBackgroundColor,
  focusectedBackgroundColor,
}) {
  switch (true) {
    case selected && focused:
      return focusectedBackgroundColor;
    case selected && !focused:
      return selectedBackgroundColor;
    case !selected && focused:
      return focusedBackgroundColor;
    default:
      return backgroundColor;
  }
}

export function Item(props: ItemProps) {
  const {
    backgroundColor,
    focusedBackgroundColor,
    focusectedBackgroundColor,
    selectedBackgroundColor,
    children,
    focused,
    selected,
    ...itemStyles
  } = props;

  return (
    <View
      style={[
        styles.container,
        itemStyles,
        {
          backgroundColor: getBackgroundColor({
            selected,
            focused,
            focusectedBackgroundColor,
            focusedBackgroundColor,
            selectedBackgroundColor,
            backgroundColor,
          }),
        },
      ]}
    >
      {children}
    </View>
  );
}
