package cn.finalteam.rxgalleryfinal.utils;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Log;

import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.ClassFileHelper;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.yalantis.ucrop.UCrop;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import cn.finalteam.rxgalleryfinal.api.CameraCallBack;

/**
 * Created by realicing on 2018/12/10.
 * realicing@sina.com
 */
public class CameraUtil {

    public static String openCamera(Activity activity, boolean isImage) {
        Intent captureIntent = isImage ? new Intent(MediaStore.ACTION_IMAGE_CAPTURE) : new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        if (captureIntent.resolveActivity(activity.getPackageManager()) == null) {
            G.shortToast("相机不可用");
            return null;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
        String filename = String.format(isImage ? IMAGE_STORE_FILE_NAME : VIDEO_STORE_FILE_NAME, dateFormat.format(new Date()));
        File fileImagePath = new File(ClassFileHelper.DIR, filename);
        String mImagePath = fileImagePath.getAbsolutePath();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileImagePath));
        } else {
            ContentValues contentValues = new ContentValues(1);
            contentValues.put(MediaStore.Images.Media.DATA, mImagePath);
            Uri uri = SheepApp.getInstance().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
            captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        }
        if (!isImage) {
            captureIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 15000L);
        }
        // video : 1: 高质量  0 低质量
        //        captureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        activity.startActivityForResult(captureIntent, isImage ? TAKE_IMAGE_REQUEST_CODE : TAKE_VIDEO_REQUEST_CODE);
        return mImagePath;
    }

    public static void onActivityResult(Activity activity, int requestCode, String path, CameraCallBack cameraCallBack) {
        Log.i(MediaUtils.class.getSimpleName(), "onActivityResult: requestCode=" + requestCode);
        if(path != null){
            activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
        }
        switch (requestCode) {
            case TAKE_IMAGE_REQUEST_CODE:
                if (path == null) {
                    Log.i(MediaUtils.class.getSimpleName(), "拍照成功");
                    cameraCallBack.onGetImageSuccess(null);
                    break;
                }
                String cropFilePath = path.substring(0, path.lastIndexOf(".")) + "temp.png";
                Log.i(MediaUtils.class.getSimpleName(), "拍照成功: " + cropFilePath);
                cameraCallBack.onGetImageSuccess(cropFilePath);
                UCrop.of(Uri.parse(path), Uri.parse(cropFilePath))
                        .start(activity);
                break;
            case TAKE_VIDEO_REQUEST_CODE:
                Log.i(MediaUtils.class.getSimpleName(), "摄像成功");
                cameraCallBack.onGetVideoSuccess();
                break;
            case UCrop.REQUEST_CROP:
                Log.i(MediaUtils.class.getSimpleName(), "裁剪成功");
                cameraCallBack.onCropImageSuccess();
                break;
        }
    }

    public static final String IMAGE_STORE_FILE_NAME = "IMG_%s.jpg";
    public static final String VIDEO_STORE_FILE_NAME = "VIDEO_%s.mp4";
    public static final int TAKE_IMAGE_REQUEST_CODE = 1001;
    public static final int TAKE_VIDEO_REQUEST_CODE = 1002;
}
