/**
 * React Native Full App (https://store.enappd.com/product/react-native-full-app)
 *
 * Copyright © 2019-present Enappd. All rights reserved.
 *
 * This source code is licensed as per the terms found in the
 * LICENSE.md file in the root directory of this source tree.
 */

import React from 'react';
import { Google } from 'expo';
import { showToast } from '../../../utils';
import { Button, Icon, Text } from 'native-base';
import styles from './style';

interface GoogleLoginProps {
  afterAccessAction: (token: any, accessToken: any) => void;
}

class GoogleLogin extends React.Component<GoogleLoginProps> {
  doLogin = async () => {
    const webClientId =
      '667888379468-f8lktfhipp65cvfejcgn0864kigeen4u.apps.googleusercontent.com';
    try {
      const { type, accessToken, idToken } = await Google.logInAsync({
        webClientId,
      });
      if (type === 'success') {
        this.props.afterAccessAction(idToken, accessToken);
      }
    } catch (e) {
      console.log(e, 'error in facebook login');
      if (e.message) {
        showToast(e.message);
      }
    }
  };

  render() {
    return (
      <Button
        iconLeft
        full
        rounded
        danger
        onPress={this.doLogin}
        style={[styles.marginTop]}
      >
        <Icon name="google" type="FontAwesome" />
        <Text>Login with Google</Text>
      </Button>
    );
  }
}

export default GoogleLogin;
