const {v4: uuidv4} = require("uuid");
const OCRResult = require("../models/OCRResult");
const fs = require("fs");
const {exec} = require("child_process");
const {findText} = require("../utiles/CmdUtils");

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

const NO_INTERNET_FR = "Aucun accés a Internet"
const NO_INTERNET_FR_2 = "Aucun accès à Internet"
const BRAVE_VPN_SKIP = "Pare-feu + VPN Brave"
const MESSAGE_URL_VALIDATION_FR = "Vous recevrez un email de validation"
const MESSAGE_URL_VALIDATION_FR_2 = "Merci de votre intérêt pour notre Maison"
const SSL_CERT_ERROR = " Votre connexion n'est pas privée"
const MESSAGE_URL_VALIDATION_EN = "Please click on the link we sent by email"
const CHOOSE_POSITION_GOOGLE_FR = " Choisir la position pour les résultats de recherche"
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 BLOCKED_MSG_FR_3 = "a été bloqué"
const BLOCKED_MSG_FR_2 = "Pourquoi ce blocage"
const CHECKING_MSG_FR = "Verifying"
const CHECKING_MSG_FR_2 = "Vérification de l'appareil"
const ERR_CACHE_MISS = "ERR_CACHE_MISS"
const ERR_CACHE_MISS_2 = "ERR_CACHE-MISS"
const ERR_CACHE_MISS_3 = "appuyer sur le bouton d'actualisation"
const ERR_CACHE_MISS_4 = "renvoyer les données"
const ERR_EMPTY_RESPONSE = "ERR_EMPTY_RESPONSE"
const ERR_SSL_PROTOCOL = "SSL_PROTOCOL_ERROR"
const SLIDING_CAPTCHA_FR = "Pourquoi cette vérification"
const SLIDING_CAPTCHA_LOADING_FR = "Chargement."
const SLIDING_CAPTCHA_FR_2 = "Glissez vers la droite pour"
const SLIDING_CAPTCHA_FR_3 = "la droite pour completer le puzzle"
const SLIDING_CAPTCHA_FR_4 = " s'assure qu'on s'adresse bien"
const SLIDING_CAPTCHA_FR_5 = "On s'assure que c'est"
const SLIDING_CAPTCHA_FR_6 = "s'assure que cest bien vous"
const MESSAGE_FILL_FIELD_FR = "Demande de rendez-vous pour"
const MESSAGE_FILL_FIELD_FR_2 = "des champs de données doivent étre complétés"
const MESSAGE_FILL_FIELD_FR_3 = "Sans préféré"
const MESSAGE_FILL_FIELD_FR_4 = "Magasin préféré"
const MESSAGE_FILL_FIELD_FR_5 = "email vous sera envoyé pour vous"
const MESSAGE_FILL_FIELD_FR_6 = "Prénom* Téléphone*"
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_6 = "Vous pouvez changer davis a tout moment"
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 PAGE_OPTIMIZATION_CHROME_FR_4 = "de nouvelles fonctionnalités"
const PAGE_OPTIMIZATION_CHROME_FR_5 = "Avec la mesure des performance"
const PAGE_OPTIMIZATION_CHROME_FR_7 = "Les annonces suggérées par les"
const PAGE_OPTIMIZATION_CHROME_FR_8 = "Chrome estime vos centres"
const ONLINE_APPOINTMENT = "Online Appointment"
const CONFIRM_RESEND_FORM_FR = "Confirmer le nouvel envoi"
const CLOSED_MESSAGE_FR = "Depuis plus de 130 ans"
const DIALOG_TO_SKIP = "facilement les commandes"
const ABOUT_BLANK = "about:blank"
const GOOGLE_DISCONNECT_FR_1 = "Connectez-vous a Google"
const GOOGLE_DISCONNECT_FR = "Rester déconnecté"
const RECAPTCHA_FAILED_FR = "captcha a échoué"
const BRAVE_NOTIFICATION = "notifications Brave"
const BRAVE_SKIP_PUB = "Appuyez pour voir les boucliers"
const BRAVE_SKIP_PUB_2 = "aucune pubilicite"
const BRAVE_SKIP_PUB_3 = "aucun pubilicité"
const BRAVE_SKIP_PUB_5 = "aucune traceur"
const BRAVE_SKIP_PUB_4 = "aucune publicité"
const BRAVE_SKIP_DEFAULT_PAGE = "confidentialité de Brave"
const BRAVE_SKIP_DEFAULT_PAGE_2 = "définissant Brave"
const BRAVE_SKIP_PRIVACY_PAGE = "Partagez des informations"
const BRAVE_SKIP_PRIVACY_PAGE_3 = "Partagez de informations"
const BRAVE_SKIP_PRIVACY_PAGE_2 = "Partagez des renseignements"

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()
        await delay(1000)
        try {
            let result = await findText(fileName)
            console.log(result)
            if (result.includes(MESSAGE_URL_VALIDATION_EN) || result.includes(MESSAGE_URL_VALIDATION_FR) || result.includes(MESSAGE_URL_VALIDATION_FR_2)) {
                await this.deleteFile(fileName)
                return OCRResult.SUCCESS
            } else if (result.includes(BRAVE_NOTIFICATION)) {
                await this.deleteFile(fileName)
                return OCRResult.BRAVE_NOTIFICATION
            } else if (result.includes(CHOOSE_POSITION_GOOGLE_FR)) {
                await this.deleteFile(fileName)
                return OCRResult.CHOOSE_POSITION
            } else if (result.includes(BRAVE_VPN_SKIP)) {
                await this.deleteFile(fileName)
                return OCRResult.BRAVE_VPN_SKIP
            } else if (result.includes(SSL_CERT_ERROR)) {
                await this.deleteFile(fileName)
                return OCRResult.SSL_CERT_ERROR
            } else if (result.includes(MESSAGE_FILL_FIELD_FR) || result.includes(MESSAGE_FILL_FIELD_FR_2) || result.includes(MESSAGE_FILL_FIELD_FR_3) || result.includes(MESSAGE_FILL_FIELD_FR_4) || result.includes(MESSAGE_FILL_FIELD_FR_5) || result.includes(MESSAGE_FILL_FIELD_FR_6)) {
                await this.deleteFile(fileName)
                return OCRResult.FILL_FIELD
            } 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) || result.includes(BLOCKED_MSG_FR_2) || result.includes(BLOCKED_MSG_FR_3)) {
                await this.deleteFile(fileName)
                return OCRResult.BLOCKED
            } else if (result.includes(ERR_CACHE_MISS) || result.includes(ERR_CACHE_MISS_2) || result.includes(ERR_CACHE_MISS_3)
                || result.includes(ERR_CACHE_MISS_4) || result.includes(ERR_EMPTY_RESPONSE)) { //|| result.includes(ERR_SSL_PROTOCOL)
                await this.deleteFile(fileName)
                return OCRResult.TO_REFRESH
            } else if (result.includes(CHECKING_MSG_FR) || result.includes(CHECKING_MSG_FR_2)) {
                await this.deleteFile(fileName)
                return OCRResult.RECHECK
            } else if (result.includes(BRAVE_NOTIFICATION)) {
                await this.deleteFile(fileName)
                return OCRResult.BRAVE_NOTIFICATION
            } else if (result.includes(BRAVE_SKIP_PUB) || result.includes(BRAVE_SKIP_PUB_2) || result.includes(BRAVE_SKIP_PUB_3) || result.includes(BRAVE_SKIP_PUB_4) || result.includes(BRAVE_SKIP_PUB_5)) {
                await this.deleteFile(fileName)
                return OCRResult.BRAVE_PRIVACY_PUB
            } else if (result.includes(SLIDING_CAPTCHA_FR) || result.includes(SLIDING_CAPTCHA_FR_2) || result.includes(SLIDING_CAPTCHA_FR_3) || result.includes(SLIDING_CAPTCHA_FR_4) || result.includes(SLIDING_CAPTCHA_FR_5) || result.includes(SLIDING_CAPTCHA_FR_6)) {
                await this.deleteFile(fileName)
                if (result.includes(SLIDING_CAPTCHA_LOADING_FR)) {
                    return OCRResult.SLIDING_CAPTCHA_LOADING
                }
                // await this.deleteFile(screenShot)
                // if (result.includes("rac"))
                //     return OCRResult.SLIDING_CAPTCHA_REFRESH
                return OCRResult.SLIDING_CAPTCHA
            } else if (result.includes(WELCOME_MESSAGE_FR)) {
                await this.deleteFile(fileName)
                return OCRResult.NEED_TO_CLICK_LATE_BTN
            } else if (result.includes(GOOGLE_DISCONNECT_FR) || result.includes(GOOGLE_DISCONNECT_FR_1)) {
                await this.deleteFile(fileName)
                return OCRResult.GOOGLE_DISCONNECT
            } else if (result.toLowerCase().includes(ONLINE_APPOINTMENT.toLowerCase())) {
                await this.deleteFile(fileName)
                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)
                || result.includes(PAGE_OPTIMIZATION_CHROME_FR_4) || result.includes(PAGE_OPTIMIZATION_CHROME_FR_5)
                || result.includes(PAGE_OPTIMIZATION_CHROME_FR_6)
                || result.includes(PAGE_OPTIMIZATION_CHROME_FR_8)
                || result.includes(PAGE_OPTIMIZATION_CHROME_FR_7)
            ) {
                await this.deleteFile(fileName)
                return OCRResult.PAGE_OPTIMIZATION
            } else if (result.includes(CONFIRM_RESEND_FORM_FR)) {
                await this.deleteFile(fileName)
                return OCRResult.CONFIRM_RESEND_FORM
            } else if (result.includes(CLOSED_MESSAGE_FR)) {
                return OCRResult.CLOSED
            } else if (result.includes(ABOUT_BLANK)) {
                return OCRResult.TERMINAED
            } else if (result.includes(DIALOG_TO_SKIP)) {
                await this.deleteFile(fileName)
                return OCRResult.TO_SKIP
            } else if (result.includes(RECAPTCHA_FAILED_FR)) {
                return OCRResult.TERMINAED
            } else if (result.includes(NO_INTERNET_FR) || result.includes(NO_INTERNET_FR_2)) {
                await this.deleteFile(fileName)
                return OCRResult.NO_INTERNET
            } else if (result.includes(BRAVE_SKIP_DEFAULT_PAGE) || result.includes(BRAVE_SKIP_DEFAULT_PAGE_2)) {
                await this.deleteFile(fileName)
                return OCRResult.BRAVE_SKIP
            } else if (result.includes(BRAVE_SKIP_PRIVACY_PAGE) || result.includes(BRAVE_SKIP_PRIVACY_PAGE_2) || result.includes(BRAVE_SKIP_PRIVACY_PAGE_3)) {
                await this.deleteFile(fileName)
                return OCRResult.BRAVE_PRIVACY
            } else {
                return OCRResult.TERMINAED
            }
        } catch (e) {
            console.log(e)
            return OCRResult.TERMINAED
        }
    }

    async deleteFile(filename) {
        try {
            fs.unlinkSync(filename)
        } catch (e) {
            console.log(e)
            return OCRResult.TERMINAED
        }
    }

    async take_screen_shot() {
        let name = this.get_file_name()
        console.log("will take screenshot for " + this.device.model() + ":" + this.device.serial())
        console.log("name is  " + name)
        let stdout1 = await exec("adb -s " + this.device.serial() + " exec-out screencap -p > " + name)
        await delay(5000);
        console.log(`stdout: ${stdout1}`);
        return name
    }
}

module.exports = OCRChecker
