/*
 *  Copyright (c) 2017 Otávio Santana and others
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *
 *   You may elect to redistribute this code under either of these licenses.
 *
 *   Contributors:
 *
 *   Otavio Santana
 */
package org.jnosql.diana.couchbase.key;

import com.couchbase.client.java.Bucket;
import org.jnosql.diana.driver.value.JSONValueProvider;
import org.jnosql.diana.driver.value.JSONValueProviderService;

import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
import java.util.Queue;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static java.util.Objects.requireNonNull;

/**
 * The couchbase implementation to {@link Queue}
 * that avoid null items, so if any null object will launch {@link NullPointerException}.
 * This class is a wrapper to {@link com.couchbase.client.java.datastructures.collections.CouchbaseQueue}. Once they only can save primitive type,
 * objects are converted to Json {@link String} using {@link JSONValueProvider#toJson(Object)}
 *
 * @param <T> the object to be stored.
 */
public class CouchbaseQueue<T> implements Queue<T> {

    private static final JSONValueProvider PROVDER = JSONValueProviderService.getProvider();


    private final String bucketName;
    private final Class<T> clazz;
    private final com.couchbase.client.java.datastructures.collections.CouchbaseQueue<String> queue;

    CouchbaseQueue(Bucket bucket, String bucketName, Class<T> clazz) {
        this.bucketName = bucketName + ":queue";
        this.clazz = clazz;
        queue = new com.couchbase.client.java.datastructures.collections.CouchbaseQueue<>(this.bucketName, bucket);
    }

    @Override
    public int size() {
        return queue.size();
    }

    @Override
    public boolean isEmpty() {
        return queue.isEmpty();
    }


    @Override
    public boolean add(T t) {
        return offer(t);
    }

    @Override
    public boolean addAll(Collection<? extends T> collection) {
        requireNonNull(collection, "collection is required");
        collection.forEach(this::add);
        return true;
    }

    @Override
    public void clear() {
        queue.clear();
    }

    @Override
    public boolean offer(T t) {
        requireNonNull(t, "object is required");
        return queue.offer(PROVDER.toJson(t));
    }

    @Override
    public T remove() {
        return poll();
    }

    @Override
    public T poll() {
        String json = queue.poll();
        return getT(json);
    }


    @Override
    public T peek() {
        String json = queue.peek();
        return getT(json);
    }


    @Override
    public T element() {
        String json = queue.element();
        return getT(json);
    }

    private T getT(String json) {
        if (Objects.nonNull(json)) {
            return PROVDER.of(json).get(clazz);
        }
        return null;
    }

    @Override
    public boolean removeAll(Collection<?> collection) {
        requireNonNull(collection, "collection is required");
        return queue.removeAll(collection.stream().map(PROVDER::toJson).collect(Collectors.toList()));
    }

    @Override
    public boolean remove(Object o) {
        Objects.requireNonNull(o, "object is required");
        return queue.remove(PROVDER.toJson(o));
    }

    @Override
    public boolean contains(Object o) {
        Objects.requireNonNull(o, "object is required");
        return queue.contains(PROVDER.toJson(o));
    }

    @Override
    public Iterator<T> iterator() {
        return StreamSupport.stream(queue.spliterator(), false)
                .map(s -> PROVDER.of(s).get(clazz))
                .collect(Collectors.toList()).iterator();
    }

    @Override
    public Object[] toArray() {
        return StreamSupport.stream(queue.spliterator(), false)
                .map(s -> PROVDER.of(s).get(clazz))
                .toArray(size -> new Object[size]);
    }

    @Override
    public <T1> T1[] toArray(T1[] t1s) {
        requireNonNull(t1s, "arrys is required");
        return StreamSupport.stream(queue.spliterator(), false)
                .map(s -> PROVDER.of(s).get(clazz))
                .toArray(size -> t1s);
    }

    @Override
    public boolean retainAll(Collection<?> collection) {
        requireNonNull(collection, "collection is required");
        return queue.retainAll(collection.stream().map(PROVDER::toJson).collect(Collectors.toList()));
    }

    @Override
    public boolean containsAll(Collection<?> collection) {
        requireNonNull(collection, "collection is required");
        return queue.containsAll(collection.stream().map(PROVDER::toJson).collect(Collectors.toList()));
    }

}

@@ -660,127 +660,75 @@
 et;%0A
+%0A
 import 
-org.jnosql.diana.driver.value.JSONValueProvider;%0Aimport org.jnosql.diana.driver.value.JSONValueProviderService;%0A
+javax.json.bind.Jsonb;%0Aimport javax.json.bind.JsonbBuilder;
 %0Aimp
@@ -830,16 +830,52 @@
 .Queue;%0A
+import java.util.function.Function;%0A
 import j
@@ -1343,32 +1343,20 @@
 %7B@link J
-SONValueProvider
+sonb
 #toJson(
@@ -1494,71 +1494,40 @@
 al J
-SONValueProvider PROVDER = JSONValueProviderService.getProvider
+sonb JSONB = JsonbBuilder.create
 ();%0A
@@ -2606,23 +2606,21 @@
 e.offer(
-PROVDER
+JSONB
 .toJson(
@@ -3119,29 +3119,29 @@
 urn 
-PROVDER.of(json).get(
+JSONB.fromJson(json, 
 claz
@@ -3362,39 +3362,37 @@
 on.stream().map(
-PROVDER
+JSONB
 ::toJson).collec
@@ -3560,23 +3560,21 @@
 .remove(
-PROVDER
+JSONB
 .toJson(
@@ -3726,23 +3726,21 @@
 ontains(
-PROVDER
+JSONB
 .toJson(
@@ -3890,36 +3890,17 @@
 map(
-s -%3E PROVDER.of(s).get(clazz
+fromJSON(
 ))%0A 
@@ -4098,36 +4098,17 @@
 map(
-s -%3E PROVDER.of(s).get(clazz
+fromJSON(
 ))%0A 
@@ -4359,36 +4359,17 @@
 map(
-s -%3E PROVDER.of(s).get(clazz
+fromJSON(
 ))%0A 
@@ -4593,39 +4593,37 @@
 on.stream().map(
-PROVDER
+JSONB
 ::toJson).collec
@@ -4850,15 +4850,13 @@
 map(
-PROVDER
+JSONB
 ::to
@@ -4899,10 +4899,109 @@
 %0A    %7D%0A%0A
+    private Function%3CString, T%3E fromJSON() %7B%0A        return s -%3E JSONB.fromJson(s, clazz);%0A    %7D%0A%0A%0A
 %7D%0A

fix Queue on couchbase