package org.embulk.parser.joni;

import com.google.common.base.Optional;
import org.embulk.config.Config;
import org.embulk.config.ConfigDefault;
import org.embulk.config.ConfigDiff;
import org.embulk.config.ConfigSource;
import org.embulk.config.Task;
import org.embulk.config.TaskSource;
import org.embulk.spi.ParserPlugin;
import org.embulk.spi.FileInput;
import org.embulk.spi.PageOutput;
import org.embulk.spi.Schema;
import org.embulk.spi.SchemaConfig;
import org.embulk.spi.PageBuilder;

import org.embulk.spi.time.TimestampParser;
import org.embulk.spi.Exec;
import org.embulk.spi.util.Timestamps;

import org.embulk.spi.util.LineDecoder;
import org.joni.*;
import org.slf4j.Logger;

import org.jcodings.specific.UTF8Encoding;

import java.util.Iterator;


public class JoniParserPlugin
        implements ParserPlugin
{
    private static final Logger logger = Exec.getLogger(JoniParserPlugin.class);

    public interface PluginTask
            extends Task,LineDecoder.DecoderTask, TimestampParser.Task
    {
        @Config("columns")
        SchemaConfig getColumns();

        @Config("stop_on_invalid_record")
        @ConfigDefault("false")
        boolean getStopOnInvalidRecord();

        @Config("format")
        String getFormat();

    }

    @Override
    public void transaction(ConfigSource config, ParserPlugin.Control control)
    {
        PluginTask task = config.loadConfig(PluginTask.class);

        Schema schema = task.getColumns().toSchema();

        control.run(task.dump(), schema);
    }

    @Override
    public void run(TaskSource taskSource, Schema schema,
            FileInput input, PageOutput output)
    {
        PluginTask task = taskSource.loadTask(PluginTask.class);
        LineDecoder lineDecoder = new LineDecoder(input, task);
        PageBuilder pageBuilder = new PageBuilder(Exec.getBufferAllocator(), schema, output);
        TimestampParser[] timestampParsers = Timestamps.newTimestampColumnParsers(task, task.getColumns());

        String format = task.getFormat();
        byte[] pattern = format.getBytes();

        Regex regex = new Regex(pattern,0,pattern.length,Option.NONE,UTF8Encoding.INSTANCE);

        while (input.nextFile()) {
            while (true) {
                String line = lineDecoder.poll();

                if (line == null) {
                    break;
                }
                Matcher matcher = regex.matcher(pattern);
                int result = matcher.search(0,line.getBytes().length,Option.DEFAULT);
                if( result != -1 ){
                     Region region = matcher.getEagerRegion();
                     for(Iterator<NameEntry> entry = regex.namedBackrefIterator(); entry.hasNext(); ){
                         NameEntry e = entry.next();
                     }
                }
            }
        }

    }
}

@@ -745,21 +745,88 @@
 ava.
-util.Iterator
+nio.charset.StandardCharsets;%0Aimport java.util.Iterator;%0Aimport java.util.Locale
 ;%0A%0A%0A
@@ -2085,24 +2085,97 @@
 etFormat();%0A
+        logger.info(String.format(Locale.ENGLISH,%22format = %25s%22,format));%0A
         byte
@@ -2409,25 +2409,104 @@
 der.poll();%0A
+//                logger.debug(String.format(Locale.ENGLISH,%22line = %25s%22,line));
 %0A
-
             
@@ -2622,23 +2622,31 @@
 matcher(
-pattern
+line.getBytes()
 );%0A     
@@ -2726,16 +2726,17 @@
 FAULT);%0A
+%0A
         
@@ -2995,33 +2995,633 @@
                 
-%7D
+    int number = e.getBackRefs()%5B0%5D;%0A                         int begin = region.beg%5Bnumber%5D;%0A                         int end = region.end%5Bnumber%5D;%0A%0A                         String str = new String(line.getBytes(StandardCharsets.UTF_8), begin, end - begin, StandardCharsets.UTF_8);%0A                         String name = new String(e.name, e.nameP, e.nameEnd - e.nameP);%0A                         logger.debug(String.format(Locale.ENGLISH,%22%3C%25s%3E = %25s%22,name,str));%0A                     %7D%0A                %7D else %7B%0A                    logger.warn(String.format(Locale.ENGLISH,%22unmatched line = %25s%22,line));
 %0A               

Implement regex named captures