package com.zswc.creativity.utils;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;

import java.util.List;

public class FlashUtils {

    private Camera camera;
    private static boolean mIsLight;
    private CameraManager systemService;

    public void switchFlash(Context context) {
        if (mIsLight) {
            close(context);
        } else {
            open(context);
        }
    }


    public void open(Context context) {

        systemService = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        if (!mIsLight) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                try {
                    systemService.setTorchMode("0", true);
                    mIsLight = true;

                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            } else {
                if (camera == null) {
                    camera = Camera.open();
                }
                camera.startPreview();
                Camera.Parameters parameters = camera.getParameters();
                List<String> flashModes = parameters.getSupportedFlashModes();
                if (flashModes == null) {
                    return;
                }
                String flashMode = parameters.getFlashMode();
                if (!flashMode.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(parameters);
                }
                mIsLight = true;
            }
        }
    }


    public void close(Context context) {
        systemService = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);

        if (mIsLight) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                try {
                    systemService.setTorchMode("0", false);
                    mIsLight = false;
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            } else {
                Camera.Parameters parameters = camera.getParameters();
                List<String> flashModes = parameters.getSupportedFlashModes();
                if (flashModes == null) {
                    return;
                }
                String flashMode = parameters.getFlashMode();
                if (!flashMode.contains(Camera.Parameters.FLASH_MODE_OFF)) {
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    camera.setParameters(parameters);
                }
                mIsLight = false;
            }
        }

    }


    /**
     * 判断Android系统版本是否 >= M(API23)
     */
    private static boolean isM() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return true;
        } else {
            return false;
        }
    }

}
