<!DOCTYPE html>
<html lang="en-us">
<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <link rel="stylesheet" href="./style.css">

    <!-- Yandex Games SDK -->
    <script src="/sdk.js"></script>

    <style>
        /* Убираем выделение по нажатию клавиш */
        canvas:focus {
            outline: none;
        }

        html, body {
            /* Убираем отступы */
            padding: 0;
            margin: 0;
            /* Отключаем скролл и лонгтап на IOS */
            overflow: hidden;
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -khtml-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            -webkit-tap-highlight-color: rgba(0,0,0,0);
            /* Ставим высоту на 100% */
            height: 100%;
        }
    </style>

    <!-- Additional head modules -->

</head>
<body class="{{{ SPLASH_SCREEN_STYLE.toLowerCase() }}}">

    <div id="unity-container" class="unity-desktop">
        <canvas id="unity-canvas" tabindex="-1"></canvas>
    </div>
    <div id="loading-cover" style="display:none;">
        <div id="unity-loading-bar">
            <div id="unity-logo"><img src="logo.png"></div>
            <div id="unity-progress-bar-empty" style="display: none;">
                <div id="unity-progress-bar-full"></div>
            </div>
            <div class="spinner"></div>
        </div>
    </div>

    <!-- Additional body modules -->

    <script>
        const hideFullScreenButton = "";
        const buildUrl = "Build";
        const loaderUrl = buildUrl + "/{{{ LOADER_FILENAME }}}";
        const config = {
            dataUrl: buildUrl + "/{{{ DATA_FILENAME }}}",
            frameworkUrl: buildUrl + "/{{{ FRAMEWORK_FILENAME }}}",
            codeUrl: buildUrl + "/{{{ CODE_FILENAME }}}",
            #if MEMORY_FILENAME
        memoryUrl: buildUrl + "/{{{ MEMORY_FILENAME }}}",
            #endif
        #if SYMBOLS_FILENAME
        symbolsUrl: buildUrl + "/{{{ SYMBOLS_FILENAME }}}",
            #endif
        streamingAssetsUrl: "StreamingAssets",
            companyName: "{{{ COMPANY_NAME }}}",
            productName: "{{{ PRODUCT_NAME }}}",
            productVersion: "{{{ PRODUCT_VERSION }}}"
        };

        const container = document.querySelector("#unity-container");
        const canvas = document.querySelector("#unity-canvas");
        const loadingCover = document.querySelector("#loading-cover");
        const progressBarEmpty = document.querySelector("#unity-progress-bar-empty");
        const progressBarFull = document.querySelector("#unity-progress-bar-full");
        const spinner = document.querySelector('.spinner');

        const canFullscreen = (function () {
            for (const key of [
                'exitFullscreen',
                'webkitExitFullscreen',
                'webkitCancelFullScreen',
                'mozCancelFullScreen',
                'msExitFullscreen',
            ]) {
                if (key in document) {
                    return true;
                }
            }
            return false;
        }());

        if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
            container.className = "unity-mobile";
            config.devicePixelRatio = 1;
        }

        // Фоновое изображение при загрузке игры. При сборке билда код меняется взависимости от настроек проекта.
        var backgroundUnity = "url('" + buildUrl + "/{{{ BACKGROUND_FILENAME.replace(/'/g, '%27') }}}') center / cover";
        loadingCover.style.background = "url('background.png') center / cover";

        loadingCover.style.display = "";

        // Выключаем появление меню при правом клике мыши
        document.addEventListener('contextmenu', event => event.preventDefault());

        // Возвращаем фокус, если кликнули по экрану
        function FocusGame() {
            window.focus();
            canvas.focus();
        }

        window.addEventListener('pointerdown', () => {
            FocusGame();
        });
        window.addEventListener('touchstart', () => {
            FocusGame();
        });

        document.addEventListener('visibilitychange', function () {
            if (ysdk !== null && initGame == true) {
                if (document.hidden) {
                    myGameInstance.SendMessage('YandexGame', 'OnVisibilityGameWindow', 'false');
                }
                else {
                    myGameInstance.SendMessage('YandexGame', 'OnVisibilityGameWindow', 'true');

                    if ((ysdk.deviceInfo.isMobile() || ysdk.deviceInfo.isTablet()) && ysdk.screen.fullscreen.status == 'off')
                        ysdk.screen.fullscreen.request();
                }
            }
        });

        function ResumeGame() {
            if (myGameInstance && myGameInstance.Module) {
                // Возобновление главного цикла Unity
                myGameInstance.Module.resumeMainLoop();

                // (Курсор снова может быть спрятан, если это требуется игрой)
            }

            let audioElements = document.querySelectorAll('audio, video');
            audioElements.forEach(element => {
                element.muted = false;
            });
        }

        let StartUnityInstance;
        let myGameInstance = null;
        let ysdk = null;
        let player = null;
        let payments = null;
        let initGame = false;
        let nowFullAdOpen = false;

        const script = document.createElement("script");
        script.src = loaderUrl;
        script.onload = () => {
            StartUnityInstance = function () {
                createUnityInstance(canvas, config, (progress) => {
                    spinner.style.display = "none";
                    progressBarEmpty.style.display = "";
                    progressBarFull.style.width = `${100 * progress}%`;
                }).then((unityInstance) => {
                    myGameInstance = unityInstance;
                    loadingCover.style.display = "none";
                }).catch((message) => {
                    console.error(message);
                });
            };
            if (LocalHost())
                StartUnityInstance_IfUnloaded();
        };

        function StartUnityInstance_IfUnloaded() {
            if (spinner.style.display !== "none")
                StartUnityInstance();
        }

        function LocalHost() {
            var host = window.location.hostname;
            if (host == "localhost" || host == "127.0.0.1")
                return true;
            else return false;
        }

        InitYSDK();
        async function InitYSDK() {
            try {
                if (LocalHost()) return;

                console.log('Init Yandex SDK');
                ysdk = await YaGames.init();

                FullAdShow(); // First ad true

                // Additional init modules

            } catch (e) {
                console.error('CRASH Initialization SDK: ', e);
            }

            StartUnityInstance_IfUnloaded();
        }

        function InitGame() {
            console.log('Init Game Success');
            initGame = true;

            if (nowFullAdOpen == true && myGameInstance != null) {
                myGameInstance.SendMessage('YandexGame', 'OpenFullAd');
            }

            // Additional start modules
        }

        function FullAdShow() {
            try {
                if (nowFullAdOpen !== true && ysdk !== null) {
                    ysdk.adv.showFullscreenAdv(
                        {
                            callbacks: {
                                onOpen: () => {
                                    console.log('Open Ad Interstitial');
                                    nowFullAdOpen = true;
                                    if (initGame === true) {
                                        myGameInstance.SendMessage('YandexGame', 'OpenFullAd');
                                    }
                                },
                                onClose: (wasShown) => {
                                    nowFullAdOpen = false;
                                    if (initGame === true) {
                                        if (wasShown) {
                                            myGameInstance.SendMessage('YandexGame', 'CloseFullAd', 'true');
                                        }
                                        else {
                                            myGameInstance.SendMessage('YandexGame', 'CloseFullAd', 'false');
                                        }
                                    }
                                    FocusGame();
                                },
                                onError: (error) => {
                                    console.error('Error Ad Interstitial', error);
                                    myGameInstance.SendMessage('YandexGame', 'ErrorFullAd');
                                    FocusGame();
                                }
                            }
                        });
                }
            }
            catch (e) {
                console.error('CRASH FullAd Show: ', e.message);
            }
        }

        function RewardedShow(id) {
            try {
                ysdk.adv.showRewardedVideo(
                    {
                        callbacks:
                        {
                            onOpen: () => {
                                console.log('Opened Video Ad');
                                myGameInstance.SendMessage('YandexGame', 'OpenVideo');
                            },
                            onClose: () => {
                                console.log('Closed Video Ad');
                                myGameInstance.SendMessage('YandexGame', 'CloseVideo');
                                FocusGame();
                            },
                            onRewarded: () => {
                                console.log('Reward Video Ad');
                                myGameInstance.SendMessage('YandexGame', 'RewardVideo', id);
                            },
                            onError: (e) => {
                                console.error('Error Video Ad', e);
                                myGameInstance.SendMessage('YandexGame', 'ErrorVideo');
                            }
                        }
                    });
            } catch (err) {
                console.error('CRASH Rewarded Video Ad Show: ', err.message);
            }
        }

        function StickyAdActivity(show) {
            try {
                ysdk.adv.getBannerAdvStatus().then(({ stickyAdvIsShowing, reason }) => {
                    if (stickyAdvIsShowing) {
                        if (!show) {
                            ysdk.adv.hideBannerAdv();
                        }
                    }
                    else if (reason) {
                        console.log('Sticky ad are not shown. Reason:', reason);
                    }
                    else if (show) {
                        ysdk.adv.showBannerAdv();
                    }
                })
            } catch (e) {
                console.error('CRASH Sticky Activity: ', e.message);
            }
        }

        function Review() {
            try {
                ysdk.feedback.canReview()
                    .then(({ value, reason }) => {
                        if (value) {
                            ysdk.feedback.requestReview().then(({ feedbackSent }) => {
                                console.log('feedbackSent ', feedbackSent);
                                if (feedbackSent) {
                                    myGameInstance.SendMessage('YandexGame', 'ReviewSent', 'true');
                                    console.log('Review left')
                                }
                                else {
                                    myGameInstance.SendMessage('YandexGame', 'ReviewSent', 'false');
                                    console.log('Review not left', reason)
                                }
                                FocusGame();
                            })
                        }
                        else {
                            console.log('Review can show = false', reason);
                            FocusGame();
                        }
                    })
            } catch (e) {
                console.error('CRASH Review: ', e.message);
                FocusGame();
            }
        }

        function PromptShow() {
            try {
                ysdk.shortcut.showPrompt()
                    .then(result => {
                        console.log('Shortcut created?:', result);
                        if (result.outcome === 'accepted') {
                            console.log('Prompt Success');
                            myGameInstance.SendMessage('YandexGame', 'OnPromptSuccess');
                        }
                        else {
                            myGameInstance.SendMessage('YandexGame', 'OnPromptFail');
                        }
                        FocusGame();
                    });
            } catch (e) {
                console.error('CRASH Prompt Show: ', e.message);
                FocusGame();
            }
        }

        // Additional script modules:

        document.body.appendChild(script);
    </script>
</body>
</html>
