package com.ycgis.macall.personalcenter.v.activity;

import android.os.Build;
import android.os.Bundle;
import android.view.View;

import com.rs.macall.androidx.basemodel.base.BaseViewBindActivity;
import com.rs.macall.androidx.basemodel.utils.StringUtil;
import com.rs.macall.androidx.basemodel.utils.ToastUtil;
import com.ycgis.macall.personalcenter.databinding.ActivityPlayVideosBinding;

import java.util.concurrent.TimeUnit;

import cn.jzvd.Jzvd;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * 播放视频
 */
public class PlayVideosActivity extends BaseViewBindActivity<ActivityPlayVideosBinding> {

    private String url;
    @Override
    protected ActivityPlayVideosBinding getViewBinding() {
        return ActivityPlayVideosBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {
        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 readInstanceState(Bundle savedInstanceState) {
        url = getIntent().getStringExtra("url");
    }

    @Override
    protected void start() {
        if (StringUtil.isNullOrEmpty(url)){
            ToastUtil.centered(getContext(),"错误的视频地址");
            delayFinish();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Jzvd.backPress();
    }
    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    /**
     * 延时关闭当前Activity
     */
    protected void delayFinish() {
        delayFinish(1500);
    }

    /**
     * 延时关闭当前Activity
     *
     * @param millisecond 延迟关闭时间毫秒
     */
    protected void delayFinish(int millisecond) {
        Observable.timer(millisecond, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long aLong) {
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}