import pandas as pd
from itertools import combinations
from string import ascii_lowercase
import os
import json
import pysolr
import re

from src.scrape_api import get_other_ids

SOLR_URL = 'http://localhost:8983/solr/esperanto'
DOMAIN = 'iiif-test.onb.ac.at'
DOMAIN_IMG = 'iiif.onb.ac.at'

ALTO_PATH = '/home/zenon/Documents/solr/solr-9.4.1/server/solr/esperanto/data/ocr/predictions'


def get_dfs(csv_path):
    csv_files = [f for f in os.listdir(csv_path) if f.endswith('.csv')]
    # csv_files = [os.path.join(csv_path, elem) for elem in csv_files]
    csv_files.sort()
    id_to_df = {}
    for file in csv_files:
        file_path = os.path.join(csv_path, file)
        df = pd.read_csv(file_path, delimiter=';', encoding='utf-8')
        astor_id = os.path.splitext(file)[0]
        id_to_df[astor_id] = df
    return id_to_df


def update_doc(solr, doc):
    try: 
        solr.add([doc])
    except pysolr.SolrError as e:
        print(e)
        print(doc)


if __name__ == '__main__':
    solr = pysolr.Solr(SOLR_URL)
    id_to_other_ids = get_other_ids()
    id_to_ext_id = id_to_other_ids.copy()
    for key, value in id_to_ext_id.items():
        id_to_ext_id[key] = value[-1]
    dfs = get_dfs('../artifacts/mapping_cleaned_links')
    id_it = combinations(list(ascii_lowercase), 5)
    for astor_id, df in dfs.items():
        for row in df.itertuples():
            if row.Images == '' or pd.isnull(row.Images):
                continue
            images = row.Images.split(',')
            article_ids = [
                os.path.splitext(os.path.basename(im))[0] for im in images
            ]
            altos = [
                os.path.join(ALTO_PATH, astor_id, article_id,
                             article_id + '.alto.xml')
                for article_id in article_ids
            ]
            ext_id_str = id_to_ext_id[astor_id]
            manifest_id = 'esperanto' + ''.join(next(id_it))
            id = f'https://{DOMAIN}/presentation/MISC/{manifest_id}'
            doc = {'id': id}
            if not pd.isnull(row.Revuo) and row.Revuo != '':
                doc['revuo'] = row.Revuo
            if not pd.isnull(row.Dato) and row.Dato != ' ':
                dato = re.split('/|,', row.Dato)
                if len(dato) == 2:
                    doc['dato'] = '[' + dato[0] + ' TO ' + dato[1] + ']'
                else:
                    doc['dato'] = row.Dato
            if not pd.isnull(row.Verkinto) and row.Verkinto != '':
                doc['verkinto'] = row.Verkinto
            if not pd.isnull(row.Titolo) and row.Titolo != '':
                doc['titolo'] = row.Titolo
            else:
                continue
            if not pd.isnull(row.Loko) and row.Loko != '':
                doc['loko'] = row.Loko
            if not pd.isnull(row.Lingvo) and row.Lingvo != '':
                doc['lingvo'] = row.Lingvo
            if not pd.isnull(row._11) and row._11 != '':
                doc['ac'] = row._11
            if not pd.isnull(row.Serĉvortoj) and row.Serĉvortoj != '':
                doc['serĉvortoj'] = row.Serĉvortoj
            if len(altos) > 0:
                doc['ocr_text'] = '+'.join(altos)
            update_doc(solr, doc)
    solr.commit()
