package com.ragnarok.rxcamera.request;

import com.ragnarok.rxcamera.OnRxCameraPreviewFrameCallback;
import com.ragnarok.rxcamera.RxCamera;
import com.ragnarok.rxcamera.RxCameraData;
import com.ragnarok.rxcamera.error.CameraDataNullException;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action0;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by ragnarok on 15/11/15.
 */
public class PeriodicDataRequest extends BaseRxCameraRequest implements OnRxCameraPreviewFrameCallback {

    private long intervalMills;

    private boolean isInstallCallback = false;

    private Subscriber<? super RxCameraData> subscriber = null;

    private RxCameraData currentData = new RxCameraData();

    public PeriodicDataRequest(RxCamera rxCamera, long intervalMills) {
        super(rxCamera);
        this.intervalMills = intervalMills;
    }

    @Override
    public Observable<RxCameraData> get() {
        return Observable.create(new Observable.OnSubscribe<RxCameraData>() {
            @Override
            public void call(final Subscriber<? super RxCameraData> subscriber) {
                PeriodicDataRequest.this.subscriber = subscriber;
                subscriber.add(Schedulers.newThread().createWorker().schedulePeriodically(new Action0() {
                    @Override
                    public void call() {
                        if (currentData.cameraData != null && !subscriber.isUnsubscribed() && rxCamera.isOpenCamera()) {
                            subscriber.onNext(currentData);
                        }
                    }
                }, 0, intervalMills, TimeUnit.MILLISECONDS));

            }
        }).doOnUnsubscribe(new Action0() {
            @Override
            public void call() {
                rxCamera.uninstallPreviewCallback(PeriodicDataRequest.this);
                isInstallCallback = false;
            }
        }).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                if (!isInstallCallback) {
                    rxCamera.installPreviewCallback(PeriodicDataRequest.this);
                    isInstallCallback = true;
                }
            }
        });
    }

    @Override
    public void onPreviewFrame(byte[] data) {
        if (subscriber != null && !subscriber.isUnsubscribed() && rxCamera.isOpenCamera()) {
            if (data == null || data.length == 0) {
                subscriber.onError(new CameraDataNullException());
            }
            currentData.cameraData = data;
            currentData.rotateMatrix = rxCamera.getRotateMatrix();
        }
    }
}

@@ -29,24 +29,50 @@
 a.request;%0A%0A
+import android.util.Log;%0A%0A
 import com.r
@@ -120,16 +120,16 @@
 llback;%0A
-
 import c
@@ -370,16 +370,64 @@
 iption;%0A
+import rx.android.schedulers.AndroidSchedulers;%0A
 import r
@@ -658,16 +658,87 @@
 back %7B%0A%0A
+    private static final String TAG = %22MicroMsg.PeriodicDataRequest%22;%0A%0A
     priv
@@ -875,16 +875,16 @@
  null;%0A%0A
-
     priv
@@ -931,24 +931,69 @@
 eraData();%0A%0A
+    private long lastSendDataTimestamp = 0;%0A%0A
     public P
@@ -2454,16 +2454,288 @@
       %7D)
+.doOnTerminate(new Action0() %7B%0A            @Override%0A            public void call() %7B%0A                rxCamera.uninstallPreviewCallback(PeriodicDataRequest.this);%0A                isInstallCallback = false;%0A            %7D%0A        %7D).observeOn(AndroidSchedulers.mainThread())
 ;%0A    %7D%0A

make PeriodicDataRequest return data on main thread