import React from 'react';
import {
  type StyleProp,
  StyleSheet,
  type TextStyle,
  TouchableOpacity,
  type ViewStyle,
} from 'react-native';
import { moderateScale } from 'react-native-size-matters';
import { defaultScale } from '../../utils/Common';
import TTypography from '../TTypography/TTypography';
import { TypographyVariant } from '../TTypography/TTypographyEnum';
import { withTheme, useTheme } from '../../core/theming';
import type { Theme } from '../../utils/types';
import IconSVG from '../../assets/svgs';

interface Props {
  onPress: () => void;
  onPressRightIcon?: () => void;
  onPressLeftIcon?: () => void;
  viewStyle?: StyleProp<ViewStyle>;
  textStyle?: StyleProp<TextStyle>;
  label: string;
  selectedBackgroundColor?: string;
  selectedTextColor?: string;
  textColor?: string;
  isDisabled?: boolean;
  iconHeight?: number;
  iconWidth?: number;
  rightIcon?: any;
  leftIcon?: any;
  isSelected?: boolean;
  theme: Theme;
}

const defaultSize = 20;

const TChip: React.FC<Props> = ({
  label,
  onPress,
  onPressRightIcon,
  onPressLeftIcon,
  selectedBackgroundColor,
  selectedTextColor,
  textColor,
  iconHeight = defaultSize,
  iconWidth = defaultSize,
  rightIcon,
  leftIcon,
  isDisabled = false,
  textStyle = {
    paddingHorizontal: moderateScale(12, defaultScale),
    paddingVertical: moderateScale(4, defaultScale),
    fontSize: moderateScale(14, defaultScale),
    lineHeight: moderateScale(21, defaultScale),
  },
  isSelected = false,
}): any => {
  const handleChip = () => {
    if (isDisabled) {
      return;
    }
    onPress();
  };
  const { colors } = useTheme();
  const bgColor = selectedBackgroundColor
    ? selectedBackgroundColor
    : colors.primaryButtonColor;

  return (
    <TouchableOpacity
      onPress={handleChip}
      activeOpacity={isDisabled ? 0.5 : 1}
      testID={isSelected ? `${label}-disabled-btn` : `${label}-enabled-btn`}
      accessibilityLabel={
        isSelected ? `${label}-disabled-btn` : `${label}-enabled-btn`
      }
      style={{
        ...styles.viewStyle,
        opacity: isDisabled ? 0.5 : 1,
        backgroundColor: isSelected ? bgColor : colors.white,
        borderColor: colors.primaryButtonColor,
      }}
    >
      <TouchableOpacity onPress={onPressLeftIcon}>
        <IconSVG name={leftIcon} height={iconHeight} width={iconWidth} />
      </TouchableOpacity>
      <TTypography
        children={label}
        variant={TypographyVariant.PRIMARY}
        color={
          isSelected
            ? selectedTextColor
              ? selectedTextColor
              : colors.white
            : textColor
            ? textColor
            : colors.tabBarColor
        }
        fontSize={moderateScale(14, defaultScale)}
        style={textStyle}
      />
      <TouchableOpacity onPress={onPressRightIcon}>
        <IconSVG name={rightIcon} height={iconHeight} width={iconWidth} />
      </TouchableOpacity>
    </TouchableOpacity>
  );
};

const styles = StyleSheet.create({
  viewStyle: {
    flexDirection: 'row',
    alignItems: 'center',
    borderWidth: moderateScale(1, defaultScale),
    borderRadius: moderateScale(4, defaultScale),
    alignSelf: 'center',
  },
});
export default withTheme(TChip);
