import * as React from "react";
import {IProps} from "./app";
import {BackHandler, KeyboardAvoidingView, Platform} from "react-native";
import LoadingInit from "../../loading/init";
import ModalInit from "../../modal/init";
import PopupInit from "../../popup/init";

const CompositeApp: React.FC<IProps> = (props) => {
    const onBackPress = () => {
        if (typeof props.backPress === "function") return props.backPress();
        return false
    }

    React.useEffect(() => {
        BackHandler.addEventListener('hardwareBackPress', () => onBackPress());
        return () => {
            BackHandler.removeEventListener('hardwareBackPress', () => onBackPress());
        }
    }, [])

    return <KeyboardAvoidingView
        style={{flex: 1}}
        behavior={Platform.OS === "ios" ? "padding" : undefined}>
        {props.children}
        <PopupInit/>
        <ModalInit/>
        <LoadingInit/>
    </KeyboardAvoidingView>
}

export default CompositeApp
