const SCORM = (function() {
    let initialized = false;

    function initialize() {
        if (initialized) {
            console.log('SCORM already initialized');
            return true;
        }

        try {
            const result = doLMSInitialize();
            if (result === "true") {
                initialized = true;
                console.log('SCORM initialized successfully');
                return true;
            }
            console.error('Failed to initialize SCORM');
            return false;
        } catch (error) {
            console.error('SCORM initialization error:', error);
            return false;
        }
    }

    function terminate() {
        if (!initialized) {
            console.error('SCORM not initialized');
            return false;
        }

        try {
            const result = doLMSFinish();
            if (result === "true") {
                initialized = false;
                console.log('SCORM terminated successfully');
                return true;
            }
            console.error('Failed to terminate SCORM');
            return false;
        } catch (error) {
            console.error('SCORM termination error:', error);
            return false;
        }
    }

    function getValue(parameter) {
        if (!initialized) {
            console.error('SCORM not initialized');
            return null;
        }

        try {
            const value = doLMSGetValue(parameter);
            const errorCode = doLMSGetLastError();
            
            if (errorCode !== "0") {
                const errorString = doLMSGetErrorString(errorCode);
                console.error(`Error getting ${parameter}:`, errorString);
                return null;
            }
            return value;
        } catch (error) {
            console.error(`Error getting ${parameter}:`, error);
            return null;
        }
    }

    function setValue(parameter, value) {
        if (!initialized) {
            console.error('SCORM not initialized');
            return false;
        }

        try {
            const result = doLMSSetValue(parameter, value);
            if (result === "false") {
                const errorCode = doLMSGetLastError();
                const errorString = doLMSGetErrorString(errorCode);
                console.error(`Failed to set ${parameter}:`, errorString);
                return false;
            }
            return true;
        } catch (error) {
            console.error(`Error setting ${parameter}:`, error);
            return false;
        }
    }

    function commit() {
        if (!initialized) {
            console.error('SCORM not initialized');
            return false;
        }

        try {
            const result = doLMSCommit();
            if (result === "false") {
                console.error('Failed to commit data');
                return false;
            }
            return true;
        } catch (error) {
            console.error('Error committing data:', error);
            return false;
        }
    }

    return {
        initialize,
        terminate,
        getValue,
        setValue,
        commit
    };
})();

// Auto-initialize SCORM when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
    SCORM.initialize();
});

// Auto-commit data before unload
window.addEventListener('beforeunload', () => {
    SCORM.commit();
});