package com.ragnarok.rxcamera.example;

import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;

import com.ragnarok.rxcamera.RxCamera;
import com.ragnarok.rxcamera.RxCameraData;
import com.ragnarok.rxcamera.config.RxCameraConfig;
import com.ragnarok.rxcamera.config.RxCameraConfigChooser;

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

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "Example.MainActivity";

    private TextureView textureView;
    private Button openCameraBtn;
    private Button closeCameraBtn;

    private RxCamera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        textureView = (TextureView) findViewById(R.id.preview_surface);
        openCameraBtn = (Button) findViewById(R.id.open_camera);
        closeCameraBtn = (Button) findViewById(R.id.close_camera);

        openCameraBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openCamera();
            }
        });

        closeCameraBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (camera != null) {
                    camera.closeCameraWithResult().subscribe(new Action1<Boolean>() {
                        @Override
                        public void call(Boolean aBoolean) {
                            Log.d(TAG, "close camera finished, success: " + aBoolean);
                        }
                    });
                }
            }
        });
    }

    private void openCamera() {
        RxCameraConfig config = RxCameraConfigChooser.obtain().
                useBackCamera().
                setAutoFocus(true).
                setPreferPreviewFrameRate(15, 30).
                setPreferPreviewSize(new Point(640, 480)).
                setHandleSurfaceEvent(true).
                get();
        Log.d(TAG, "config: " + config);
        RxCamera.open(this, config).flatMap(new Func1<RxCamera, Observable<RxCamera>>() {
            @Override
            public Observable<RxCamera> call(RxCamera rxCamera) {
                Log.d(TAG, "isopen: " + rxCamera.isOpenCamera());
                camera = rxCamera;
                return rxCamera.bindTexture(textureView);
            }
        }).subscribeOn(Schedulers.io()).flatMap(new Func1<RxCamera, Observable<RxCamera>>() {
            @Override
            public Observable<RxCamera> call(RxCamera rxCamera) {
                Log.d(TAG, "isbindsurface: " + rxCamera.isBindSurface());
                return rxCamera.startPreview();
            }
        }).flatMap(new Func1<RxCamera, Observable<RxCameraData>>() {
            @Override
            public Observable<RxCameraData> call(RxCamera rxCamera) {
                return rxCamera.request().periodicDataRequest(1000);
            }
        }).subscribe(new Subscriber<RxCameraData>() {
            @Override
            public void onCompleted() {
                Log.d(TAG, "onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.e(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onNext(RxCameraData rxCameraData) {
                Log.d(TAG, "onNext, data.length: " + rxCameraData.cameraData.length);
            }
        });


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (camera != null) {
            camera.closeCamera();
        }
    }
}

@@ -753,24 +753,72 @@
 bscription;%0A
+import rx.android.schedulers.AndroidSchedulers;%0A
 import rx.fu
@@ -2979,16 +2979,56 @@
 Camera()
+ + %22, thread: %22 + Thread.currentThread()
 );%0A     
@@ -3144,37 +3144,8 @@
  %7D).
-subscribeOn(Schedulers.io()).
 flat
@@ -3353,16 +3353,56 @@
 urface()
+ + %22, thread: %22 + Thread.currentThread()
 );%0A     
@@ -3639,86 +3639,277 @@
     
-return rxCamera.request().periodicDataRequest(1000);%0A            %7D%0A        %7D).
+Log.d(TAG, %22after start preview, thread: %22 + Thread.currentThread());%0A                return rxCamera.request().periodicDataRequest(1000);%0A            %7D%0A        %7D).subscribeOn(Schedulers.io()).%0A                observeOn(AndroidSchedulers.mainThread()).%0A                
 subs
@@ -4374,16 +4374,56 @@
 a.length
+ + %22, thread: %22 + Thread.currentThread()
 );%0A     

update example