import React,{useState,useEffect} from 'react';
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} from 'react-native';
import { ReactReduxContext,connect,useSelector, useDispatch  } from 'react-redux';
import { mapStateToProps,mapDispatchToProps } from '../reducers/contextProvider';
import SQLite from 'react-native-sqlite-2';
import {notify} from '../components/notify';
import moment from 'moment';
import {getDownloadJson,DownloadData} from '../controller/functions';
import {get_item,set_item,clear_item} from '../components/localStorage';
import {version,sloganline,appname,testurl, db} from '../constants/constants';
import CustomHeader from '../components/customHeader';
import {Card} from 'react-native-paper';
import {AntDesign, Feather, FontAwesome, MaterialCommunityIcons} from '../components/icons';
import { WebView } from 'react-native-webview';
import CustomLoader from '../components/CustomLoader';
import NoDataComponent from '../components/NoDataComponent';
import LinearGradient from 'react-native-linear-gradient';
// import RNExitApp from 'react-native-exit-app';
import { ConfirmSaveAlert } from '../components/alert';
import CustomModal from '../components/CustomModal';
import { PrimaryTheme } from '../styles/Themes';
import * as RNFS from 'react-native-fs';


function GetDb(props) { 
    const route = useRoute();
  
    const ST=props.StaticText || {};
    // get all theme styles acc to current theme set 
    const PageTheme=GetPageTheme(props.DarkMode,route.name);
    const customStyle=customStyles(props.DarkMode,route.name);
  
    const [mpin, setMpin] = useState('');
    const [processing, setProcessing] = useState(false);
    const [isDataFound, setisDataFound] = useState(false);
    const [showAlert, setShowAlert] = useState(false);
    const [showMPinModal, setShowMPinModal] = useState(false);
    
    
    const [reportData, setReportData] = useState([]);
    const [designation, setDesignation] = useState('');
    const d1=new Date();
    const d2=moment(d1).format('MM/DD/YYYY');
  
    useEffect(() => {
      
    }, []);


  

    async function showdbfile(){
      // let source='/data/data/com.performicsmanager/databases/';
        let source='/data/data/com.performicsmanager/files/';
        console.log('path:',source);
        const items = await RNFS.readDirAssets(source);
        console.log('items:',items);
        items.forEach(async item => {
            if (item.isFile()) {
              console.log(`f ${item.path}`);
      
              const destPath = RNFS.DocumentDirectoryPath + "/Performicsdatabase/" + item.name;
      
              console.log(`cp ${item.path} ${destPath}`);
              await RNFS.copyFileAssets(item.path, destPath);
            } else {
              console.log(`d ${item.path}/`);
            }
        });
        const destPath1 =RNFS.DownloadDirectoryPath + "/PerformicsSupervisor.db";
        let dbFile='/data/data/com.performicsmanager/files/PerformicsSupervisor.db';
        RNFS.exists(dbFile)
        .then(resn => {
            console.log('isExist res:',resn)
            RNFS.copyFile(dbFile,destPath1);
        });
    }
    

    


  
    return ( 
      <Container {...props} pt={0} avoidSafeArea={(Platform.OS=='ios'?true:false)}>
        {processing && <CustomLoader title={ST.Loading+"..."}/>}
        
        <CustomHeader {...props} title={ST.Settings} hideBellIcon={true} hideTimer={true}/>
      
        <LinearGradient colors={[PageTheme.$gradient1,PageTheme.$primary_color]} start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }} style={customStyle.roundScrollWrapper}>
        
        <View style={[customStyle.pageContentWrapper]}>
            {/* {borderTopLeftRadius:0,borderTopRightRadius:0,} */}
              <View style={customStyle.settingsWrap}>
                  <TouchableOpacity style={customStyle.settingsBtn} activeOpacity={0.6} onPress={()=>{showdbfile() }}>
                      <Text style={customStyle.settingsBtnText}>{'OPen Database file'}</Text>
                  </TouchableOpacity>
              </View>
           
        </View>
        </LinearGradient>

      </Container>   
    ); 
  }
  
  

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