//Using shortcut instead of pipwerks namespace, SCORM 1.2

var XAPI_Manager = (function() {

    let _self = this,
        actor = null,
        actorData = null,
        _xapiHandler = null;

    this.init = function(callback) {
        try {
            _xapiHandler = xapiHandler;
            actor = null;
            actorData = null;
            _xapiHandler.initialize(function() {
             actor = _xapiHandler.getActorDetail();
                let _lData = _xapiHandler.getLoginData();
                let _sData = _xapiHandler.getSuspendData();
                actorData = _sData.replace(/'/g, '"'); 
                if (callback) {
                    callback()
                }
            });
        } catch (e) {
            console.error('	xapiHandler initialize:	', e)
        }
    }

    this.initialization = function() {
        try {
            let p_actor = '',
                p_verb = "initialized",
                p_object = "initialize";
            _xapiHandler.setData(p_actor, p_verb, p_object);
        } catch (err) {
            console.error("xapiIinitialize: ", err.toString());
        }
    }

    this.resume = function() {
        try {
            let p_actor = '',
                p_verb = "resumed",
                p_object = "resume"
            _xapiHandler.setData(p_actor, p_verb, p_object);
        } catch (err) {
            console.error("resume: ", err.toString());
        }
    }


    this.retakeCourse = function() {

    }

    this.setCourseStatus = function(status) {
        let title = "course-complete";
        _self.setPageStatus(title, 'completed')
    };


    this.setPageStatus = function(pTitle, status) {
        //  checkONLineOffline()
        let _status = (status == 'completed') ? status : 'explored',
            p_user = '',
            p_verb = _status,
            p_object = pTitle;
        _xapiHandler.setData(p_user, p_verb, p_object);

    }



    this.set = function(parameter, value) {
        try {

        } catch (err) {
            console.error("xapiManager:	", err.toString())
        }
    }

    this.get = function(parameter) {}


    var getActorDetail = function() {
        try {
            if (actor == null) {
                actor = _xapiHandler.getActorDetail();
            }
            return actor;
        } catch (err) {
            console.error("xapiManager:	", err.toString())
        }
    }

    var setActorDetail = function(user) {
        try {
            if (actor == null) {
                _xapiHandler.setActorDetail(user);
            }
        } catch (err) {
            console.error("xapiManager:	", err.toString())
        }
    }




    var checkONLineOffline = function() {
        try {
            var status = navigator.onLine ? true : false,
                _msg = (status == true) ? "'You are online!" : 'You are offline!'
                // console.log('checkONLineOffline:    ', _msg)

        } catch (err) {
            console.error("xapiManager:	", err.toString())
        }
        return status
    }

    this.setUserData = function(uData) {

        // checkONLineOffline();

        let _uData = JSON.parse(uData);
        try {
            if (actor == null && _uData.userName.firstName !== '') {
                setActorDetail(_uData.userName);
                getActorDetail();
            }
            actorData = JSON.stringify(_uData).replace(/ /g, "_");
        } catch (e) {
            console.error("xapiManager:	", e.toString())
        }

    }


    this.sendToLRS = function(p_verb, uData) {
        try {
            let _uData = JSON.parse(uData),
                _p_actor = '',
                _p_verb = p_verb,
                _p_object = JSON.stringify(_uData).replace(/ /g, "_");
            _xapiHandler.xapiSetData(_p_actor, _p_verb, _p_object);

        } catch (e) {
            console.error("xapiManager:	", e.toString())
        }

    }


    this.getCourseProgressData = function() {
        try {
          let _result = _xapiHandler.getSuspendData();
            return _result;
        } catch (e) {
            console.error("getCourseProgressData:	", e.toString())
        }
    }

    this.setCourseProgressData = function(p_verb, uData) {
        try {
            let _uData = JSON.parse(uData),
                _p_actor = '',
                _p_verb = p_verb,
                _p_object = JSON.stringify(_uData).replace(/"/g, "'");
            _xapiHandler.setSuspendData(_p_actor, _p_verb, _p_object);
        } catch (e) {
            console.error("setCourseProgressData:	", e.toString())
        }
    }


    this.getLoginData = function() {
        try {
            let _result = _xapiHandler.getLoginData();
            return _result;
        } catch (e) {
            console.error("getLoginData:	", e.toString())
        }
    }

    this.setLoginData = function(p_verb, uData) {
        try {
            let _uData = JSON.parse(uData),
                _p_actor = '',
                _p_verb = p_verb,
                _p_object = JSON.stringify(_uData).replace(/"/g, "'");
            _xapiHandler.setLoginData(_p_actor, _p_verb, _p_object);
        } catch (e) {
            console.error("setLoginData:	", e.toString())
        }
    }



    this.getUserData = function(uProp) {
        //   checkONLineOffline()
        // console.log("getUserData:   ", actorData)
        return actorData;
    }

    this.quit = function() {}

    this.getLocation = function(value) {
        try {

        } catch (err) {

        }
    };

    this.setLocation = function(value) {
        try {

        } catch (err) {
            console.error("xapiManager:	", err.toString())
        }
    };

    this.setScore = function(value) {
        try {

        } catch (err) {
            console.error("xapiManager:	", err.toString())
        }
    };

    this.getStatus = function() {
        try {

        } catch (err) {
            console.error("xapiManager:	", err.toString())
        }
    };


    return this;
})