'use strict';

import React, { Component, useEffect, useState } from 'react';

// import QRCodeScanner from 'react-native-qrcode-scanner';
import Container from '../components/container';
import { useRoute } from '@react-navigation/native';
import { globalStyles,customStyles,GetPageTheme } from "../styles/Global";
import {StatusBar,Image,TextInput,View,Text,ScrollView,TouchableOpacity,Platform,PermissionsAndroid,FlatList, Alert, Animated, DeviceEventEmitter,StyleSheet} from 'react-native';
import { ReactReduxContext,connect,useSelector, useDispatch  } from 'react-redux';
import { mapStateToProps,mapDispatchToProps } from '../reducers/contextProvider';
import {notify} from '../components/notify';
import moment from 'moment';

function QRScanner (props){

    const route = useRoute();
  
    // get all theme styles acc to current theme set 
    const PageTheme=GetPageTheme(props.DarkMode,route.name);
    const customStyle=customStyles(props.DarkMode,route.name);
    
    const [processing, setProcessing] = useState(false);
    const [loaderTitle, setLoaderTitle] = useState('loading...');
    const [params, setParams] = useState({'cameraType':'back'});
    const [showCamera, setShowCamera] = useState(true);
    const [hasUnsavedChanges,setHasUnsavedChanges] = useState(false);

    const d1=new Date();
    const d2=moment(d1).format('MM/DD/YYYY');
    
  
    useEffect(() => {
        // let params1=props.route.params? props.route.params:{};
        // setParams(params1);
    }, []);
    
   

    useEffect(() => {
        console.log('QR Page:', props.showCamera);
        
    },[props.showCamera]);

    async function hideCamera() {
        if (props.onHideCamera != null && typeof props.onHideCamera == 'function') {
            props.onHideCamera();
        }
        props.navigation.goBack();
    }

    async function onImageCaptured(imgData){
        console.log('on data rec:',imgData.data);
        console.log('onImageCaptured in camera screen');
        // imgData['storeData']=params.storeData
        // emit onImageCaptured in the screen from where this screen is opened
        DeviceEventEmitter.emit("OnQRSuccess",imgData.data);
        props.navigation.goBack();
    }

    
    
    return (
        <Container {...props} pt={0} avoidSafeArea={(Platform.OS=='ios'?true:false)}>
   
        {/* <QRCodeScanner
            onRead={onImageCaptured}
            cameraStyle
            // flashMode={RNCamera.Constants.FlashMode.torch}
            topContent={
            <Text style={styles.centerText}>
                scan the QR code.
            </Text>
            }
            bottomContent={
            <TouchableOpacity style={styles.buttonTouchable}>
                <Text style={styles.buttonText}>OK. Got it!</Text>
            </TouchableOpacity>
            }
        /> */}
         </Container>   
    );

}

const styles = StyleSheet.create({
  centerText: {
    flex: 1,
    fontSize: 18,
    padding: 32,
    color: '#777'
  },
  textBold: {
    fontWeight: '500',
    color: '#000'
  },
  buttonText: {
    fontSize: 21,
    color: 'rgb(0,122,255)'
  },
  buttonTouchable: {
    padding: 16
  }
});


export default connect(mapStateToProps, mapDispatchToProps)(QRScanner);
