package org.maxwe.epub.parser.meta;

import org.maxwe.epub.parser.constant.XmlLabelName;
import org.maxwe.epub.parser.core.ADocumentParser;
import org.maxwe.epub.parser.core.INavigation;
import org.maxwe.epub.parser.impl.Navigation;
import org.maxwe.epub.parser.meta.xml.NavPoint;
import org.maxwe.epub.parser.meta.xml.Ncx;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.File;
import java.io.FileReader;
import java.util.LinkedList;

/**
 * Created by Pengwei Ding on 2015-09-01 19:40.
 * Email: www.dingpengwei@foxmail.com www.dingpegnwei@gmail.com
 * Description: 解析导航的配置
 */
public class TocNcx extends ADocumentParser {

    private Ncx ncx;

    public TocNcx(String documentPath) throws Exception {
        super(documentPath);
    }

    @Override
    protected void parser() throws Exception {
        XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
        pullParserFactory.setNamespaceAware(true);
        //获取XmlPullParser的实例
        XmlPullParser xmlPullParser = pullParserFactory.newPullParser();
        xmlPullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        //设置输入流xml文件
        xmlPullParser.setInput(new FileReader(new File(this.documentPath)));

        int eventType = xmlPullParser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String nodeName = xmlPullParser.getName();
            switch (eventType) {
                //文档开始
                case XmlPullParser.START_DOCUMENT:
                    break;
                //开始节点
                case XmlPullParser.START_TAG:
                    if (XmlLabelName.NCX.toString().equals(nodeName)) {
                        this.ncx = new Ncx(xmlPullParser);
                    }
                    break;
                //结束节点
                case XmlPullParser.END_TAG:
                    break;
                default:
                    break;
            }
            eventType = xmlPullParser.next();
        }
    }

    public LinkedList<INavigation> getNavigations() {
        LinkedList<INavigation> navigations = new LinkedList<INavigation>();
        LinkedList<NavPoint> navPoints = this.ncx.getNavMap().getNavPoints();
        int index = 0;
        for (NavPoint navPoint : navPoints) {
            Navigation navigation = new Navigation(navPoint.getId(), navPoint.getPlayOrder(), navPoint.getNavLabel().getText().getValue(), navPoint.getContent().getValue());
            navigations.add(navigation);
            if (navPoint.getSubNavPoints() != null){
                test(navPoint,navigation);
            }
            index++;
        }
        return navigations;
    }


    public LinkedList<INavigation> getNavigations(String containerPath) {
        LinkedList<INavigation> navigations = new LinkedList<INavigation>();
        LinkedList<NavPoint> navPoints = this.ncx.getNavMap().getNavPoints();
        int index = 0;
        for (NavPoint navPoint : navPoints) {
            navigations.add(new Navigation(navPoint.getId(), navPoint.getPlayOrder(), navPoint.getNavLabel().getText().getValue(), this.pathLinker(containerPath, navPoint.getContent().getValue())));
            index++;
        }
        return navigations;
    }


    private void test(NavPoint navPoint,Navigation navigation){
        LinkedList<NavPoint> subNavPoints = navPoint.getSubNavPoints();
        if (subNavPoints == null){
            return;
        }else{
            int index = 0;
            for (NavPoint subNavPoint:subNavPoints){
                Navigation subNavigation = new Navigation(subNavPoint.getId(), navPoint.getPlayOrder(), subNavPoint.getNavLabel().getText().getValue(), subNavPoint.getContent().getValue());
                navigation.getSubNavigations().add(subNavigation);
                test(subNavPoint,subNavigation);
                index++;
            }
        }
    }

}

@@ -2248,39 +2248,16 @@
 ints();%0A
-        int index = 0;%0A
         
@@ -2578,20 +2578,28 @@
         
-test
+buildNavTree
 (navPoin
@@ -2600,16 +2600,17 @@
 avPoint,
+ 
 navigati
@@ -2628,37 +2628,16 @@
       %7D%0A
-            index++;%0A
         
@@ -2903,39 +2903,16 @@
 ints();%0A
-        int index = 0;%0A
         
@@ -2953,33 +2953,33 @@
 ) %7B%0A            
-n
+N
 avigations.add(n
@@ -2971,22 +2971,30 @@
 vigation
-s.add(
+ navigation = 
 new Navi
@@ -3157,31 +3157,169 @@
 ()))
-);%0A            index++;
+;%0A            navigations.add(navigation);%0A            if (navPoint.getSubNavPoints() != null)%7B%0A                buildNavTree(navPoint, navigation);%0A            %7D
 %0A   
@@ -3378,20 +3378,28 @@
 te void 
-test
+buildNavTree
 (NavPoin
@@ -3409,16 +3409,17 @@
 avPoint,
+ 
 Navigati
@@ -3580,35 +3580,8 @@
 se%7B%0A
-            int index = 0;%0A
     
@@ -3700,33 +3700,36 @@
 vPoint.getId(), 
-n
+subN
 avPoint.getPlayO
@@ -3909,12 +3909,20 @@
     
-test
+buildNavTree
 (sub
@@ -3930,16 +3930,17 @@
 avPoint,
+ 
 subNavig
@@ -3951,33 +3951,8 @@
 n);%0A
-                index++;%0A
     

fix play order bug