const {v4: uuidv4} = require("uuid");
const tesseract = require("node-tesseract-ocr");
const OCRResult = require("../models/OCRResult");

function delay(delayInMs) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(2);
        }, delayInMs);
    });
}

const config = {
    lang: "eng",
    oem: 1,
    psm: 3,
}

const MESSAGE_URL_VALIDATION_FR = "Nous avons envoyé un lien par e-mail."
const MESSAGE_URL_VALIDATION_EN = "Please click on the link we sent by email"
const CAPTCHA_ERROR_MESSAGE = "Error verifying captcha, please try again"
const CAPTCHA_ERROR_MESSAGE_FR = "La vérification du captcha a échoué"
const BLOCKED_MSG_EN = "have been blocked"
const BLOCKED_MSG_FR = "avez été bloqué"
const CHECKING_MSG_FR = "Verifying"
const ERR_CACHE_MISS = "ERR_CACHE_MISS"
const SLIDING_CAPTCHA_FR = "Pourquoi cette vérification"
const MESSAGE_FILL_FIELD_FR = "Demande de rendez-vous pour"
const WELCOME_MESSAGE_FR = "Bienvenue dans Chrome"
const PAGE_OPTIMIZATION_CHROME_FR = "Vous pouvez changer d'avis a tout moment dans"
const PAGE_OPTIMIZATION_CHROME_FR_2 = "Vous pouvez modifier vos options a tout moment"
const PAGE_OPTIMIZATION_CHROME_FR_3 = "Vous pouvez effectuer des modifications"
const ONLINE_APPOINTMENT = "Online Appointment"
const CONFIRM_RESEND_FORM_FR = "Confirmer le nouvel envoi"

class OCRChecker {

    constructor(device, contact) {
        this.device = device;
        this.contact = contact;
    }

    get_file_name() {
        let uuid = uuidv4();
        return this.contact.passportNumber + "_" + uuid + ".png"
    }

    async get_result() {
        let fileName = await this.take_screen_shot()
        let result = await tesseract
            .recognize(fileName, config)
        console.log(result)
        if (result.includes(MESSAGE_URL_VALIDATION_EN) || result.includes(MESSAGE_URL_VALIDATION_FR)) {
            return OCRResult.SUCCESS
        } else if (result.includes(CAPTCHA_ERROR_MESSAGE) || result.includes(CAPTCHA_ERROR_MESSAGE_FR)) {
            return OCRResult.RECAPTCHA_ERROR
        } else if (result.includes(BLOCKED_MSG_EN) || result.includes(BLOCKED_MSG_FR)) {
            return OCRResult.BLOCKED
        } else if (result.includes(ERR_CACHE_MISS)) {
            return OCRResult.TO_REFRESH
        } else if (result.includes(CHECKING_MSG_FR)) {
            return OCRResult.RECHECK
        } else if (result.includes(SLIDING_CAPTCHA_FR)) {
            return OCRResult.SLIDING_CAPTCHA
        } else if (result.includes(MESSAGE_FILL_FIELD_FR)) {
            return OCRResult.FILL_FIELD
        } else if (result.includes(WELCOME_MESSAGE_FR)) {
            return OCRResult.NEED_TO_CLICK_LATE_BTN
        } else if (result.toLowerCase().includes(ONLINE_APPOINTMENT.toLowerCase())) {
            return OCRResult.ONLINE_APPOINTMENT
        } else if (result.includes(PAGE_OPTIMIZATION_CHROME_FR) || result.includes(PAGE_OPTIMIZATION_CHROME_FR_2) || result.includes(PAGE_OPTIMIZATION_CHROME_FR_3)) {
            return OCRResult.PAGE_OPTIMIZATION
        } else if (result.includes(CONFIRM_RESEND_FORM_FR)) {
            return OCRResult.CONFIRM_RESEND_FORM
        }
    }

    async take_screen_shot() {
        let name = this.get_file_name()
        await this.device.screenshot({path: name});
        await delay(1000);
        return name
        // console.log(`stdout: ${stdout1}`);
    }
}

module.exports = OCRChecker
