package com.github.niqdev.mjpeg;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * A library wrapper for handle mjpeg streams.
 *
 * @see
 * <ul>
 *     <li><a href="https://bitbucket.org/neuralassembly/simplemjpegview">simplemjpegview</a></li>
 *     <li><a href="https://code.google.com/archive/p/android-camera-axis">android-camera-axis</a></li>
 * </ul>
 */
public class Mjpeg {

    /**
     * Library implementation type
     */
    public enum Type {
        DEFAULT, NATIVE
    }

    private final Type type;

    private Mjpeg(Type type) {
        if (type == null) {
            throw new IllegalArgumentException("null type not allowed");
        }
        this.type = type;
    }

    /**
     * Uses {@link Type#DEFAULT} implementation.
     *
     * @return Mjpeg instance
     */
    public static Mjpeg newInstance() {
        return new Mjpeg(Type.DEFAULT);
    }

    /**
     * Choose among {@link com.github.niqdev.mjpeg.Mjpeg.Type} implementations.
     *
     * @return Mjpeg instance
     */
    public static Mjpeg newInstance(Type type) {
        return new Mjpeg(type);
    }

    /**
     * Configure authentication.
     *
     * @param username
     * @param password
     *
     * @return Mjpeg instance
     */
    public Mjpeg credential(String username, String password) {
        // TODO
        return this;
    }

    public Observable<MjpegInputStream> open(String url) {
        return Observable.defer(() -> {
            try {
                HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
                InputStream inputStream = urlConnection.getInputStream();
                switch (type) {
                    // handle multiple implementations
                    case DEFAULT:
                        return Observable.just(new MjpegInputStreamDefault(inputStream));
                    case NATIVE:
                        return Observable.just(new MjpegInputStreamNative(inputStream));
                }
                throw new IllegalStateException("invalid type");
            } catch (IOException e) {
                return Observable.error(e);
            }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
    }

}

@@ -99,29 +99,100 @@
 ava.net.
-HttpURLConnec
+Authenticator;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.PasswordAuthentica
 tion;%0Aim
@@ -1437,23 +1437,16 @@
 assword%0A
-     *%0A
      * @
@@ -1551,43 +1551,482 @@
     
-// TODO%0A        return this;%0A    %7D%0A
+if (username != null && password != null) %7B%0A            Authenticator.setDefault(new Authenticator() %7B%0A                protected PasswordAuthentication getPasswordAuthentication() %7B%0A                    return new PasswordAuthentication(username, password.toCharArray());%0A                %7D%0A            %7D);%0A        %7D%0A        return this;%0A    %7D%0A%0A    /**%0A     * Connect to a Mjpeg stream.%0A     *%0A     * @param url%0A     * @return Observable%3CMjpegInputStream%3E Mjpeg stream%0A     */
 %0A   

add mjpeg credential