import React, {Component} from 'react';
import {
  Platform,
  StyleSheet,
  View,
  TouchableOpacity,
  Alert,
  SafeAreaView,
  Text,
  ScrollView,
} from 'react-native';
import {observable, action, transaction} from 'mobx';
import {Button} from 'react-native-elements';
import {observer, inject} from 'mobx-react';

import {scale, verticalScale, moderateScale} from 'react-native-size-matters';
import Icon from 'react-native-vector-icons/dist/FontAwesome5';
import Icon2 from 'react-native-vector-icons/dist/MaterialCommunityIcons';
import Toast, {DURATION} from 'react-native-easy-toast';
import BackHeader from '../../components/backHeader';
import LoadingView from '../../common/loading';
import {
  CreditCardInput,
  LiteCreditCardInput,
} from 'react-native-credit-card-input';
//component

// function
import {width, height} from '../../common/screen';
import {colors} from '../../assets/styles/colors-theme';
import {Fonts} from '../../assets/styles/fonts-theme';
import Size from '../../common/size';
const size = new Size();
//const log = new Log()
@inject('appStore')
@observer
export default class SyncApartment extends Component {
  static navigationOptions = {
    gesturesEnabled: false,
  };

  constructor(props) {
    super(props);
    this.store = props.appStore;
    this.state = {
      apartmentNeedToUpdate: this.store.apartmentDataWhichNeedToSync,
    };
  }

  renderCheckBox(selected) {
    if (selected) {
      return <Icon name="check-square" color={colors.mainColor} size={18} />;
    } else {
      return <Icon name="square" color={colors.mainColor} size={18} />;
    }
  }

  renderApartment() {
    return this.state.apartmentNeedToUpdate.map((items, index) => {
      return (
        <TouchableOpacity
          style={{
            flexDirection: 'row',
            width: width,
            alignItems: 'center',
            marginTop: 20,
          }}
          onPress={() => {
            this.store.apartmentDataWhichNeedToSync[index].selected = !this.store.apartmentDataWhichNeedToSync[index].selected;
            this.setState({apartmentNeedToUpdate:this.store.apartmentDataWhichNeedToSync})
          }}>
          <View style={{marginLeft: 20}}>
            {this.renderCheckBox(
             this.state.apartmentNeedToUpdate[index].selected
            )}
          </View>

          <Text
            style={{
              fontSize: 18,
              color: colors.textColor,
              marginLeft: 40,
              marginRight: 20,
            }}>
            {items.name}
          </Text>
        </TouchableOpacity>
      );
    });
  }

  render() {
    return (
      <View style={{flex: 1, backgroundColor: colors.mainColor}}>
        <View style={styles.container}>
          <LoadingView visible={this.store.loading} />
          <View
            style={{
              width: width,
              height: Platform.OS === 'ios' ? 65 : 80,
              backgroundColor: colors.mainColor,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Text
              style={{fontFamily: Fonts.ubuntu, color: 'white', fontSize: 20}}>
              {'上傳物業'}
            </Text>
            <TouchableOpacity style={{position: 'absolute', left: 20}}
            onPress = {()=>this.props.navigation.navigate('home')}>
              <Text style={{color: 'white', fontFamily: Fonts.ubuntu}}>
                {'略過'}
              </Text>
            </TouchableOpacity>
            <TouchableOpacity style={{position: 'absolute', right: 20}}
            onPress = {()=>this.store.uploadApartmentWhenLoginFindApartmentWhichNotSync(this)}>
              <Text style={{color: 'white', fontFamily: Fonts.ubuntu}}>
                {'上傳'}
              </Text>
            </TouchableOpacity>
          </View>
          <Text
            style={{
              fontSize: 18,
              color: colors.mainColor,
              fontFamily: Fonts.ubuntu,
              margin: 20,
            }}>
            {'發現有物業尚未上傳，請選擇以下物業上傳'}
          </Text>
          <ScrollView style={{flex: 1}}>{this.renderApartment()}</ScrollView>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    backgroundColor: 'white',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
  buttonView: {
    width: '80%',
    height: 60,
    marginTop: 10,
    alignItems: 'center',
    justifyContent: 'center',
    borderColor: 'black',
    borderWidth: 1,
  },
  textView: {
    color: colors.textColor,
    fontFamily: Fonts.ubuntu,
  },
  label: {
    color: 'black',
    fontSize: 12,
  },
  input: {
    fontSize: 16,
    color: 'black',
  },
});
