import React, { useMemo } from "react";
import { StyleSheet } from "react-native";
import { Font } from "../../themes";
import { AppUtils } from "../../utils";
import ScalableText from "./ScalableText";
import type { F2TextProps } from "./types";

const Text: React.FC<F2TextProps> = ({
  accessibilityLabel,
  children,
  style,
  mode,
  decoration,
  position,
  color,
  onPress,
  onRef,
  link,
  size,
  underline,
  numberOfLines,
  onLayout,
  ...props
}) => {
  const colorStyle = color && { color };
  const linkStyle = link && Font.link;
  const underlineStyle = underline && Font.underline;
  const sizeStyle = size && {
    fontSize: size,
    lineHeight: size * 1.5,
  };

  const decorationStyle = useMemo(() => {
    return decorationStyles[decoration || "regular"];
  }, [decoration]);

  const positionStyle = useMemo(() => {
    return positionStyles[position || "left"];
  }, [position]);

  const fontStyle = useMemo(() => {
    return Font[mode || "default"];
  }, [mode]);

  return (
    <ScalableText
      accessibilityLabel={accessibilityLabel}
      onRef={onRef}
      onPress={onPress}
      style={[
        Font.default,
        fontStyle,
        style,
        decorationStyle,
        positionStyle,
        colorStyle,
        linkStyle,
        sizeStyle,
        underlineStyle,
      ]}
      numberOfLines={numberOfLines}
      onLayout={onLayout}
      {...props}
    >
      {children}
    </ScalableText>
  );
};

export const decorationStyles = StyleSheet.create<{ [key: string]: object }>({
  "bold": { fontFamily: "ProximaNova-Bold" },
  "medium": { fontFamily: "ProximaNova-Medium" },
  "italic": {
    fontFamily: AppUtils.isIOS()
      ? "ProximaNova-MediumIt"
      : "ProximaNova-Medium-It",
  },
  "bold-italic": { fontFamily: "ProximaNova-BoldItalic" },
});

const positionStyles = StyleSheet.create({
  left: { textAlign: "left" },
  center: { textAlign: "center" },
  right: { textAlign: "right" },
});

Text.defaultProps = {
  decoration: "medium",
  position: "left",
  mode: "default",
};

export default Text;
