<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>app Installed</title>
</head>
<body>

<script>
    /**
     * 아래 코드는 모바일에서 잘 동작함
     *  jQuery 환경
     * */
    $(document).ready(function() {
        const typeData = "<%= request.getAttribute("type") %>";
        const $btnAndroid = $('.btn_android');
        const $btnIos = $('.btn_ios');
        let isOpened = false;

        $btnAndroid.show();
        $btnIos.show();

        // blur 이벤트 등록
        window.addEventListener('blur', () => {
            console.log('true: its blurred')
            isOpened = true;
        });

        // pagehide 이벤트 등록
        window.addEventListener('pagehide', function() {
            console.log('true: its pagehided')
            isOpened = true;
        });

        // visibilitychange 이벤트 등록
        document.addEventListener('visibilitychange', function() {
            if (document.hidden) {
                console.log('true: its visibilitychanged')
                isOpened = true;
            }
        });

        function isAppNotInstalled() {
            console.log('>> isAppNotInstalled is called');
            let loader = new tui.component.m.AppLoader();

            if (loader.agentDetector.isAndroid()) {
                console.log('>> isAndroid');
                $btnAndroid.attr('href', 'intent://unione/#Intent;scheme=unione;package=com.tesco.lk.manager;end').show();
                window.location.href = $btnAndroid.attr('href');
            } else if (loader.agentDetector.isIOS()) {
                console.log('>> isIos');
                window.location.href = $btnIos.attr('href');
            }
        }

        var appScheme;
        if (typeData === 'mall') {
            console.log('>> type is mall');
            appScheme = 'testapp://webview?landingtype=1&webviewtype=3&url=http%3A%2F%2Fiammall.net';
        } else {
            console.log('>> type is else');
            appScheme = 'testapp://';
        }

        // move to app
        window.location.href = appScheme;

        setTimeout(() => {
            if (!(document.hidden || document.webkitHidden || isOpened)) {
                console.log('>> app is not installed');
                isAppNotInstalled();
            }
        }, 2000);
    });
</script>
</body>
</html>