/**
 * @jest-environment jsdom
 */
const fs = require('fs')
const yaml = require('js-yaml');
var beautify = require("json-beautify");

import Ajv from 'ajv'

import ELearning from "..";
import CfCore from "../../../core/CfCore"
import Navigation from "../../Navigation"
import { ContentBlock } from "../../Navigation/typings";
import {
    ModuleAction
    , ExamAction,
    ExamProperties
} from "../typings";
import {
    ELearningTypes,
    QuestionAction
} from "../typings";

let windowSpy
let mockSender;
let mockImpressionManager = {};

describe('ELearning - API', () => {1
    let schemaValidate;

    beforeAll(() => {
        const schemaPath = `${__dirname}/../../../../scripts/standardized-schema.json`
        const jsonSchema = fs.readFileSync(schemaPath, 'utf8')
        const ajv = new Ajv()

        schemaValidate = ajv.compile(JSON.parse(jsonSchema))

        mockSender = {
            add() { return false }
        }
    })

    beforeEach(() => {
        windowSpy = jest.spyOn(window, "window", "get");
    })

    afterEach(() => {
        // to remove the singleton instance
        (CfCore as any).instance = null
    })

    it('Should deliver a "Module" event', () => {
        const senderSpy = jest.spyOn(mockSender, 'add')
        const bsCore = CfCore.createInstance(mockSender as any, mockImpressionManager as any, false, ContentBlock.Core, 'device-id', false)

        const event = {
            id: 'id',
            progress: 78,
            action: ModuleAction.View
        }

        ELearning.logModuleEvent(event)
        const valid = schemaValidate(senderSpy.mock.calls[0][2])
        if (!valid) {
            console.log(beautify(senderSpy.mock.calls[0][2], null, 2, 80))
            console.log(schemaValidate.errors)
        }

        expect(valid).toEqual(true)
    })


    it('Should deliver a "Exam" event', () => {
        const senderSpy = jest.spyOn(mockSender, 'add')
        const bsCore = CfCore.createInstance(mockSender as any, mockImpressionManager as any, false, ContentBlock.Core, 'device-id', false)

        const event = {
            id: 'id',
            action: ExamAction.Start,
        }

        ELearning.logExamEvent(event as ExamProperties)
        const valid = schemaValidate(senderSpy.mock.calls[0][2])
        if (!valid) {
            console.log(beautify(senderSpy.mock.calls[0][2], null, 2, 80))
            console.log(schemaValidate.errors)
        }

        expect(valid).toEqual(true)
    })

    it('Should deliver a "Question" event', () => {
        const senderSpy = jest.spyOn(mockSender, 'add')
        const bsCore = CfCore.createInstance(mockSender as any, mockImpressionManager as any, false, ContentBlock.Core, 'device-id', false)

        const event = {
            id: 'id',
            exam_id: '123',
            answer_id: 'id2',
            action: QuestionAction.Answer
        }

        ELearning.logQuestionEvent(event)

        const valid = schemaValidate(senderSpy.mock.calls[0][2])
        if (!valid) {
            console.log(beautify(senderSpy.mock.calls[0][2], null, 2, 80))
            console.log(schemaValidate.errors)
        }

        expect(valid).toEqual(true)
    })


})