import * as React from "react";
import { View, Alert } from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import { globalStyles, customStyles } from "../styles/Global";
import { useRoute } from '@react-navigation/native';
import NetworkStatusBar from "./NetworkStatusBar";
import RNExitApp from 'react-native-exit-app';
import moment from "moment";
import { db } from "../constants/constants";
import {isDeviceTimeValid } from '../controller/functions';


function onRemoveScreen(e) {
  Alert.alert(
    "",
    "Your Device Date and Time is not correct. Please correct and restart App.",
    [
      {
        text: "OK", onPress: async () => {
          RNExitApp.exitApp();
        }
      }
    ]
  );
}

const Container = (props) => {
  const route = useRoute();
  const globalStyle = globalStyles(props.DarkMode, route.name);
  const paddingS = props.pt != null ? { paddingTop: props.pt } : {};

  let d1 = new Date();
  let d2 = moment(d1).format('MM/DD/YYYY');

  React.useEffect(() => {
    ///this function for date and time match
    isDeviceTimeValid();
    // comment becuase server date table not coming from backent
    // CheckVisitDate()
  }, [])

  const CheckVisitDate = async () => {
    db.transaction(function (txn) {
      txn.executeSql(`select * from ServerTime`, [], function (txn1, txnres) {
        let data = txnres.rows.item(0);
        console.log('check to show DT alert', route.name, d2 != data.ServerDate);
        if (d2 != data.ServerDate && route.name != "MpinLogin" && route.name != "PrevStoreReason" && route.name != "initialScreen" && route.name != "Project" && route.name != "Login" && route.name != "Mpin" && route.name != "One_QAD" && route.name != "Notice Board" && route.name != "NoticeBoard") {
          console.log('show DT alert', route.name);
          onRemoveScreen()
        } 
      }, function (txnE, txnerr) {
        console.log(txnerr);
      });
    });
  }
  return (
    <SafeAreaView  edges={['bottom']} style={[globalStyle.container, paddingS]}>
      {props.children}
      <NetworkStatusBar {...props} />
    </SafeAreaView>
  );
};

export default Container;