package com.zhaohaoting.framework.imagepicker.ui;


import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;

import com.zhaohaoting.framework.R;
import com.zhaohaoting.framework.abs.BaseNoActionBarActivity;
import com.zhaohaoting.framework.abs.contract.BaseContract;
import com.zhaohaoting.framework.databinding.ActivityCameraBinding;
import com.zhaohaoting.framework.imagepicker.camera.JCameraView;
import com.zhaohaoting.framework.imagepicker.camera.listener.JCameraListener;
import com.zhaohaoting.framework.imagepicker.camera.util.FileUtil;
import com.zhaohaoting.framework.utils.DataCache;

/**
 * 录制视频或者照相
 */
public class CameraActivity extends BaseNoActionBarActivity {

    private ActivityCameraBinding binding;

    @Override
    protected BaseContract.BasePresenter initPresenter() {
        return null;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_camera;
    }

    @Override
    protected void onInit() {
        setLightMode(getResources().getColor(R.color.transparent), false, true);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        binding = (ActivityCameraBinding) rootDataBinding;

        binding.jcameraview.setSaveVideoPath(DataCache.getVoiceCacheDir(this));
        //默认拍照录像都可以
        int state = getIntent().getIntExtra("state", JCameraView.BUTTON_STATE_BOTH);
        if (state != JCameraView.BUTTON_STATE_BOTH) {
            binding.jcameraview.setTip("");
        }
        binding.jcameraview.setFeatures(state);

        binding.jcameraview.setLeftClickListener(() -> finish());
        /**
         * JCameraView监听
         */
        binding.jcameraview.setJCameraLisenter(new JCameraListener() {

            @Override
            public void captureSuccess(Bitmap bitmap) {
                /**
                 * 获取图片bitmap
                 */
                String path = FileUtil.saveBitmap("image", bitmap);
                Intent intent = new Intent();
                intent.putExtra("path", path);
                setResult(101, intent);
                finish();
            }

            @Override
            public void recordSuccess(String url, Bitmap firstFrame) {
                Intent intent = new Intent();
                intent.putExtra("path", url);
                setResult(102, intent);
                finish();
            }
        });
    }

    @Override
    public 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
    public void onResume() {
        super.onResume();
        binding.jcameraview.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        binding.jcameraview.onPause();
    }
}