package io.fotoapparat.hardware.v2;

import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.TextureView;

import io.fotoapparat.hardware.CameraDevice;
import io.fotoapparat.hardware.CameraException;
import io.fotoapparat.hardware.Capabilities;
import io.fotoapparat.hardware.Parameters;
import io.fotoapparat.hardware.v2.capabilities.CameraCapabilities;
import io.fotoapparat.hardware.v2.captor.PhotoCaptor;
import io.fotoapparat.hardware.v2.connection.CameraConnection;
import io.fotoapparat.hardware.v2.session.PreviewOperator;
import io.fotoapparat.parameter.LensPosition;
import io.fotoapparat.photo.Photo;

/**
 * Camera hardware driver for v2 {@link Camera2} API.
 */
@SuppressWarnings("MissingPermission")
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class Camera2 implements CameraDevice, PreviewOperator, PhotoCaptor {

	private final io.fotoapparat.hardware.v2.CameraManager cameraManager;
	private final CameraSelector cameraSelector;

	public Camera2(Context context) {
		CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);

		cameraSelector = new CameraSelector(manager);

		CameraConnection cameraConnection = new CameraConnection(manager);
		CameraCapabilities cameraCapabilities = new CameraCapabilities(manager);

		cameraManager = new io.fotoapparat.hardware.v2.CameraManager(
				cameraConnection,
				cameraCapabilities
		);
	}

	@Override
	public void open(LensPosition lensPosition) {
		try {
			String cameraId = cameraSelector.findCameraId(lensPosition);
			cameraManager.open(cameraId);
		} catch (CameraAccessException e) {
			throw new CameraException(e);
		}
	}

	@Override
	public void close() {
		cameraManager.close();
	}

	@Override
	public void startPreview() {
		cameraManager.startPreview();
	}

	@Override
	public void stopPreview() {
		cameraManager.stopPreview();
	}

	@Override
	public void setDisplaySurface(TextureView textureView) {
		cameraManager.setSurface(textureView);
	}

	@Override
	public void setDisplayOrientation(int degrees) {
		cameraManager.onOrientationChanged(degrees);
	}

	@Override
	public void updateParameters(Parameters parameters) {
		// TODO actually do something
	}

	@Override
	public Capabilities getCapabilities() {
		cameraManager.getCapabilities();
		// TODO: return the capabilties of the camera device
		return null;
	}

	@Override
	public Photo takePicture() {
		return cameraManager.takePicture();
	}
}

@@ -2065,21 +2065,66 @@
 ace(
-TextureView t
+Object displaySurface) %7B%0A%09%09if (displaySurface instanceof T
 extu
@@ -2129,24 +2129,25 @@
 tureView) %7B%0A
+%09
 %09%09cameraMana
@@ -2161,17 +2161,19 @@
 Surface(
-t
+((T
 extureVi
@@ -2175,17 +2175,138 @@
 ureView)
-;
+ displaySurface));%0A%09%09%7D else %7B%0A%09%09%09throw new IllegalArgumentException(%22Unsupported display surface: %22 + displaySurface);%0A%09%09%7D
 %0A%09%7D%0A%0A%09@O

revert setDisplaySurface to object