package com.tofly.ghrq_route.common.ui.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.listener.ErrorListener;
import com.cjt2325.cameralibrary.listener.JCameraListener;
import com.cjt2325.cameralibrary.util.DeviceUtil;
import com.cjt2325.cameralibrary.util.FileUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.C;

public class CameraActivity extends AppCompatActivity {
    private JCameraView jCameraView;

    public static final int VIDEO_REQUEST_CAPTURE_CODE = 101;
    public static final int VIDEO_REQUEST_RECORD_CODE = 102;
    public static final int VIDEO_REQUEST_ERROR_CODE = 103;

    public static final String VIDEO_FIRST_FRAME_KEY = "first_frame_path";
    public static final String VIDEO_VIDEO_URL_KEY = "video_url_path";
    public static final String VIDEO_VIDEO_LENGTH_KEY = "video_length";
    public static final String VIDEO_VIDEO_PROGRESS_KEY = "video_progress";
    public static final String VIDEO_VIDEO_NAME_KEY = "video_name";
    public static final String VIDEO_SCREEN_ORIENTATION = "video_screen_orientation";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_camera);
        jCameraView = findViewById(R.id.jcameraview);
        //设置视频保存路径
        jCameraView.setSaveVideoPath(C.dir.images);
        jCameraView.setFeatures(JCameraView.BUTTON_STATE_ONLY_RECORDER);
        jCameraView.setTip("长按录制视频");
        jCameraView.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE);
        jCameraView.setErrorLisenter(new ErrorListener() {
            @Override
            public void onError() {
                //错误监听
                Log.i("CJT", "camera error");
                Intent intent = new Intent();
                setResult(VIDEO_REQUEST_ERROR_CODE, intent);
                finish();
            }

            @Override
            public void AudioPermissionError() {
                Toast.makeText(CameraActivity.this, "请给予录音权限！", Toast.LENGTH_SHORT).show();
            }
        });

        //JCameraView监听
        jCameraView.setJCameraLisenter(new JCameraListener() {
            @Override
            public void captureSuccess(Bitmap bitmap) {
                //获取图片bitmap
//                Log.i("JCameraView", "bitmap = " + bitmap.getWidth());
                String path = FileUtil.saveBitmap(C.dir.images, bitmap);
                Intent intent = new Intent();
                intent.putExtra("path", path);
                setResult(VIDEO_REQUEST_CAPTURE_CODE, intent);
                finish();
            }

            @Override
            public void recordSuccess(String url, Bitmap firstFrame, long videoLength) {
                //获取视频路径
                String path = FileUtil.saveBitmap(C.dir.images, firstFrame);
                Log.i("CJT", "url = " + url + ", Bitmap = " + path);
                Intent intent = new Intent();
                intent.putExtra(VIDEO_FIRST_FRAME_KEY, path);
                intent.putExtra(VIDEO_VIDEO_URL_KEY, url);
                intent.putExtra(VIDEO_VIDEO_LENGTH_KEY, videoLength);
                setResult(VIDEO_REQUEST_RECORD_CODE, intent);
                finish();
            }
        });

        jCameraView.setLeftClickListener(() -> this.finish());
        jCameraView.setRightClickListener(() -> Toast.makeText(this,"Right",Toast.LENGTH_SHORT).show());

        Log.i("CJT", DeviceUtil.getDeviceModel());
    }

    @Override
    protected void onStart() {
        super.onStart();
        //全屏显示
        if (Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        } else {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(option);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        jCameraView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        jCameraView.onPause();
    }
}
