import React from 'react';
import {View, Text, TextInput} from 'react-native';
import {WIDTHXD, getFontXD, WIDTH, HEIGHT} from '../../config/Functions';
import R from '../../assets/R';
import {RFValue} from 'react-native-responsive-fontsize';
const TextField = props => {
  const {
    title,
    onChangeText,
    isPassword,
    maxLength,
    isNumber,
    value,
    editable,
    error,
    placeholder,
    backgroundColor,
    onFocus,
    fontSize,
    fontSizeTitle,
    color,
    required,
    containerMarginVertical,
    containerMarginTop,
    containerMarginHorizontal,
    containerMarginBottom,
    containerBackgroundColor,
  } = props;
  const renderMess = () => {
    if (value && value.trim().length > 0) return `${title} không hợp lệ`;
    return `Vui lòng nhập ${title ? title.toLowerCase() : messageError} `;
  };

  return (
    <View style={{marginVertical: containerMarginVertical, marginHorizontal: containerMarginHorizontal, marginBottom: containerMarginBottom , backgroundColor: containerBackgroundColor, marginTop: containerMarginTop}}>
      <Text
        style={{
          fontSize: fontSizeTitle,
          color: R.colors.black,
          marginBottom: 5,
          fontFamily:R.fonts.fontRegular,
          fontWeight:'400',

        }}>
        {title ? title : ''}
        <Text style={{color: R.colors.red}}> {required ? "*" : ""}</Text>
      </Text>
      <TextInput

        placeholder={placeholder}
        maxLength={maxLength ? maxLength : 256}
        placeholderTextColor={R.colors.placeHolder}
        editable={editable != null ? editable : true}
        secureTextEntry={isPassword}
        autoCapitalize="none"
        value={value}
        onFocus={onFocus}
        keyboardType={isNumber ? 'number-pad' : 'default'}
        onChangeText={val => onChangeText(val)}
        style={{
          height: HEIGHT(35),
          color: color,
          borderColor:R.colors.grayBorderInputTextHeader,
          borderWidth:1,
          borderRadius: HEIGHT(6),
          fontSize: fontSize,
          paddingVertical: 5,
          paddingHorizontal: 10,
          backgroundColor: backgroundColor,
        }}
      />
      {error && <View
        style={{
          height: HEIGHT(20),
          marginTop: 5,
        }}>
        {error && !error.ref?.value && (
          <Text
            ellipsizeMode={'tail'}
            numberOfLines={1}
            style={{
              flex: 1,
              color: R.colors.red,
              fontSize: RFValue(10),
            }}>
            {error.message ? error.message : renderMess()}
          </Text>
        )}
      </View>}
    </View>
  );
};

export default React.memo(TextField);
