package org.lockss.plugin.clockss.euilaw;

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

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

public class EuiLawSourceZipXmlArticleIteratorFactory extends SourceZipXmlArticleIteratorFactory {

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

  protected static final String ALL_PATTERN_TEMPLATE = "\"^%s[^/]+/.*\\.zip!/.*\\.xml$\",base_url";

  public static final Pattern XML_PATTERN = Pattern.compile("/(.*)XML_(.*)\\.zip!/(.*)\\.xml$", Pattern.CASE_INSENSITIVE);
  //public static final Pattern PDF_PATTERN = Pattern.compile("/(.*)(Volume_.*)/(.*)\\.pdf$", Pattern.CASE_INSENSITIVE);
  public static final String XML_REPLACEMENT = "/$1XML_$2.zip!/$3.xml";
  private static final String PDF_REPLACEMENT = "/$1$2/$3.pdf";

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

    // no need to limit to ROOT_TEMPLATE
    builder.setSpec(builder.newSpec()
        .setTarget(target)
        .setPatternTemplate(getIncludePatternTemplate(), Pattern.CASE_INSENSITIVE)
        .setExcludeSubTreePattern(getExcludeSubTreePattern())
        .setVisitArchiveMembers(true)
        .setVisitArchiveMembers(getIsArchive()));

    builder.addAspect(XML_PATTERN,
        XML_REPLACEMENT,
        ArticleFiles.ROLE_ARTICLE_METADATA);

    builder.addAspect(
        PDF_REPLACEMENT,
        ArticleFiles.ROLE_FULL_TEXT_PDF);

    builder.setFullTextFromRoles(
        ArticleFiles.ROLE_FULL_TEXT_PDF,
        ArticleFiles.ROLE_ARTICLE_METADATA);

    return builder.getSubTreeArticleIterator();
  }

  protected String getIncludePatternTemplate() {
    return ALL_PATTERN_TEMPLATE;
  }

  protected boolean getIsArchive() {
    return true;
  }

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

@@ -810,19 +810,25 @@
 p!/.*%5C%5C.
+(
 xml
+%7Cpdf)
 $%5C%22,base
@@ -837,16 +837,207 @@
 rl%22;%0A%0A  
+// Old way. they may go back to it.%0A  // %3Cbase_url%3E/%3Cdir%3E/lawandworld_XML_Volume_8_Issue_1_2022.zip!/First_Last.xml%0A  // %3Cbase_url%3E/%3Cdir%3E/lawandworld_Volume_8_Issue_1_2022/First_Last.pdf%0A  //
 public s
@@ -1093,17 +1093,23 @@
 (%22/(
-.
+%5B%5E/%5D
 *)XML_(
-.
+%5B%5E/%5D
 *)%5C%5C
@@ -1115,17 +1115,20 @@
 %5C.zip!/(
-.
+%5B%5E/%5D
 *)%5C%5C.xml
@@ -1158,24 +1158,26 @@
 ITIVE);%0A  //
+  
 public stati
@@ -1188,19 +1188,425 @@
 nal 
-Pattern PDF
+String XML_REPLACEMENT = %22/$1XML_$2.zip!/$3.xml%22;%0A  //  private static final String PDF_REPLACEMENT = %22/$1$2/$3.pdf%22;%0A%0A  // New way. still need work from them.%0A  // %3Cbase_url%3E/%3Cdir%3E/Volume_1_Issue_2_2015.zip!/Volume_1_Issue_2_2015/Volume_1_Issue_2_2015_PDF/First_Last.pdf%0A  // %3Cbase_url%3E/%3Cdir%3E/Volume_1_Issue_2_2015.zip!/Volume_1_Issue_2_2015/Volume_1_Issue_2_2015_XML/First_Last.xml%0A  public static final Pattern XML
 _PAT
@@ -1635,33 +1635,44 @@
 (%22/(
-.*)(Volume_.*)/(.
+%5B%5E/%5D*%5C%5C.zip!)/(.*)_XML/(%5B%5E/%5D
 *)%5C%5C.
-pdf
+xml
 $%22, 
@@ -1697,29 +1697,30 @@
 SITIVE);%0A  p
-ublic
+rivate
  static fina
@@ -1746,35 +1746,31 @@
 EMENT = %22/$1
-XML_$2.zip!
+/$2_XML
 /$3.xml%22;%0A  
@@ -1811,34 +1811,39 @@
 PLACEMENT = %22/$1
+/
 $2
+_PDF
 /$3.pdf%22;%0A%0A  @Ov
@@ -1836,17 +1836,16 @@
 3.pdf%22;%0A
-%0A
   @Overr

update article aspect patterns