package org.buildmlearn.toolkit.learnspelling.data;

import android.content.ContentValues;
import android.content.Context;
import android.os.AsyncTask;

import org.buildmlearn.toolkit.learnspelling.Constants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

/**
 * Created by Anupam (opticod) on 11/6/16.
 */

/**
 * @brief Used to parse XML and save in database for learn spelling template's simulator.
 */
public class FetchXMLTask extends AsyncTask<String, Void, Void> {

    private final Context mContext;

    public FetchXMLTask(Context context) {
        mContext = context;
    }

    private static String getValue(String tag, Element element) {
        NodeList nodeList = null;
        NodeList node1 = element.getElementsByTagName(tag);
        if (node1 != null && node1.getLength() != 0)
            nodeList = node1.item(0).getChildNodes();
        if (nodeList == null)
            return "";
        else if (nodeList.getLength() == 0)
            return "";
        else {
            Node node = nodeList.item(0);
            return node.getNodeValue();
        }
    }

    private void saveSpellData(ArrayList<SpellModel> Spells) {

        Vector<ContentValues> cVVector = new Vector<>(Spells.size());

        for (int i = 0; i < Spells.size(); i++) {

            String word;
            String meaning;

            SpellModel SpellSpell = Spells.get(i);

            word = SpellSpell.getWord();
            meaning = SpellSpell.getMeaning();

            ContentValues SpellValues = new ContentValues();

            SpellValues.put(SpellContract.Spellings.WORD, word);
            SpellValues.put(SpellContract.Spellings.MEANING, meaning);
            SpellValues.put(SpellContract.Spellings.ATTEMPTED, 0);

            cVVector.add(SpellValues);
        }
        // add to database
        if (cVVector.size() > 0) {
            ContentValues[] cvArray = new ContentValues[cVVector.size()];
            cVVector.toArray(cvArray);
            SpellDb db = new SpellDb(mContext);
            db.open();
            db.bulkInsertSpellings(cvArray);
            db.close();
        }
    }

    @Override
    protected Void doInBackground(String... params) {

        if (params.length == 0) {
            return null;
        }
        String fileName = params[0];
        ArrayList<SpellModel> mList;

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        dbf.setValidating(false);

        DocumentBuilder db;
        Document doc;
        try {
            File fXmlFile = new File(Constants.XMLFileName);
            db = dbf.newDocumentBuilder();
            doc = db.parse(fXmlFile);
            doc.normalize();
            mList = new ArrayList<>();

            NodeList childNodes = doc.getElementsByTagName("item");

            for (int i = 0; i < childNodes.getLength(); i++) {
                SpellModel app = new SpellModel();

                Node child = childNodes.item(i);

                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    Element element2 = (Element) child;

                    app.setWord(getValue("word", element2));
                    app.setMeaning(getValue("meaning", element2));

                }
                mList.add(app);
            }
            saveSpellData(mList);
        } catch (ParserConfigurationException e) {
            return null;
        } catch (FileNotFoundException e) {
            return null;
        } catch (SAXException e) {
            return null;
        } catch (IOException e) {
            return null;
        }
        return null;
    }
}