/*
 * Copyright (C) 2013 Steelkiwi Development, Julia Zudikova, Viacheslav Tiagotenkov
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.skd.androidrecording.video;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.media.CamcorderProfile;
import android.os.Build;
import android.view.SurfaceHolder;

import java.io.IOException;

/*
 * Manages camera preview
 */

public class CameraManager {

	private Camera camera;
	private int camerasCount;
	private int defaultCameraID;
	private int cameraRotationDegree;
	private boolean isPreviewStarted = false;
	private CamcorderProfile profile;
	
	public CameraManager() {
		camerasCount = CameraHelper.getAvailableCamerasCount();
		defaultCameraID = CameraHelper.getDefaultCameraID();
	}
	
	public void openCamera() {
		if (camera != null) {
			releaseCamera();
		}
		camera = Camera.open(defaultCameraID);
	}
	
	public void releaseCamera() {
		if (camera != null) {
			camera.release();
			camera = null;
		}
	}
	
	public void switchCamera() {
		stopCameraPreview();
		
		defaultCameraID = (defaultCameraID + 1) % camerasCount;
		openCamera();
	}
	
	public void setupCameraAndStartPreview(SurfaceHolder sf, Size sz, int displayRotation) {
		stopCameraPreview();
		
		cameraRotationDegree = CameraHelper.setCameraDisplayOrientation(defaultCameraID, camera, displayRotation);

		chooseCamcorderProfile(sz);

		Parameters param = camera.getParameters();

		// samsung hack
		// http://stackoverflow.com/questions/7225571/camcorderprofile-quality-high-resolution-produces-green-flickering-video
		//
		// XXX still not work on samsung galaxy 3 with video size = 1280x720,
		// seems no video data is got by MediaRecorder.
		if (sz.height >= 720)
			param.set("cam_mode", 1);

		param.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
		param.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);
        if (Build.VERSION.SDK_INT >= 14)
            param.setRecordingHint(true);
		camera.setParameters(param);
		
		if (setDisplay(sf)) {
			startCameraPreview();
		}	
	}

	private void chooseCamcorderProfile(Size sizeHint) {
		// For android 2.3 devices video quality = low
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
			profile = (CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
		else {
			// For >= Android 3.0 devices select 720p, 480p or low quality of video
			if (CamcorderProfile.hasProfile(getCameraID(), CamcorderProfile.QUALITY_720P)
					&& (sizeHint == null || sizeHint.height >= 720)) {
				profile = (CamcorderProfile.get(CamcorderProfile.QUALITY_720P));
				return;
			}

			if (CamcorderProfile.hasProfile(getCameraID(), CamcorderProfile.QUALITY_480P)
					&& (sizeHint == null || sizeHint.height >= 480)) {
				profile = (CamcorderProfile.get(CamcorderProfile.QUALITY_480P));
				return;
			}

			profile = (CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
		}
	}

	public boolean setDisplay(SurfaceHolder sf) {
		try {
			camera.setPreviewDisplay(sf);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public void startCameraPreview() {
		camera.startPreview();
		isPreviewStarted = true;
	}
	
	public void stopCameraPreview() {
		if (isPreviewStarted && (camera != null)) {
			isPreviewStarted = false;
			camera.stopPreview();
		}
	}

	public Camera getCamera() {
		return camera;
	}

	public CamcorderProfile getProfile() {
		return profile;
	}

	public int getCameraDisplayOrientation() {
		return (CameraHelper.isCameraFacingBack(defaultCameraID)) ? cameraRotationDegree : cameraRotationDegree + 180;
	}
	
	public boolean hasMultipleCameras() {
		return (camerasCount > 1);
	}

	public boolean isPreviewStarted() {
		return isPreviewStarted;
	}

	public int getCameraID() {
		return defaultCameraID;
	}
}

@@ -1997,328 +1997,8 @@
 );%0A%0A
-%09%09// samsung hack%0A%09%09// http://stackoverflow.com/questions/7225571/camcorderprofile-quality-high-resolution-produces-green-flickering-video%0A%09%09//%0A%09%09// XXX still not work on samsung galaxy 3 with video size = 1280x720,%0A%09%09// seems no video data is got by MediaRecorder.%0A%09%09if (sz.height %3E= 720)%0A%09%09%09param.set(%22cam_mode%22, 1);%0A%0A
 %09%09pa

remove the cam_mode=1 hack