import React, {useState} from 'react';
import {View, TextInput, StyleSheet, Dimensions, Text} from 'react-native';
import {Colors, FONTS} from 'global-styles';
import {RFValue} from 'react-native-responsive-fontsize';
import {Icon} from 'components';
const {width} = Dimensions.get('window');

const _Phone = props => {
  const {label, editable, note, error, required} = props;
  const [isFocused, setIsFocused] = useState(false);
  return (
    <View style={styles.container}>
      {label && (
        <Text style={styles.label}>
          {label} {required && <Text style={{color: Colors.RED}}>*</Text>}
        </Text>
      )}
      <View style={styles.inputSection(editable)}>
        <View style={styles.numberIdSection}>
          <Text style={styles.numberIdText}>🇮🇩 +62</Text>
        </View>
        <TextInput
          {...props}
          style={styles.input(isFocused)}
          editable={editable}
          keyboardType="decimal-pad"
          onChange={e => setIsFocused(e.nativeEvent.text.length > 0)}
          placeholderTextColor={Colors.GREY}
        />
      </View>
      {note && <Text style={styles.note}>{note}</Text>}
      {error && (
        <View style={styles.errorSection}>
          <Icon name="error" type="MaterialIcons" style={styles.errorIcon} />
          <Text style={styles.errorText}>{error}</Text>
        </View>
      )}
    </View>
  );
};

_Phone.defaultProps = {
  editable: true,
  note: undefined,
  error: undefined,
  required: false,
};

const styles = StyleSheet.create({
  container: {},
  label: {
    fontFamily: FONTS.poppins[600],
    fontSize: RFValue(11.5),
    color: Colors.TEXT,
  },
  inputSection: editable => ({
    flexDirection: 'row',
    alignItems: 'center',
    height: width * 0.12,
    borderWidth: 1,
    borderColor: Colors.LINE_STROKE,
    borderRadius: width * 0.02,
    marginVertical: width * 0.023,
    justifyContent: 'center',
    backgroundColor: editable ? Colors.WHITE : Colors.DISABLED,
  }),
  input: isFocused => ({
    flex: 1,
    fontFamily: FONTS.poppins[isFocused ? 500 : 400],
    fontSize: RFValue(13),
    color: Colors.TEXT,
    paddingHorizontal: width * 0.04,
  }),
  note: {
    fontFamily: FONTS.poppins[400],
    fontSize: RFValue(10),
    color: Colors.TEXT_LIGHT,
    marginTop: -width * 0.005,
  },
  errorSection: {
    flexDirection: 'row',
    alignItems: 'center',
    marginTop: width * 0.002,
  },
  errorIcon: {
    color: Colors.RED,
  },
  errorText: {
    fontFamily: FONTS.poppins[400],
    fontSize: RFValue(10),
    color: Colors.RED,
    marginTop: -width * 0.001,
    marginLeft: width * 0.01,
  },
  numberIdSection: {
    height: width * 0.11,
    backgroundColor: Colors.DISABLED,
    borderRightWidth: 1,
    borderColor: Colors.LINE_STROKE,
    borderTopLeftRadius: width * 0.02,
    borderBottomLeftRadius: width * 0.02,
    alignItems: 'center',
    justifyContent: 'center',
    paddingHorizontal: width * 0.02,
  },
  numberIdText: {},
});

export default _Phone;
