package com.ez.ertc.meeting.widget;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.ez.basertc.view.VideoCanvasView;
import com.ez.ertc.meeting.R;
import com.ez.ertc.meeting.base.YsCallback;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ErtcTextureView extends FrameLayout {
    public static final int PERIOD_DEFAULT = 1000;//1秒取不到视频流显示loading
    public static final int PERIOD_SHARE_DESKTOP = 3000;//共享时3秒取不到视频流显示loading
    private VideoCanvasView videoCanvasView;
    private WaterMarkView waterMarkView;
    private ProgressBar progressBar;
    private volatile long millis;
    private Disposable disposable;
    private boolean enableScale = false;
    public boolean isShowLoadingProgress = true;

    private int mLoadingPeriod = PERIOD_DEFAULT;

    public void setLoadingPeriod(boolean isShare){
        mLoadingPeriod = isShare? PERIOD_SHARE_DESKTOP : PERIOD_DEFAULT;
    }

    public ErtcTextureView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public ErtcTextureView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ErtcTextureView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ErtcTextureView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context){
        View view = View.inflate(context, R.layout.ertc_layout_texture_view,this);
        videoCanvasView = view.findViewById(R.id.videoCanvasView);
        waterMarkView = view.findViewById(R.id.waterMarkView);
        progressBar = view.findViewById(R.id.progressBar);
        millis = System.currentTimeMillis();
        Observable.interval(PERIOD_DEFAULT, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new YsCallback<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(Object o) {
                        if (progressBar.getVisibility() == GONE
                                && videoCanvasView.isShown()
                                && isShowLoadingProgress
                                && (System.currentTimeMillis() - millis >= mLoadingPeriod)){
                            progressBar.setVisibility(VISIBLE);
                        }
                    }
                });
    }
    @Override
    public void setVisibility(int visibility) {
        //从隐藏到显示时重置时间，防止有些一开始就loading有些1秒后才开始loading
        if (getVisibility() != VISIBLE && visibility == VISIBLE){
            millis = System.currentTimeMillis();
        }
        super.setVisibility(visibility);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
            disposable = null;
        }
    }

    public void setWaterMark(String nickname,String phone){
        waterMarkView.setWaterMark(nickname,phone);
    }
    public void showWaterMark(boolean show){
        waterMarkView.setVisibility(show ? VISIBLE : GONE);
    }

    public void setWaterMarkSize(int value){
        waterMarkView.setWaterMarkSize(value);
    }

    public void hideProgress() {
        isShowLoadingProgress = false;
        progressBar.setVisibility(GONE);
    }

    public void showProgress() {
        isShowLoadingProgress = true;
        progressBar.setVisibility(VISIBLE);
    }

    public VideoCanvasView getVideoCanvasView() {
        return videoCanvasView;
    }

    public void cleanLastFrame(){
        //todo 取消订阅
    }

    public void setEnableScale(Context context, boolean enableScale) {
        //todo 设置是否可以缩放
    }
}
