import React, {Component} from 'react';
import {
  View,
  Text,
  TouchableOpacity,
  StyleSheet,
  Image,
  FlatList,
  Linking,
} from 'react-native';
import FastImage from 'react-native-fast-image';
import Icon from 'react-native-vector-icons/FontAwesome5';
import {colors} from '../../assets/styles/colors-theme';
import {Fonts} from '../../assets/styles/fonts-theme';
import history from '../../common/history';
import LoadingView from '../../common/loading';
import NaviBar from '../../components/navi-bar';
import {scale, verticalScale, moderateScale} from 'react-native-size-matters';
import Migrations from '../../services/realm/Mirgrations';
import {inject, observer} from 'mobx-react';
import {width, height} from '../../common/screen';

export default class RepairList extends Component {
  constructor(props) {
    super(props);
  }

  renderMobile() {
    if (this.props.items.mobile != '') {
      return (
        <Text style={styles.textView}>
          {'手機: ' + this.props.items.mobile}
        </Text>
      );
    } else {
      return <Text style={styles.textView}>{'手機: N/A'}</Text>;
    }
  }

  renderTel() {
    if (this.props.items.tel != '') {
      return (
        <Text style={styles.textView}>
          {'電話: '}
          <Text style={[styles.textView, {color: colors.mainColor}]}>
            {this.props.items.tel}
          </Text>
        </Text>
      );
    } else {
      return <Text style={styles.textView}>{'電話: N/A'}</Text>;
    }
  }

  renderWebsite() {
    if (this.props.items.website != '') {
      return (
        <TouchableOpacity
          onPress={() => {
            Linking.canOpenURL(this.props.items.website).then(supported => {
              if (supported) {
                Linking.openURL(this.props.items.website);
              } else {
                console.log("Don't know how to open URI");
              }
            });
          }}>
          <Text style={styles.textView}>
            {'網站: '}
            <Text style={[styles.textView, {color: colors.mainColor}]}>
              {this.props.items.website}
            </Text>
          </Text>
        </TouchableOpacity>
      );
    } else {
      return <Text style={styles.textView}>{'網站: N/A'}</Text>;
    }
  }

  render() {
    return (
      <TouchableOpacity
        style={{
          marginTop: 10,
          marginBottom: 10,
          marginLeft: 10,
          marginRight: 10,
        }}
        //onPress={this.props.onPress && this.props.onPress}
      >
        <View
          style={{
            flexDirection: 'row',
            justifyContent: 'space-between',
            alignItems: 'center',
          }}>
          <View>
            <Text style={styles.titleView}>{this.props.items.name}</Text>
            <Text style={styles.textView}>{this.props.items.addr}</Text>

            {this.renderTel()}
            {this.renderWebsite()}
          </View>
        </View>
        <View
          style={{
            height: 1,
            width: width - 20,
            backgroundColor: colors.textColor,
            marginTop: 15,
          }}
        />
      </TouchableOpacity>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  titleView: {
    marginTop: 3,
    fontFamily: Fonts.ubuntu,
    color: 'black',
    fontSize: 17,
  },
  textView: {
    marginTop: 3,
    fontFamily: Fonts.ubuntu,
    color: colors.textColor,
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  button: {
    borderWidth: 1,
    borderColor: '#000000',
    margin: 5,
    padding: 5,
    width: '70%',
    backgroundColor: '#DDDDDD',
    borderRadius: 5,
  },
  textField: {
    borderWidth: 1,
    borderColor: '#AAAAAA',
    margin: 5,
    padding: 5,
    width: '70%',
  },
  spacer: {
    height: 10,
  },
  title: {
    fontWeight: 'bold',
    fontSize: 20,
    textAlign: 'center',
  },
});
