package com.sjkj.appthreefloor_tsgz.activity;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.MediaController;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.activity.viewModel.VideoViewModel;
import com.sjkj.appthreefloor_tsgz.databinding.ActivityMyVideoPlayerBinding;
import com.sjkj.base_lib.utils.ToastUtils;
import com.sjkj.base_lib.view.BaseActivity;

import java.io.File;

;

/**
 * 视频播放页
 */
public class MyVideoPlayerActivity extends BaseActivity {

    private ActivityMyVideoPlayerBinding binding;
    private VideoViewModel viewModel;
    //    private String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
    private boolean isFull = false;

    @Override
    protected int initLayout() {
        return R.layout.activity_my_video_player;
    }

    @Override
    public void initBinding() {
        super.initBinding();
        binding = (ActivityMyVideoPlayerBinding) getViewDataBinding();
        viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(VideoViewModel.class);
        binding.setViewModel(viewModel);
        binding.setLifecycleOwner(this);
    }

    @Override
    public void initView() {
        super.initView();
        getTitleView().setText("视频播放");
        getSupportActionBar().show();
        viewModel.videoUrl.set(getIntent().getStringExtra("videoUrl"));
        if (TextUtils.isEmpty(viewModel.videoUrl.get())){
            ToastUtils.show("视频资源有误");
            return;
        }
        if (viewModel.videoUrl.get().startsWith("http")){
            binding.videoView.setVideoURI(Uri.parse(viewModel.videoUrl.get()));
        }else {
            if (new File(viewModel.videoUrl.get()).exists()){
                binding.videoView.setVideoPath(viewModel.videoUrl.get());
            }else {
                ToastUtils.show("视频资源不存在");
            }
        }

        Glide.with(this)
                .setDefaultRequestOptions(
                        new RequestOptions()
                                .frame(1000)
                                .centerCrop()
                )
                .load(viewModel.videoUrl.get())
                .into(binding.iv);
        binding.videoView.setMediaController(new MediaController(this));
        binding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                binding.progressView.setVisibility(View.GONE);
                
                binding.iv.setVisibility(View.GONE);
            }
        });
        binding.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                binding.progressView.setVisibility(View.GONE);
                binding.iv.setVisibility(View.GONE);
            }
        });
        binding.videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
                    binding.progressView.setVisibility(View.GONE);
                    binding.iv.setVisibility(View.GONE);
                } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
                    binding.progressView.setVisibility(View.GONE);
                    binding.iv.setVisibility(View.GONE);
                }
                return true;
            }
        });
        binding.videoView.seekTo(1);
        binding.videoView.requestFocus();
        binding.progressView.setVisibility(View.VISIBLE);
        binding.iv.setVisibility(View.VISIBLE);
        binding.videoView.start();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!binding.videoView.isPlaying()) {
            binding.videoView.start();
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (binding.videoView.isPlaying()) {
            binding.videoView.pause();
        }
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //继承自AppCompatActivity时使用
            getSupportActionBar().hide();
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) binding.videoFrame.getLayoutParams();
            params.width = metrics.widthPixels;
            params.height = metrics.heightPixels;
            params.leftMargin = 0;
            binding.videoFrame.setLayoutParams(params);
            isFull = true;
        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            getSupportActionBar().show();
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) binding.videoFrame.getLayoutParams();
            params.width = metrics.widthPixels;
            params.height = /*(int) (300*metrics.density);*/metrics.heightPixels;
            params.leftMargin = 0;
            binding.videoFrame.setLayoutParams(params);
            isFull = false;
        }
    }

    /**
     * 切换横竖屏
     */
    public void switchScreen() {
        if (isFull) {
            isFull = false;
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            isFull = true;
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        }
    }


    @Override
    public void onBackPressed() {
        if (isFull) {
            switchScreen();
        } else {
            this.finish();
        }
    }
}
