package org.lockss.plugin.resiliencealliance;

import org.lockss.daemon.PluginException;
import org.lockss.extractor.ArticleMetadataExtractor;
import org.lockss.extractor.ArticleMetadataExtractorFactory;
import org.lockss.extractor.BaseArticleMetadataExtractor;
import org.lockss.extractor.MetadataTarget;
import org.lockss.plugin.ArchivalUnit;
import org.lockss.plugin.ArticleFiles;
import org.lockss.plugin.ArticleIteratorFactory;
import org.lockss.plugin.SubTreeArticleIteratorBuilder;
import org.lockss.util.Logger;

import java.util.Iterator;
import java.util.regex.Pattern;

public class ResilienceAllianceArticleIteratorFactory implements ArticleIteratorFactory,
        ArticleMetadataExtractorFactory {

    protected static Logger log = Logger.getLogger(ResilienceAllianceArticleIteratorFactory .class);

    /*
    https://www.ace-eco.org/vol14/iss1/art1
    https://www.ace-eco.org/vol14/iss1/art1/ACE-ECO-2018-1293.pdf
    https://www.ace-eco.org/vol14/iss1/art1/appendix1.pdf
    https://www.ace-eco.org/vol14/iss2/art9/eqn1.gif -- this one will throw exception
     */

    protected static final String ROOT_TEMPLATE = "\"%s\", base_url";
    protected static final String PATTERN_TEMPLATE =
            "\"%s(vol%s/iss[^/]+/art\\.*)\", base_url, volume_name";

    private static final Pattern HTML_PATTERN = Pattern.compile(
            "/(vol[^/]+/iss[^/]+/art[^/]+)",
            Pattern.CASE_INSENSITIVE);
    private static final String HTML_REPLACEMENT = "/$1";

    private static final Pattern PDF_PATTERN = Pattern.compile(
            "/(vol[^/]+/iss[^/]+/art[^/]+)(/ACE.*\\.pdf)",
            Pattern.CASE_INSENSITIVE);
    private static final String PDF_REPLACEMENT = "/$1/$2";


    @Override
    public Iterator<ArticleFiles> createArticleIterator(ArchivalUnit au, MetadataTarget target)
            throws PluginException {
        SubTreeArticleIteratorBuilder builder = new SubTreeArticleIteratorBuilder(au);

        builder.setSpec(target,
                ROOT_TEMPLATE,
                PATTERN_TEMPLATE, Pattern.CASE_INSENSITIVE);

        builder.addAspect(
                PDF_PATTERN,
                PDF_REPLACEMENT,
                ArticleFiles.ROLE_FULL_TEXT_PDF);

        builder.addAspect(
                HTML_PATTERN,
                HTML_REPLACEMENT,
                ArticleFiles.ROLE_ABSTRACT,
                ArticleFiles.ROLE_FULL_TEXT_HTML,
                ArticleFiles.ROLE_ARTICLE_METADATA);

        builder.setFullTextFromRoles(ArticleFiles.ROLE_FULL_TEXT_PDF);

        return builder.getSubTreeArticleIterator();
    }

    @Override
    public ArticleMetadataExtractor createArticleMetadataExtractor(MetadataTarget target)
            throws PluginException {
        return new BaseArticleMetadataExtractor(ArticleFiles.ROLE_ARTICLE_METADATA);
    }

}



@@ -1218,17 +1218,16 @@
    %22%5C%22%25s
-(
 vol%25s/is
@@ -1240,13 +1240,14 @@
 /art
-%5C%5C.*)
+%5B%5E/%5D+$
 %5C%22, 
@@ -1378,16 +1378,17 @@
 rt%5B%5E/%5D+)
+$
 %22,%0A     
@@ -1484,230 +1484,8 @@
 %22;%0A%0A
-    private static final Pattern PDF_PATTERN = Pattern.compile(%0A            %22/(vol%5B%5E/%5D+/iss%5B%5E/%5D+/art%5B%5E/%5D+)(/ACE.*%5C%5C.pdf)%22,%0A            Pattern.CASE_INSENSITIVE);%0A    private static final String PDF_REPLACEMENT = %22/$1/$2%22;%0A
 %0A%0A  
@@ -1841,25 +1841,24 @@
 ITIVE);%0A
-%0A
         
 builder.
@@ -1853,139 +1853,8 @@
     
-builder.addAspect(%0A                PDF_PATTERN,%0A                PDF_REPLACEMENT,%0A                ArticleFiles.ROLE_FULL_TEXT_PDF);%0A
 %0A   
@@ -2035,16 +2035,16 @@
 T_HTML,%0A
+
         
@@ -2093,80 +2093,8 @@
 );%0A%0A
-        builder.setFullTextFromRoles(ArticleFiles.ROLE_FULL_TEXT_PDF);%0A%0A
     

make the ArticleIterator using html file instead of PDF