package com.viaccessorca.voplayerdemo.mediamgt;

import android.os.Environment;
import android.util.Log;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class MediaCustomListMapper implements MediasMapperInterface {

    private static final String TAG = "MediaCustomListMapper";

    private Map<String, List<MediaItem>> mappedMedias;

    private String customListFilePath;

    public MediaCustomListMapper(String customListFilePath) {

        this.customListFilePath = customListFilePath;
        this.refresh();
    }

    @Override
    public Map<String, List<MediaItem>> getMappedMedias() {
        return mappedMedias;
    }

    @Override
    public void refresh() {
        parseCustomList();
    }

    private void parseCustomList() {

        Map<String, List<MediaItem>> tmpMap = new HashMap<>(0);
        try {
            InputStream is = new FileInputStream(new File(customListFilePath));

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(is);
            doc.getDocumentElement().normalize();

            NodeList sections = doc.getElementsByTagName("ListSection");
            int nbSections = sections.getLength();

            if(nbSections > 0) {

                for(int s=0; s<nbSections; s++) {

                    Element sectionNode = (Element)sections.item(s);

                    String sectionName = sectionNode.getAttributes().getNamedItem("name").getNodeValue();
                    List<MediaItem> sectionList = new ArrayList<>(0);

                    NodeList streams = sectionNode.getElementsByTagName("Stream");
                    int nbStreams = streams.getLength();
                    if(nbStreams > 0) {

                        for (int i = 0 ; i < nbStreams ; i++) {
                            Element curElement = (Element) streams.item(i);

                            String streamName = curElement.getAttributes().getNamedItem("name").getNodeValue();
                            StreamingMediaItem mediaItem = MediaDescriptionParser.parseMediaItemFromXMLElement( curElement, streamName, Environment.getExternalStorageDirectory().getAbsolutePath() + "/vop_medias/");

                            if(null != mediaItem) {
                                sectionList.add(mediaItem);
                            } else {
                                Log.e(TAG, "readMediaItemFromXMLDesc - No tag URL in directData");
                            }
                        }
                    }
                    if(sectionList.size() > 0 ) {
                        tmpMap.put(sectionName, sectionList);
                    }
                }
                mappedMedias = tmpMap;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }
}
