import React, { PropsWithChildren, ReactNode, useMemo } from "react";
import { TouchableOpacity, Text, ActivityIndicator, View } from "react-native";
import * as Haptics from "expo-haptics";

interface ButtonProps {
  onPress?: () => void;
  className?: string;
  textClassName?: string;
  disabled?: boolean;
  children?: string;
  loading?: boolean;
  color?: "purple" | "red";
  iconRight?: ReactNode;
}

export const Button: React.FC<PropsWithChildren<ButtonProps>> = ({
  onPress,
  className = "",
  textClassName = "",
  disabled = false,
  loading = false,
  color = "purple",
  iconRight,
  children,
}) => {
  const colorClasses = useMemo(() => {
    switch (color) {
      case "purple":
        return "bg-purple-600 active:bg-purple-700";
      case "red":
        return "bg-red-500 active:bg-red-600";
    }
  }, [color]);

  return (
    <TouchableOpacity
      className={`
        p-3 rounded-xl items-center justify-center
        ${loading || (disabled && "opacity-50")}
        ${colorClasses}
        ${className}
      `}
      onPress={() => {
        if (!loading && !disabled && onPress) {
          onPress();
          Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
        }
      }}
      disabled={disabled || loading}
    >
      {loading ? (
        <ActivityIndicator color={"white"} size={24} />
      ) : (
        <View className="flex flex-row items-center">
          <Text
            className={`
          text-white font-bold text-base
          ${disabled ? "text-gray-300" : ""}
          ${textClassName}
            ${iconRight ? "mr-2" : ""}
        `}
          >
            {children}
          </Text>
          {iconRight}
        </View>
      )}
    </TouchableOpacity>
  );
};
