package com.cooleshow.base.ui.video;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.MediaController;

import com.cooleshow.base.R;
import com.cooleshow.base.databinding.ActivityVideoPlayLayoutBinding;
import com.cooleshow.base.ui.activity.BaseActivity;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.utils.Utils;
import com.umeng.socialize.utils.CommonUtil;

/**
 * Author by pq, Date on 2022/6/28.
 */
public class VideoPlayActivity extends BaseActivity<ActivityVideoPlayLayoutBinding> implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, View.OnClickListener {
    public static final String VIDEO_PATH_KEY = "video_path";
    private MediaController mMediaController;

    public static void start(Context context, String videoPath) {
        Intent intent = new Intent(context, VideoPlayActivity.class);
        intent.putExtra(VIDEO_PATH_KEY, videoPath);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        Utils.setHeadView(viewBinding.viewStatusBar, this, 0);
        String videoPath = getIntent().getStringExtra(VIDEO_PATH_KEY);
        if (TextUtils.isEmpty(videoPath)) {
            ToastUtil.getInstance().showShort("视频地址不可为空");
            finish();
            return;
        }
        viewBinding.ivIconBack.setOnClickListener(this);
        viewBinding.videoView.setVideoPath(videoPath);
        viewBinding.videoView.requestFocus();
        mMediaController = new MediaController(this);
        viewBinding.videoView.setMediaController(mMediaController);
        viewBinding.videoView.setOnPreparedListener(this);
        viewBinding.videoView.setOnCompletionListener(this);
        viewBinding.videoView.setOnErrorListener(this);
        viewBinding.videoView.start();
    }

    @Override
    protected ActivityVideoPlayLayoutBinding getLayoutView() {
        return ActivityVideoPlayLayoutBinding.inflate(getLayoutInflater());
    }

    /**
     * \
     * videoView onPrepared
     *
     * @param mp
     */
    @Override
    public void onPrepared(MediaPlayer mp) {
    }

    /**
     * videoView onCompletion
     *
     * @param mp
     */
    @Override
    public void onCompletion(MediaPlayer mp) {

    }

    /**
     * videoView onError
     *
     * @param mp
     * @param what
     * @param extra
     * @return
     */
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        ToastUtil.getInstance().showShort("视频加载失败");
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewBinding.videoView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (viewBinding.videoView.canPause()) {
            viewBinding.videoView.pause();
        }
    }

    @Override
    protected void onDestroy() {
        mMediaController = null;
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_icon_back) {
            finish();
            return;
        }
    }
}
