import React, {useEffect, useState} from 'react';
import {
  SafeAreaView,
  Text,
  StatusBar,
  TouchableOpacity,
  DeviceEventEmitter,
  NativeEventEmitter,
  Alert,
  View,
} from 'react-native';

import VnpayMerchant, {VnpayMerchantModule} from 'react-native-vnpay-merchant';
const eventEmitter = new NativeEventEmitter(VnpayMerchantModule);

const Payment = (props) => {
  const [text, setText] = useState('OpenSDK');
  useEffect(() => {
    // mở sdk
    eventEmitter.addListener('PaymentBack', (e) => {
      console.log('Sdk back!');

      // Đã available trên cả ios, android
      if (e) {
        switch (e.resultCode) {
          case 0:
            console.log('Sdk closed');
            break;
          case -1:
            console.log('Người dùng nhấn back từ sdk để quay lại');
            break;
          case 10: //ios
            console.log(
              'Người dùng nhấn chọn thanh toán qua app thanh toán (Mobile Banking, Ví...)',
            );
            break;
          case 99:
            console.log(
              'Người dùng nhấn back từ trang thanh toán thành công khi thanh toán qua thẻ khi gọi đến http://sdk.merchantbackapp',
            );
        }
      }
    });
    return () => {
      // khi tắt sdk
      eventEmitter.removeAllListeners('PaymentBack');
    };
  }, []);

  return (
    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
      <TouchableOpacity
        style={{
          paddingHorizontal: 24,
          paddingVertical: 10,
          backgroundColor: 'blue',
          borderRadius: 10,
        }}
        onPress={() => {
          VnpayMerchant.show({
            iconBackName: 'ic_back',
            paymentUrl:
              'http://sandbox.vnpayment.vn/paymentv2/vpcpay.html?vnp_Amount=2500000&vnp_Command=pay&vnp_CreateDate=20210310143159&vnp_CurrCode=VND&vnp_IpAddr=172.69.134.207&vnp_Locale=vn&vnp_OrderInfo=Thanh+toan+goi+98003+gia+25000&vnp_OrderType=190002&vnp_ReturnUrl=http%3A%2F%2Fmy.dcvinvest.com%2Fapp-vnpay-return-url.html&vnp_TmnCode=ICTLIFE1&vnp_TxnRef=EC_98003_20210310143159&vnp_Version=2.0.0&vnp_SecureHashType=SHA256&vnp_SecureHash=f9a293f8e12eeae28172c90446f955dd9ba21ff2f9d2119574e0297542501563',
            scheme: 'sampleapp',
            tmn_code: 'FAHASA03',
          });
          setText('Sdk opened');
        }}>
        <Text style={{color: 'white'}}>{text}</Text>
      </TouchableOpacity>
    </View>
  );
};

export default Payment;
