declare var pipwerks;
module ScormHelper {
    export class ScormHelper {
        private static isConnected = false;

        constructor() {
            throw new Error("Cannot create new instance of this Class!");
        }
        

        /* Sets score of exam and completion status to completed*/
        static setExamCompleted(scoreMin, scoreMax, score) {   
                 
            console.log("ScoreMin " + scoreMin + " ScoreMax " + scoreMax + " score " + score + " score/scoremax " + score / scoreMax); 
        
            if (this.openConnection()) {              
                var sMin = pipwerks.SCORM.set("cmi.score.min", scoreMin);               
                var sMax = pipwerks.SCORM.set("cmi.score.max", scoreMax);
                var sRaw = pipwerks.SCORM.set("cmi.score.raw", Math.round((score/scoreMax)*100));
                var successStatus = pipwerks.SCORM.set("cmi.success_status", "passed");
                var sScaled = pipwerks.SCORM.set("cmi.score.scaled", String(score / scoreMax));
                var progressMeasure = pipwerks.SCORM.set("cmi.progress_measure", String(score / scoreMax));
                var completionStatus = pipwerks.SCORM.set("cmi.completion_status", "completed");
                if (sMin && sMax && sRaw && successStatus  && completionStatus) {
                    if (pipwerks.SCORM.save()) {
                        pipwerks.SCORM.quit();
                        return;
                    }
                } else {
                    this.handleError("Error: Score couldn't be saved!");
                }
            } else {
                this.handleError("Error: Could not connect with LMS");
            }
            this.handleError("Error: Score couldn't be saved!");
            pipwerks.SCORM.quit();
        }
        /* Sets score of workcard to 100% and completion status to completed*/
        static setWorkCardCompleted() {
            if (this.openConnection()) {
                var sMin = pipwerks.SCORM.set("cmi.score.min", "0");
                var sMax = pipwerks.SCORM.set("cmi.score.max", "100");
                var sRaw = pipwerks.SCORM.set("cmi.score.raw", "100");
                var successStatus = pipwerks.SCORM.set("cmi.success_status", "passed");
                var sScaled = pipwerks.SCORM.set("cmi.score.scaled", "1");
                var progressMeasure = pipwerks.SCORM.set("cmi.progress_measure", "1");
                var completionStatus = pipwerks.SCORM.set("cmi.completion_status", "completed");
                if (sMin && sMax && sRaw && successStatus && sScaled && progressMeasure && completionStatus) {
                    if (pipwerks.SCORM.save()) {
                        pipwerks.SCORM.quit();
                        return;
                    }
                } else {
                    this.handleError("Error: Score couldn't be saved!");
                }
            } else {
                this.handleError("Error: Could not connect with LMS!");
            }
            this.handleError("Error: Score couldn't be saved!");
            pipwerks.SCORM.quit();
        }

        static openConnection() {
            return this.isConnected = pipwerks.SCORM.init();
        }

        static handleError(msg) {
            var c = pipwerks.SCORM.debug.getCode();
            var desc = pipwerks.SCORM.debug.getInfo(c);
            console.error(desc);
            alert(msg);
        }
    }
}