/*
 * MIT License
 *
 * Copyright (c) 2018 Ilia Rogozhin (@smallcreep) <ilia.rogozhin@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.github.smallcreep.cucumber.seeds.table;

import com.github.smallcreep.cucumber.seeds.SqlValue;
import com.github.smallcreep.cucumber.seeds.Table;
import com.github.smallcreep.cucumber.seeds.sqlvalue.Bytea;
import com.github.smallcreep.cucumber.seeds.sqlvalue.Serial;
import com.github.smallcreep.cucumber.seeds.sqlvalue.Text;
import com.github.smallcreep.cucumber.seeds.sqlvalue.Varchar;
import com.jcabi.xml.XML;
import java.util.Collection;
import java.util.Map;
import org.cactoos.iterable.Mapped;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;

/**
 * Envelope Table, add to values correct wrap use XML document for type.
 * @since 0.2.0
 */
public final class TableXml implements Table {

    /**
     * Origin origin.
     */
    private final Table origin;

    /**
     * Xml table schema.
     */
    private final XML schema;

    /**
     * SqlValues casting map.
     */
    private final Map<String, SqlValue> values;

    /**
     * Ctor.
     * @param table Origin origin
     * @param schema Xml table schema
     */
    public TableXml(final Table table, final XML schema) {
        this(
            table,
            schema,
            new MapOf<String, SqlValue>(
                new MapEntry<>(
                    "Varchar",
                    new Varchar()
                ),
                new MapEntry<>(
                    "Serial",
                    new Serial()
                ),
                new MapEntry<>(
                    "Text",
                    new Text()
                ),
                new MapEntry<>(
                    "Bytea",
                    new Bytea()
                )
            )
        );
    }

    /**
     * Ctor.
     * @param table Origin origin
     * @param schema Xml table schema
     * @param values SqlValues casting map.
     */
    private TableXml(
        final Table table,
        final XML schema,
        final Map<String, SqlValue> values
    ) {
        this.origin = table;
        this.schema = schema;
        this.values = values;
    }

    // @todo #133:15m/DEV add correct exception if not found in this.value
    //  value, because now threw java.lang.NullPointerException with next
    //  stacktrace:
    //  at com.github.smallcreep.cucumber.seeds.table.TableXml
    //  .lambda$null$0(TableXml.java:107)
    //  at org.cactoos.map.MapOf.lambda$new$0(MapOf.java:83)
    //  at org.cactoos.func.UncheckedFunc.lambda$apply$0(UncheckedFunc.java:56)
    //  at org.cactoos.scalar.CheckedScalar.value(CheckedScalar.java:76)
    //  at org.cactoos.scalar.IoCheckedScalar.value(IoCheckedScalar.java:63)
    @Override
    public Collection<Long> insert(
        final Iterable<Map<String, String>> rows
    ) throws Exception {
        return this.origin.insert(
            new Mapped<Map<String, String>, Map<String, String>>(
                input -> new MapOf<String, String>(
                    Map.Entry::getKey,
                    entry -> this.values.get(
                        this.schema.xpath(
                            String.format(
                                "//column[text()='%s']/@type",
                                entry.getKey()
                            )
                        ).get(0)
                    ).apply(entry.getValue()),
                    input.entrySet()
                ),
                rows
            )
        );
    }

    @Override
    public String value() throws Exception {
        return this.origin.value();
    }
}

@@ -1596,16 +1596,44 @@
 ml.XML;%0A
+import java.io.IOException;%0A
 import j
@@ -1773,16 +1773,55 @@
 p.MapOf;
+%0Aimport org.cactoos.text.FormattedText;
 %0A%0A/**%0A *
@@ -3305,573 +3305,8 @@
  %7D%0A%0A
-    // @todo #133:15m/DEV add correct exception if not found in this.value%0A    //  value, because now threw java.lang.NullPointerException with next%0A    //  stacktrace:%0A    //  at com.github.smallcreep.cucumber.seeds.table.TableXml%0A    //  .lambda$null$0(TableXml.java:107)%0A    //  at org.cactoos.map.MapOf.lambda$new$0(MapOf.java:83)%0A    //  at org.cactoos.func.UncheckedFunc.lambda$apply$0(UncheckedFunc.java:56)%0A    //  at org.cactoos.scalar.CheckedScalar.value(CheckedScalar.java:76)%0A    //  at org.cactoos.scalar.IoCheckedScalar.value(IoCheckedScalar.java:63)%0A
     
@@ -3307,32 +3307,32 @@
 %0A%0A    @Override%0A
+
     public Colle
@@ -3650,24 +3650,9 @@
  -%3E 
-this.values.get(
+%7B
 %0A   
@@ -3667,24 +3667,43 @@
             
+ final String key =
  this.schema
@@ -3925,16 +3925,17 @@
 ).get(0)
+;
 %0A       
@@ -3951,33 +3951,491 @@
     
-).apply(entry.getValue())
+    if (this.values.containsKey(key)) %7B%0A                            return this.values.get(%0A                                key%0A                            ).apply(entry.getValue());%0A                        %7D%0A                        throw new IOException(%0A                            new FormattedText(%0A                                %22Not found type '%25s'%22,%0A                                key%0A                            ).asString()%0A                        );%0A                    %7D
 ,%0A  

add correct exception if not found...