<%- include(versionCheckTemplate, { version: '1.0.0'}) %>
let cc;

function gameStarted() {
    return cc?.director?.getScene()
}

export class Application {
    constructor () {
        this.settingsPath = '<%= settingsJsonPath %>';
        this.showFPS = <%= showFPS %>;
        this.isRB7 = (new URLSearchParams(window.location.search).get('brand') || '').toLowerCase() === 'rb7';
    }
    
    init (engine) {
        cc = engine;
        cc.game.onPostBaseInitDelegate.add(this.onPostInitBase.bind(this));
        cc.game.onPostSubsystemInitDelegate.add(this.onPostSystemInit.bind(this));

        let hasGameStarted = false;
        let hasAnimationFinished = false;
        const self = this;

        const loadLottie = () =>{
            return new Promise((resolve, reject) => {
                const lottie = document.createElement('script');
                lottie.src = 'https://cdnjs.cloudflare.com/ajax/libs/lottie-web/5.9.6/lottie.min.js';
                lottie.onload = () => resolve();
                lottie.onerror = () => reject(new Error('Failed to load lottie'));
                document.head.appendChild(lottie);
            });
        }
        
        const hideLogoScreen = () => {
            if (hasGameStarted && hasAnimationFinished) {
                document.getElementById('VideoWrapper').style.display = 'none';
                document.getElementById('RB7Wrapper').style.display = 'none';
            }
        }

        const initSplashScreen = () => {
            const videoWrapper = document.getElementById('VideoWrapper');
            const rb7Wrapper = document.getElementById('RB7Wrapper');
            
            if(self.isRB7) {
                rb7Wrapper.style.display = 'block';
                loadLottie().then(() => {
                    initRB7Animation();
                })
            } else {
                videoWrapper.style.display = 'block';
            }
        }

        const initRB7Animation = () => {
            // 设置容器样式
            const rb7Wrapper = document.getElementById('RB7Wrapper');
            const rb7Animation = document.getElementById('rb7Animation');
            
            // 设置包裹容器样式
            rb7Wrapper.style.position = 'fixed';
            rb7Wrapper.style.width = '100%';
            rb7Wrapper.style.height = '100%';
            rb7Wrapper.style.top = '0';
            rb7Wrapper.style.left = '0';
            rb7Wrapper.style.background = 'black';
            
            // 设置动画容器样式
            rb7Animation.style.position = 'absolute';            
            rb7Animation.style.top = '50%';
            rb7Animation.style.left = '50%';

            if (window.innerWidth > window.innerHeight) {
                rb7Animation.style.transform = 'translate(-50%, -50%) scale(0.7)';
            } else {
                rb7Animation.style.width = '100%';
                rb7Animation.style.height = '100%';
                rb7Animation.style.transform = 'translate(-50%, -50%) scale(1)';
            }
            

            const animation = lottie.loadAnimation({
                container: rb7Animation,
                renderer: 'svg',
                loop: false,
                autoplay: true,
                path: '/shared/rb7/introl_RB7.json',
                rendererSettings: {
                    preserveAspectRatio: 'xMidYMid meet' // 保持宽高比并居中
                }
            });

            animation.addEventListener('complete', () => {
                hasAnimationFinished = true;
                hideLogoScreen();
            });
        }
            
        const delayTime = self.isRB7 ? 5400 : 2000;
        if(!self.isRB7){
            setTimeout(() => {
                hasAnimationFinished = true;
                hideLogoScreen();
            }, delayTime);
        }

        // Init
        initSplashScreen();

        cc.game.onStart = function () {
            console.log('on game start!');            
            hasGameStarted = true;
            hideLogoScreen();
        };

    }

    onPostInitBase () {
        // cc.settings.overrideSettings('assets', 'server', '');
        // do custom logic
    }

    onPostSystemInit () {
        // do custom logic
    }

    start () {
        return cc.game.init({
            debugMode: <%= debugMode %> ? cc.DebugMode.INFO : cc.DebugMode.ERROR,
            settingsPath: this.settingsPath,
            overrideSettings: {
                // assets: {
                //      preloadBundles: [{ bundle: 'main', version: 'xxx' }],
                // }
                profiling: {
                    showFPS: this.showFPS,
                }
            }
        }).then(() => cc.game.run());
    }
}
