from patch.include import *
from tqdm import tqdm
import csv
import os


@app_context(commit=True)
def patch():
    # TODO1: заменить на INSERT or UPDATE
    if models.CmfSynonym.get():
        print(f'Error! Словарь синонимов не пуст: {models.CmfSynonym.get()}')
        return
    i = 0
    for dictinary, orderno in [['ru_common', 10000], ['ru_it', 1000]]:
        print(f'Загружаем словарь {dictinary}')
        with open(f'/opt/eva-app/patch/cmf_synonym_{dictinary}.dict', 'r', newline='') as csvfile:
            reader = csv.reader(csvfile, delimiter=';',
                                quotechar='"', quoting=csv.QUOTE_MINIMAL)
            values = []
            for row in tqdm(reader):
                word = row[0]
                word_stat = row[1]
                if word_stat:
                    word_stat = float(word_stat)
                else:
                    word_stat = None
                syns = row[2]

                values.append({
                    'id': models.CmfSynonym.gen_id(),
                    'word': word,
                    'syns': syns,
                    'dictinary': dictinary,
                    'word_stat': word_stat,
                    'orderno': orderno,
                })
                # Old version
                # s = models.CmfSynonym()
                # s.name = word
                # s.text = syns
                # s.dict_name = dictinary
                # s.stat_lang_usage = word_stat
                # s.save(emit=False, only_data=True)
                if i % 1000 == 0:
                    sql = '''INSERT INTO cmf_synonym (id, cmf_created_at, cmf_modified_at, cmf_deleted, cmf_archived, system, stat_usage, stat_search, name, text, dict_name, stat_lang_usage, orderno)
                     VALUES (:id, now(), now(), FALSE, FALSE, TRUE, 0, 0, :word, :syns, :dictinary, :word_stat, :orderno)'''
                    models.CmfSynonym.dp.data_driver.Session().execute(sql, values)
                    commit_all_ds()
                    values = []
                i += 1
            if values:
                sql = '''INSERT INTO cmf_synonym (id, cmf_created_at, cmf_modified_at, cmf_deleted, cmf_archived, system, stat_usage, stat_search, name, text, dict_name, stat_lang_usage, orderno)
                 VALUES (:id, now(), now(), FALSE, FALSE, TRUE, 0, 0, :word, :syns, :dictinary, :word_stat, :orderno)'''
                models.CmfSynonym.dp.data_driver.Session().execute(sql, values)
                commit_all_ds()


if __name__ == "__main__":
    os.environ["NO_CACHE"] = "1"
    patch()
