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

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

import androidx.appcompat.app.AppCompatActivity;

import com.cjt2325.cameralibrary.JCameraView;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.C;
import com.tofly.ghrq_route.common.util.image.ImageUtil;

import java.io.File;

public class CameraPreviewActivity extends AppCompatActivity {
    private JCameraView jCameraView;
    private Bitmap bitmapFirstFrame;
    private String videoUrl;
    private long videoLength = 0;

    @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 = (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.setOnlyPreview(true);

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


        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            String firstFrame = bundle.getString(CameraActivity.VIDEO_FIRST_FRAME_KEY, "");
            String videoUrl = bundle.getString(CameraActivity.VIDEO_VIDEO_URL_KEY, "");
            long videoLength = bundle.getLong(CameraActivity.VIDEO_VIDEO_LENGTH_KEY, 0);
            int orientation = bundle.getInt(CameraActivity.VIDEO_SCREEN_ORIENTATION, 1);
            if (orientation  == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
            String videoName = bundle.getString(CameraActivity.VIDEO_VIDEO_NAME_KEY, "");
            jCameraView.setMainTitle(videoName);
            Bitmap bitmap = ImageUtil.getBitmapFromUri(this, Uri.fromFile(new File(firstFrame)));
            if (bitmap != null)
                this.bitmapFirstFrame = bitmap;
            if (videoUrl != null)
                this.videoUrl = videoUrl;
            this.videoLength = videoLength;
        }

        jCameraView.setSurfaceListener(() -> jCameraView.playVideo(bitmapFirstFrame, videoUrl, videoLength));

        jCameraView.setCloseClickListener(() -> this.finish());
    }

    @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();
    }

    @Override
    public void onBackPressed() {
        jCameraView.onPlayStop();
        this.finish();
    }
}
