/**
 * 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 * as Facebook from 'expo-facebook';
import React from 'react';
import { Button, Icon, Text } from 'native-base';
import { showToast } from '../../../utils';
import styles from './style';

interface FacebookLoginProps {
  afterAccessAction: (token: any) => void;
}

class FacebookLogin extends React.Component<FacebookLoginProps> {
  doLogin = async () => {
    try {
      const options: any = {
        permissions: ['public_profile'],
        behavior: 'web',
      };
      const { type, token } = await Facebook.logInWithReadPermissionsAsync(
        '408937656335090',
        options
      );
      if (type === 'success') {
        this.props.afterAccessAction(token);
      }
    } catch (e) {
      console.log(e, 'error in facebook login');
      if (e.message) {
        showToast(e.message);
      }
    }
  };

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

export default FacebookLogin;
