package org.lockss.plugin.wroclawmedicaluniversity;

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.plugin.anu.AnuArticleIteratorFactory;
import org.lockss.util.Logger;

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

public class WroclawMedicalUniversityArticleIteratorFactory implements ArticleIteratorFactory,
        ArticleMetadataExtractorFactory {

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

    protected static final String ROOT_TEMPLATE = "\"%s\", base_url";
    protected static final String PATTERN_TEMPLATE =
            "\"%s((en/article)|pdf)/%d/%s/\", base_url, year, volume_number";

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

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


    //It has only html and PDF, no separate pages other aspects, like  abstract/full_tex
    //http://www.dmp.umed.wroc.pl/en/article/2019/56/3/317
    //http://www.dmp.umed.wroc.pl/pdf/2019/56/3/317.pdf
    @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(
                HTML_PATTERN,
                HTML_REPLACEMENT,
                ArticleFiles.ROLE_FULL_TEXT_HTML,
                ArticleFiles.ROLE_ARTICLE_METADATA);

        builder.addAspect(
                PDF_REPLACEMENT,
                ArticleFiles.ROLE_FULL_TEXT_PDF);

        builder.setFullTextFromRoles(
                ArticleFiles.ROLE_ABSTRACT,
                ArticleFiles.ROLE_FULL_TEXT_HTML);

        return builder.getSubTreeArticleIterator();
    }

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

}


@@ -1022,17 +1022,16 @@
   %22%5C%22%25s(
-(
 en/artic
@@ -1032,17 +1032,16 @@
 /article
-)
 %7Cpdf)/%25d
@@ -1159,16 +1159,20 @@
        %22
+.*/(
 en/artic
@@ -1173,16 +1173,17 @@
 /article
+)
 /(%5B%5E/%5D+/
@@ -1306,17 +1306,17 @@
 rticle/$
-1
+2
 %22;%0A%0A    
@@ -1388,19 +1388,24 @@
        %22
+.*/(
 pdf
+)
 /(%5B%5E/%5D+/
@@ -1416,20 +1416,19 @@
 +/%5B%5E/%5D+/
-%5B%5E/%5D
+%5C%5Cd
 +)%5C%5C.pdf
@@ -1525,17 +1525,17 @@
 = %22pdf/$
-1
+2
 .pdf%22;%0A%0A
@@ -2095,24 +2095,164 @@
 ENSITIVE);%0A%0A
+        builder.addAspect(%0A                PDF_PATTERN,%0A                PDF_REPLACEMENT,%0A                ArticleFiles.ROLE_FULL_TEXT_PDF);%0A%0A
         buil
@@ -2454,201 +2454,29 @@
 der.
-addAspect(%0A                PDF_REPLACEMENT,%0A                ArticleFiles.ROLE_FULL_TEXT_PDF);%0A%0A        builder.setFullTextFromRoles(%0A                ArticleFiles.ROLE_ABSTRACT,%0A                
+setFullTextFromRoles(
 Arti
@@ -2499,20 +2499,19 @@
 LL_TEXT_
-HTML
+PDF
 );%0A%0A    

make ArticleIterator using PDF instead of html