import React from 'react';
import {
  StyleSheet,
  TextInput,
  View,
  ViewStyle,
} from 'react-native';
import { Icon } from 'react-native-elements';
import { connect } from 'react-redux';
import { RootState, UserRole } from '../../models';
import { Colors } from '../ui';

interface Props {
  onInputTextChanged: (text: string) => void;
  onSendRequest: () => void;
  text: string;
  style?: ViewStyle;
}

interface StateProps {
  accentColor: string;
}

const ChatInputComponent: React.SFC<Props & StateProps> = ({
  text, onInputTextChanged, onSendRequest, style, accentColor }) => (
    <View style={[styles.container, style]}>
      <TextInput
        multiline={true}
        numberOfLines={4}
        style={styles.input}
        value={text}
        onChangeText={onInputTextChanged}
      />
      <Icon
        raised
        name='send'
        type='font-awesome'
        color={accentColor}
        onPress={onSendRequest}
      />
    </View>
  );

const mapStateToProps: (state: RootState) => StateProps =
  (state) => {
    const user = state.user;
    const accentColor = user.body ?
      (
        user.role === UserRole.EMPLOYEE
          ? Colors.employee.primary
          : Colors.employer.primary
      )
      : Colors.default.primary;
    return { accentColor };
  };

export const ChatInput = connect(mapStateToProps)(ChatInputComponent);

const styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  input: {
    marginLeft: 12,
    height: 36,
    flex: 1,
    padding: 4,
  },
});
