// Path: reactive-lab-gateway/src/main/java/io/reactivex/lab/gateway/clients/BookmarksCommand.java
// public static class Bookmark {
// 
//     private final Map<String, Object> data;
// 
//     Bookmark(Map<String, Object> data) {
//         this.data = data;
//     }
// 
//     public static Bookmark fromJson(String json) {
//         return new Bookmark(SimpleJson.jsonToMap(json));
//     }
// 
//     public int getPosition() {
//         return (int) data.get("position");
//     }
// 
//     public int getVideoId() {
//         return Integer.parseInt(String.valueOf(data.get("videoId")));
//     }
// 
// }
// 
// Path: reactive-lab-gateway/src/main/java/io/reactivex/lab/gateway/clients/PersonalizedCatalogCommand.java
// public static class Video implements ID {
// 
//     private final int id;
// 
//     public Video(int id) {
//         this.id = id;
//     }
// 
//     public int getId() {
//         return id;
//     }
// 
// }


import io.netty.buffer.ByteBuf;
import io.reactivex.lab.gateway.clients.BookmarksCommand.Bookmark;
import io.reactivex.lab.gateway.clients.PersonalizedCatalogCommand.Video;
import io.reactivex.lab.gateway.loadbalancer.DiscoveryAndLoadBalancer;
import io.reactivex.netty.protocol.http.sse.ServerSentEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import netflix.ocelli.LoadBalancer;
import netflix.ocelli.rxnetty.HttpClientHolder;
import rx.functions.Func1;
import com.netflix.hystrix.HystrixCollapser.CollapsedRequest;
import com.netflix.hystrix.HystrixObservableCollapser;
import com.netflix.hystrix.HystrixObservableCommand;
package io.reactivex.lab.gateway.clients;





public class BookmarkCommand extends HystrixObservableCollapser<Integer, Bookmark, Bookmark, Video> {

    private final Video video;
    private static final LoadBalancer<HttpClientHolder<ByteBuf, ServerSentEvent>> loadBalancer =
            DiscoveryAndLoadBalancer.getFactory().forVip("reactive-lab-bookmark-service");

    public BookmarkCommand(Video video) {
        this.video = video;
    }

    @Override
    public Video getRequestArgument() {
        return video;
    }

    @Override
    protected HystrixObservableCommand<Bookmark> createCommand(Collection<CollapsedRequest<Bookmark, Video>> requests) {
        List<Video> videos = new ArrayList<>();
        for (CollapsedRequest<Bookmark, Video> r : requests) {
            videos.add(r.getArgument());
        }
// Path: reactive-lab-gateway/src/main/java/io/reactivex/lab/gateway/clients/BookmarksCommand.java
// public static class Bookmark {
// 
//     private final Map<String, Object> data;
// 
//     Bookmark(Map<String, Object> data) {
//         this.data = data;
//     }
// 
//     public static Bookmark fromJson(String json) {
//         return new Bookmark(SimpleJson.jsonToMap(json));
//     }
// 
//     public int getPosition() {
//         return (int) data.get("position");
//     }
// 
//     public int getVideoId() {
//         return Integer.parseInt(String.valueOf(data.get("videoId")));
//     }
// 
// }
// 
// Path: reactive-lab-gateway/src/main/java/io/reactivex/lab/gateway/clients/PersonalizedCatalogCommand.java
// public static class Video implements ID {
// 
//     private final int id;
// 
//     public Video(int id) {
//         this.id = id;
//     }
// 
//     public int getId() {
//         return id;
//     }
// 
// }


// Path: reactive-lab-gateway/src/main/java/io/reactivex/lab/gateway/clients/BookmarkCommand.java
import io.netty.buffer.ByteBuf;
import io.reactivex.lab.gateway.clients.BookmarksCommand.Bookmark;
import io.reactivex.lab.gateway.clients.PersonalizedCatalogCommand.Video;
import io.reactivex.lab.gateway.loadbalancer.DiscoveryAndLoadBalancer;
import io.reactivex.netty.protocol.http.sse.ServerSentEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import netflix.ocelli.LoadBalancer;
import netflix.ocelli.rxnetty.HttpClientHolder;
import rx.functions.Func1;
import com.netflix.hystrix.HystrixCollapser.CollapsedRequest;
import com.netflix.hystrix.HystrixObservableCollapser;
import com.netflix.hystrix.HystrixObservableCommand;

package io.reactivex.lab.gateway.clients;





public class BookmarkCommand extends HystrixObservableCollapser<Integer, Bookmark, Bookmark, Video> {

    private final Video video;
    private static final LoadBalancer<HttpClientHolder<ByteBuf, ServerSentEvent>> loadBalancer =
            DiscoveryAndLoadBalancer.getFactory().forVip("reactive-lab-bookmark-service");

    public BookmarkCommand(Video video) {
        this.video = video;
    }

    @Override
    public Video getRequestArgument() {
        return video;
    }

    @Override
    protected HystrixObservableCommand<Bookmark> createCommand(Collection<CollapsedRequest<Bookmark, Video>> requests) {
        List<Video> videos = new ArrayList<>();
        for (CollapsedRequest<Bookmark, Video> r : requests) {
            videos.add(r.getArgument());
        }
        return new BookmarksCommand(videos, loadBalancer);