package com.xypower.common;

import android.content.Context;
import android.graphics.ImageFormat;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.text.TextUtils;
import android.util.Log;
import android.util.Size;

public class CameraUtils {

    public static String getAllCameraInfo(Context context) {
        StringBuilder builder = new StringBuilder();
        try {
            CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
            String[] cameraIds = cameraManager.getCameraIdList();

            builder.append("Count:");
            builder.append(Integer.toString(cameraIds.length));
            builder.append("\n");
            int idx = 1;
            for (String cameraId : cameraIds) {

                builder.append(idx++);
                builder.append(": Id=");
                builder.append(cameraId);

                CameraCharacteristics cameraCharacteristics = null;

                try {
                    cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                if (cameraCharacteristics != null) {

                    builder.append(" facing=");
                    Integer facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
                    builder.append(facing == null ? "" : facing.toString());
                    builder.append(" orientation=");
                    Integer orientation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
                    builder.append(orientation == null ? "" : orientation.toString());

                    StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
                    Size[] sizes = map.getOutputSizes(ImageFormat.YUV_420_888);

                    for (int i = 0; i < sizes.length; i++) {
                        Size itemSize = sizes[i];
                        builder.append("(" + itemSize.getWidth() + "," + itemSize.getHeight() + ")");
                        break;
                    }

                    builder.append("\n");
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return builder.toString();
    }

    public static String getDetailedCameraInfo(Context context, String cameraId) {
        StringBuilder builder = new StringBuilder();
        try {
            CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);

            CameraCharacteristics cameraCharacteristics = null;

            try {
                cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            if (cameraCharacteristics != null) {

                int[] capabilities = cameraCharacteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);
                if (capabilities != null) {
                    for (int c : capabilities) {
                        builder.append("CONTROL_AF_AVAILABLE_MODES: (" + Integer.toString(c) + ")");
                        if (c == CameraMetadata.CONTROL_AF_MODE_OFF) {
                            builder.append(" CONTROL_AF_MODE_OFF");
                        } else if (c == CameraMetadata.CONTROL_AF_MODE_AUTO) {
                            builder.append(" CONTROL_AF_MODE_AUTO");
                        } else if (c == CameraMetadata.CONTROL_AF_MODE_MACRO) {
                            builder.append(" CONTROL_AF_MODE_MACRO");
                        } else if (c == CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_VIDEO) {
                            builder.append(" CONTROL_AF_MODE_CONTINUOUS_VIDEO");
                        } else if (c == CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE) {
                            builder.append(" CONTROL_AF_MODE_CONTINUOUS_PICTURE");
                        } else if (c == CameraMetadata.CONTROL_AF_MODE_EDOF) {
                            builder.append(" CONTROL_AF_MODE_EDOF");
                        }
                        builder.append("\n");
                    }
                }

                float[] apertures = cameraCharacteristics.get(CameraCharacteristics.LENS_INFO_AVAILABLE_APERTURES);
                if (apertures != null) {
                    if (apertures != null) {
                        for (float f : apertures) {
                            builder.append("LENS_INFO_AVAILABLE_APERTURES: " + Float.toString(f) + "\n");
                        }
                    }
                }
                Integer maxAf = cameraCharacteristics.get(CameraCharacteristics.CONTROL_MAX_REGIONS_AF);
                builder.append("CONTROL_MAX_REGIONS_AF: " + Integer.toString(maxAf) + "\n");
                float[] lengths = cameraCharacteristics.get(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS);
                if (lengths != null) {
                    for (float f : lengths) {
                        builder.append("LENS_INFO_AVAILABLE_FOCAL_LENGTHS: " + Float.toString(f) + "\n");
                    }
                }

                Float dis = cameraCharacteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE);
                builder.append("LENS_INFO_MINIMUM_FOCUS_DISTANCE: " + ((dis == null) ? "null" : dis.toString()) + "\n");

                StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
                Size[] sizes = map.getOutputSizes(ImageFormat.YUV_420_888);

                for (int i = 0; i < sizes.length; i++) { //遍历所有Size
                    Size itemSize = sizes[i];
                    // Log.e(TAG, "当前itemSize 宽=" + itemSize.getWidth() + "高=" + itemSize.getHeight());
                    builder.append("Available Size: (" + itemSize.getWidth() + "," + itemSize.getHeight() + ")\n");
                }

                Log.i("CAM", builder.toString());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return builder.toString();
    }

}
