import React from 'react';
import {View, Text} from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
import MIcon from 'react-native-vector-icons/MaterialCommunityIcons';
import {colors, font} from '../utils/common';
import Touch from './Touch';
import Modal from './Modal';
import ModalStyles from './Modal/styles';

const mStyles = {
  ...ModalStyles,
  innerContainer: {
    ...ModalStyles.innerContainer,
    width: 580,
  },
  header: {
    ...ModalStyles.header,
    fontSize: font.title,
  },
};

export default class KeyboardModal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      clickDot: true,
    };
  }

  getKeyboard = () => {
    const rows = [];
    let items = [];
    const {dot} = this.props;
    for (let i = 1; i <= 12; i++) {
      let unit;
      if (i === 10 && !dot) {
        unit = (
          <MIcon style={styles.keyUnit} name="backspace-outline" size={60} />
        );
      } else if (i === 10 && dot) {
        unit = <Text style={styles.keyUnit}>.</Text>;
      } else if (i === 11) {
        unit = <Text style={styles.keyUnit}>0</Text>;
      } else if (i === 12) {
        unit = <Text style={styles.keyUnit}>确认</Text>;
      } else {
        unit = <Text style={styles.keyUnit}>{i}</Text>;
      }
      const item = (
        <Touch style={styles.keyTouch} key={i} onPress={() => this.pressKey(i)}>
          {unit}
        </Touch>
      );
      items.push(item);
      if (i % 3 === 0) {
        rows.push(
          <View style={styles.keyRow} key={i}>
            {items}
          </View>,
        );
        items = [];
      }
    }
    return <View>{rows}</View>;
  };

  pressKey = index => {
    let {clickDot} = this.state;
    const {dot, operateCb, value, setValue} = this.props;
    let numberValue = value;
    if (index < 10) {
      numberValue += index;
    } else if (index === 10 && !dot) {
      numberValue = numberValue.substr(0, numberValue.length - 1);
    } else if (index === 10 && dot && clickDot) {
      numberValue += '.';
      clickDot = false;
    } else if (index === 11) {
      numberValue += 0;
    } else if (index === 12) {
      this.props.onSubmit(numberValue);
      this.handleClose();
      return;
    }
    this.setState({clickDot});
    operateCb();
    setValue(numberValue);
  };

  handleClose = clear => {
    const {onClose, setValue} = this.props;
    this.setState({clickDot: true});
    setValue('');
    if (!clear) {
      onClose();
    }
  };

  render() {
    const {title, visible, value} = this.props;
    return (
      <Modal
        styles={mStyles}
        visible={visible}
        transparent
        maskClosable
        onClose={this.handleClose}
        title={title}>
        <Touch
          style={{position: 'absolute', top: -65, right: 20}}
          onPress={this.handleClose}
          feedback={false}>
          <Icon size={50} name="ios-close-circle-outline" />
        </Touch>
        <View
          style={{
            width: 550,
            borderTopColor: '#cccccc',
            borderTopWidth: 1,
            paddingVertical: 20,
          }}>
          <View
            style={{
              width: 550,
              height: 70,
              justifyContent: 'center',
              position: 'relative',
            }}>
            <View
              style={{
                borderBottomWidth: 2,
                borderBottomColor: '#ddd',
                alignItems: 'center',
                marginLeft: 20,
                marginRight: 20,
              }}>
              <Text style={{color: 'gray', fontSize: 35, paddingBottom: 15}}>
                {value || ' '}
              </Text>
            </View>
            <Touch
              onPress={() => this.handleClose(true)}
              style={{
                position: 'absolute',
                right: 30,
                paddingLeft: 10,
                paddingRight: 10,
              }}>
              <Icon name="ios-close" size={50} color="#D1D1D1" />
            </Touch>
          </View>
          {this.getKeyboard()}
        </View>
      </Modal>
    );
  }
}

const styles = {
  keyRow: {
    flexDirection: 'row',
    justifyContent: 'center',
    marginTop: 15,
  },
  keyTouch: {
    width: 80,
    height: 80,
    justifyContent: 'center',
    alignItems: 'center',
    borderWidth: 1,
    borderColor: '#cccccc',
    borderRadius: 5,
    marginHorizontal: 15,
  },
  keyUnit: {
    // fontSize: font.textActive,
    fontSize: 25,
    color: colors.text,
  },
};
