import { gramaticalDetail, Word } from "../shared/models/word";
import { getDatabaseClient, DB_NAME, connectToDatabaseServer } from "../db-utils";
import da from '../default-library/DA.json';
import db from '../default-library/DB.json';
import dc from '../default-library/DC.json';
import dd from '../default-library/DD.json';
import de from '../default-library/DE.json';
import df from '../default-library/DF.json';
import dg from '../default-library/DG.json';
import dh from '../default-library/DH.json';
import di from '../default-library/DI.json';
import dj from '../default-library/DJ.json';
import dk from '../default-library/DK.json';
import dl from '../default-library/DL.json';
import dm from '../default-library/DM.json';
import dn from '../default-library/DN.json';
import doy from '../default-library/DO.json';
import dp from '../default-library/DP.json';
import dq from '../default-library/DQ.json';
import dr from '../default-library/DR.json';
import ds from '../default-library/DS.json';
import dt from '../default-library/DT.json';
import du from '../default-library/DU.json';
import dv from '../default-library/DV.json';
import dw from '../default-library/DW.json';
import dx from '../default-library/DX.json';
import dy from '../default-library/DY.json';
import dz from '../default-library/DZ.json';

const defaultLibFiles = [da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm, dn, doy, dp, dq, dr, ds, dt, du, dv, dw, dx, dy, dz];


(async function compile_default_library_to_db() {
    await connectToDatabaseServer();

    const wordCollection = getDatabaseClient().db(DB_NAME).collection<Word>('words');

    let allWords: Array<Word> = [];

    for (let fileIndex = 0; fileIndex < defaultLibFiles.length; fileIndex += 1) {
        const file: any = defaultLibFiles[fileIndex];
    
        const allKeys: Array<string> = (Object.keys(file));

        for (let i = 0; i < allKeys.length; i += 1) {
            // file[allKeys[i]]
            let wordName = allKeys[i];
            let wordDetails = file[allKeys[i]];

            let gramaticalDetailKeys: Array<string> = [];
            let grammaticalDetails : Array<gramaticalDetail> = [];

            if (wordDetails && wordDetails['MEANINGS']) {
                gramaticalDetailKeys = (Object.keys(wordDetails['MEANINGS']));

                for (let j = 0; j < gramaticalDetailKeys.length; j += 1) {
                    if (wordDetails['MEANINGS'] && wordDetails['MEANINGS'][gramaticalDetailKeys[j]]) {
                        grammaticalDetails.push({
                            type: wordDetails['MEANINGS'][gramaticalDetailKeys[j]][0],
                            description: wordDetails['MEANINGS'][gramaticalDetailKeys[j]][1],
                            context: wordDetails['MEANINGS'][gramaticalDetailKeys[j]][2],
                            examples: wordDetails['MEANINGS'][gramaticalDetailKeys[j]][3]
                        });
                    }
                }
            }

            allWords.push({
                name: wordName,
                languageType: 'ENGLISH',
                similarWords: wordDetails && wordDetails['SYNONYMS'] ? wordDetails['SYNONYMS'] : [],
                oppositeWords: wordDetails && wordDetails['ANTONYMS'] ? wordDetails['ANTONYMS'] : [],
                grammaticalDetails: grammaticalDetails
            });        
        }
    }

    const newWordCollection = await wordCollection.insertMany(allWords);

    console.log(newWordCollection.acknowledged);

    return;

}())