package net.ddns.mahaljsp.camera3d;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;
import android.hardware.camera2.TotalCaptureResult;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Size;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.TextureView;

import java.util.Arrays;

/**
 * Created by Thomas Wu(mahaljsp@gmail.com) on 2017/1/6.
 */

public class Camera3D {
    Context context;
    CameraManager manager;
    private Size size;
    private CameraDevice camera;
    private HandlerThread handlerThread;
    private Handler handler;
    CaptureRequest.Builder captureBuilder;
    @SuppressWarnings("MissingPermission")
    public Camera3D(Context context){
        this.context=context;
        handlerThread=new HandlerThread("Camera2");
        handlerThread.start();
        handler=new Handler(handlerThread.getLooper());
        manager=(CameraManager)context.getSystemService(Context.CAMERA_SERVICE);
        try {
            CameraCharacteristics characteristics = manager.getCameraCharacteristics("0");
            StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            size=map.getOutputSizes(SurfaceTexture.class)[0];
            manager.openCamera("0", mDeviceCallback, handler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
    public void startPreview(SurfaceTexture view){
        Surface surface=new Surface(view);
        try {
            captureBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            captureBuilder.addTarget(surface);
            camera.createCaptureSession(Arrays.asList(surface), mSessionCallback, handler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
    private CameraDevice.StateCallback mDeviceCallback=new CameraDevice.StateCallback(){
        @Override
        public void onOpened(CameraDevice cameraDevice) {
            camera=cameraDevice;
        }
        @Override
        public void onDisconnected(CameraDevice cameraDevice) {}
        @Override
        public void onError(CameraDevice cameraDevice, int i) {}
    };
    private CameraCaptureSession.StateCallback mSessionCallback=new CameraCaptureSession.StateCallback(){
        @Override
        public void onConfigured(CameraCaptureSession session) {
            try {
                captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                        CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                captureBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                        CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
                session.setRepeatingRequest(captureBuilder.build(), mCaptureCallBack, handler);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onConfigureFailed(CameraCaptureSession session) {}
    };
    private CameraCaptureSession.CaptureCallback mCaptureCallBack=new CameraCaptureSession.CaptureCallback(){
        @Override
        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
        }
        @Override
        public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult) {
        }
    };
}
