package com.tenlionsoft.baselib.core.widget;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.R2;
import com.tenlionsoft.baselib.constant.PathConfig;
import com.tenlionsoft.baselib.core.camera.JCameraView;
import com.tenlionsoft.baselib.core.camera.listener.ErrorListener;
import com.tenlionsoft.baselib.core.camera.listener.JCameraListener;
import com.tenlionsoft.baselib.core.widget.base.BaseActivity;
import com.tenlionsoft.baselib.utils.ImageUtils;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 作者: Adam
 * 日期: 2021-10-16
 * 邮箱: itgaojian@163.com
 * 描述: 摄像或拍照
 */
@Route(path = PathConfig.PATH_MODULE_BASELIB_TAKE_PHOTO)
public class TakeVideoActivity extends BaseActivity {


    @BindView(R2.id.jcv_camera)
    JCameraView mJCameraView;
    private String mPath;
    private Unbinder mBind;
    private String mType;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_take_video;
    }

    @Override
    protected void doShowOptions() {

    }

    @Override
    protected void clearSearch() {

    }

    @Override
    protected void doSearchByTitle() {

    }

    @Override
    public void initData() {
        mBind = ButterKnife.bind(this);
        refreshView(STATE_LOAD_SUCCESS);
        mTvBaseTitle.setText("拍摄");
        initView();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        mPath = getIntent().getStringExtra("path");
        mType = getIntent().getStringExtra("type");
        recordVideo();
    }

    /**
     * 录制视频
     */
    private void recordVideo() {
        mJCameraView.setSaveVideoPath(mPath);
        //设置只能录像或只能拍照或两种都可以（默认两种都可以）
        if ("video".equals(mType)) {
            mJCameraView.setFeatures(JCameraView.BUTTON_STATE_ONLY_RECORDER);
        } else {
            mJCameraView.setFeatures(JCameraView.BUTTON_STATE_ONLY_CAPTURE);
        }
        //设置视频质量
        mJCameraView.setMediaQuality(JCameraView.MEDIA_QUALITY_LOW);
        mJCameraView.setErrorLisenter(new ErrorListener() {
            @Override
            public void onError() {
                //打开Camera失败回调
                Log.i("CJT", "open camera error");
                ToastUtils.show("打开摄像头失败.");
            }

            @Override
            public void AudioPermissionError() {
                //没有录取权限回调
                Log.i("CJT", "AudioPermissionError");
                ToastUtils.show("摄像头未授权.");
            }
        });

        mJCameraView.setJCameraLisenter(new JCameraListener() {

            @Override
            public void captureSuccess(Bitmap bitmap) {
                //获取图片bitmap
                Log.i("JCameraView", "bitmap = " + bitmap.getWidth());
                String filePath = mPath + File.separator + System.currentTimeMillis() + ".jpg";
                boolean save = ImageUtils.save(bitmap, filePath, Bitmap.CompressFormat.JPEG);
                if (save) {
                    Intent intent = getIntent();
                    intent.putExtra("name", filePath);
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                } else {
                    ToastUtils.show("照片拍摄失败,请稍后重试.");
                    finish();
                }

            }

            @Override
            public void recordSuccess(String s, Bitmap bitmap) {
                //获取视频路径
                Log.i("CJT", "url = " + s);
                //重新命名
                if (!TextUtils.isEmpty(s)) {
                    File file = new File(s);
                    File newFile = new File(mPath + File.separator + System.currentTimeMillis() + ".mp4");
                    if (file.exists()) {
                        boolean b = file.renameTo(newFile);
                        Log.e("TextureVideoActivity", "recordSuccess: " + b);
                        if (b) {
                            Intent intent = getIntent();
                            intent.putExtra("name", newFile.getAbsolutePath());
                            setResult(Activity.RESULT_OK, intent);
                            finish();
                        } else {
                            ToastUtils.show("视频录制失败,请重新录制.");
                        }
                    } else {
                        Log.e("TextureVideoActivity", "recordSuccess: 不存在");
                        ToastUtils.show("视频录制失败,请重新录制.");
                    }
                } else {
                    Log.e("TextureVideoActivity", "recordSuccess:路径未空 ");
                    ToastUtils.show("视频录制失败,请重新录制.");
                }
            }
        });
        //左边按钮点击事件
        mJCameraView.setLeftClickListener(this::finish);
    }

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