import ELearningPropertiesTI from './typings-ti'

import {
    ELearningTypes,
    ModuleProperties,
    QuestionProperties,
    ExamProperties, ExamAction,
} from "./typings"

import { injectEvent } from "../../core/injector"
import { ContentBlock } from '../Navigation/typings'
import CfExceptionHandler from "../../core/CfExceptionHandler";

const moduleName = ContentBlock.ELearning

const logModuleEvent = (properties: ModuleProperties, sendNow = false) => {

    if(properties.id == ''){
        new CfExceptionHandler().throwAndLogError(ELearningTypes.Module, 'empty module id')
    }else if(properties.action == undefined){
        new CfExceptionHandler().throwAndLogError(ELearningTypes.Module, 'invalid module action')
    }else if(properties.progress == undefined){
        new CfExceptionHandler().throwAndLogError(ELearningTypes.Module, 'invalid module progress')
    }

    injectEvent(
        properties, 
        [ELearningPropertiesTI], 
        ELearningTypes.Module, 
        moduleName, 
        '',
        sendNow)
}

const logExamEvent = (properties: ExamProperties, sendNow = false) => {

    if(properties.id == ''){
        new CfExceptionHandler().throwAndLogError(ELearningTypes.Exam, 'empty exam id')
    }else if(properties.action == undefined){
        new CfExceptionHandler().throwAndLogError(ELearningTypes.Exam, 'invalid exam action')
    }else if(properties.action == ExamAction.Submit && properties.duration < 0){
        new CfExceptionHandler().throwAndLogError(ELearningTypes.Exam, 'exam duration value required for submit action')
    }else if(properties.action == ExamAction.Result && properties.score == undefined){
        new CfExceptionHandler().throwAndLogError(ELearningTypes.Exam, 'exam score required for result action')
    }else if(properties.action == ExamAction.Result && properties.is_passed == undefined){
        new CfExceptionHandler().throwAndLogError(ELearningTypes.Exam, 'exam is_passed required for result action')
    }

    injectEvent(
        properties, 
        [ELearningPropertiesTI], 
        ELearningTypes.Exam, 
        moduleName,
        '', 
        sendNow)
}

const logQuestionEvent = (properties: QuestionProperties, sendNow = false) => {

    if(properties.id == ''){
        new CfExceptionHandler().throwAndLogError(ELearningTypes.Question, 'empty question id')
    }else if(properties.action == undefined){
        new CfExceptionHandler().throwAndLogError(ELearningTypes.Question, 'invalid question action')
    }else if(properties.answer_id == ''){
        new CfExceptionHandler().throwAndLogError(ELearningTypes.Question, 'empty question answer id')
    }else if(properties.exam_id == ''){
        new CfExceptionHandler().throwAndLogError(ELearningTypes.Question, 'empty question exam id')
    }

    injectEvent(
        properties, 
        [ELearningPropertiesTI], 
        ELearningTypes.Question, 
        moduleName,
        '',
        sendNow)
}

export default {
    logModuleEvent,
    logQuestionEvent,
    logExamEvent
}