import React, { Component } from "react";
import {
    StyleSheet,
    Text,
    View,
    BackHandler,
    Platform,
    StatusBar,
} from "react-native";
import { WebView } from "react-native-webview";
// import { CAR_INSURANCE_URL as DEFAULT_URL } from "./defaultURLs";
import { Spinner } from "react-native-digitcamera/src/common/ThemedComponents";
import {
    hideHeaderInWebView,
    onErrorhandler,
    getUTMSource,
} from "../common/commonFunction"
import Header from '../Screens/Header'

class CustomWebview extends Component {
    constructor(props) {
        super(props);
        this.state = {
            url: "https://prod-customerdatadocument.godigit.com/CustomerDataDocument/feedback?feedbackSource=AUTO_PI&feedbackKey=UP32LT5046",
            backButtonEnabled: false,
            scalesPageToFit: true,
            canGoBack: false,
            activityIndicatorVisibility: true,
            status: "No Page Loaded",
            forwardButtonEnabled: false,
            loading: true,
            title: "",
        };
    }

    onBackPress = () => {
        const { canGoBack } = this.state;
        console.log("canGoBack -> ", canGoBack);
        if (canGoBack) {
            this.WEBVIEW_REF.goBack();
            return true;
        }
        return false;
    };
    componentDidMount() {
        console.log("https://prod-customerdatadocument.godigit.com/CustomerDataDocument/feedback?feedbackSource=AUTO_PI&feedbackKey=UP32LT5046");
        if (Platform.OS === "android")
            BackHandler.addEventListener("hardwareBackPress", this.onBackPress);
    }

    componentWillUnmount() {
        if (Platform.OS === "android")
            BackHandler.removeEventListener("hardwareBackPress", this.onBackPress);
    }

    onNavigationStateChange(events) {
        this.setState({
            backButtonEnabled: events.canGoBack && Platform.OS === "ios",
            forwardButtonEnabled: events.canGoForward,
            url: events.url,
            // status: events.title,
            // loading: events.loading,
            scalesPageToFit: true,
            canGoBack: events.canGoBack,
            title: events.title,
        });
    }

    render() {
        let redirectionLink = this.props.route.params?.redirectionLink;
        if (redirectionLink) {
            redirectionLink = getUTMSource(redirectionLink);
        } else redirectionLink = "https://prod-customerdatadocument.godigit.com/CustomerDataDocument/feedback?feedbackSource=AUTO_PI&feedbackKey=UP32LT5046";
        return (
            <>
                <View style={{ marginTop: StatusBar.currentHeight, marginBottom: 0 }}>
                    <Header
                        hideArrowBackButton={true}
                    />
                </View>

                <StatusBar
                    barStyle="dark-content"
                    hidden={false}
                    backgroundColor="#ffffff"
                    translucent={true}
                />
                <WebView
                    ref={(ref) => (this.WEBVIEW_REF = ref)}
                    source={{ uri: redirectionLink }}
                    onNavigationStateChange={(event) =>
                        this.onNavigationStateChange(event)
                    }
                    javaScriptEnabledAndroid={true}
                    javaScriptEnabled={true}
                    automaticallyAdjustContentInsets={false}
                    startInLoadingState={true}
                    allowsBackForwardNavigationGestures={true}
                    injectedJavaScript={hideHeaderInWebView}
                    onLoadProgress={(progress) => {
                        if (this.state.url !== progress.nativeEvent.url)
                            this.onNavigationStateChange(progress.nativeEvent);
                    }}
                    onError={(syntheticEvent) => {
                        const { nativeEvent } = syntheticEvent;
                        onErrorhandler(nativeEvent);
                        this.props.navigation.goBack();
                    }}
                    renderLoading={() => {
                        return <Spinner animating />;
                    }}
                />
            </>
        );
    }
}

export default CustomWebview;
