import GlobalVariable as MYSY_GV
from pymongo import MongoClient
from collections import Counter

import prj_common as mycommon
from unidecode import unidecode
import inspect
import sys, os
from autocorrect import Speller
from datetime import datetime
import re
import Ela_Spacy as ls




def YTUBES_ela_index_class_transcription(external_code="", source_field = ""):
    try:

        collection = MYSY_GV.YTUBES_dbname["mysyclass"]
        collection = MYSY_GV.dbname["myclass"]

        if( len(str(source_field)) == 0):
            source_field = 'default'

        print(" external_code = "+str(external_code)+ " source_field = "+str(source_field))
        for doc in collection.find({"external_code":external_code, 'owner':'youtubes'}):

            # Indexation du champ : video_text , donc la transcription d'une video
            if (str(source_field) == "video_text"):
                print(" lalalalal")

                # Recuperation du nom du fichier
                if ("transciption_file_name" in doc.keys()):
                    file_name = doc["transciption_file_name"]
                    print(' file_name = ' + str(file_name))
                    status, Ytubes_text = YTUBES_Read_Transcritpion(file_name)
                    if (status is False):
                        return False

                print(" text indexer "+ Ytubes_text[:MYSY_GV.MAX_CARACT] + " ...")

                # mycommon.myprint(" external_code = "+str(doc["external_code"])+" ==> "+str(chaine))
                retval = ls.YTUBES_ela_index_record_field(Ytubes_text, str(doc["external_code"]), source_field)

                '''
                # Apres indexation, on met a jour la formation pour 
                # mettre le champ "indexe" a 1.
                '''
                if (retval):
                    # mise à jour du champ "indexed"
                    myobjectif = {}
                    myobjectif['indexed_video_text'] = '1'

                    ret_val2 = collection.find_and_modify(query={"external_code": external_code},
                                                          update={"$set": myobjectif}
                                                          )

                    if (ret_val2 and ret_val2['_id']):
                        mycommon.myprint("La formation indexée YTUBES (champs video_text) =" + str(ret_val2['_id']))
                        return True
                    else:
                        return False

        return True
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
        return False



'''
Cette fonction va lire la transcription youtubes qui se trouve
dans un fichier '''

def YTUBES_Read_Transcritpion(transcitp_file_name = None):
    try:
        text = ""
        with open(transcitp_file_name, encoding="utf-8") as f:
            text = text + f.read()

        return True, text


    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
        return False, ""



'''
'''
def ela_index_all_ytubes_video_transcription():
    try:

        print(" on est dans ytubes read")
        Y_collection = MYSY_GV.YTUBES_dbname["mysyclass"]
        Y_collection = MYSY_GV.dbname["myclass"]


        Ytubes_text = ""
        nb_class_indexed = 0
        for doc in Y_collection.find({'indexed_video_text': '0', 'owner':'youtubes'}):

            if ("transciption_file_name" in doc.keys()):
                retval = YTUBES_ela_index_class_transcription(str(doc["external_code"]), "video_text")
                # print(" apres : ela_index_all_classes_video_texte retval =  " + str(retval))

                if (retval is False):
                    mycommon.myprint("IMPOSSIBLE d'indexer la formation = " + str(doc["external_code"]))
                else:
                    nb_class_indexed = nb_class_indexed + 1

                # retval = ls.ela_index_record_field(chaine, str(doc["external_code"]))


        mycommon.myprint(str(nb_class_indexed) + " ont été indexes YTUBES- champ video_text")
        return True, str(nb_class_indexed) + " ont été indexes YTUBES - champ video_text "

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
        return False