package com.ez.ertc.meeting.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

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.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author xiezuoyuan
 * @time 2018/11/21 19:28
 */
public class MeetingEndDialog extends Dialog {

    private Disposable countDownDisposable;
    private OnConfirmListener onConfirmListener;
    public MeetingEndDialog(@NonNull Context context, OnConfirmListener onConfirmListener) {
        super(context , R.style.ezmCommonDialog);
        this.onConfirmListener = onConfirmListener;
        getWindow().setGravity(Gravity.CENTER);
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ezm_ezviz_dialog_layout_count_down);
        final TextView tvButton = findViewById(R.id.tvButton);
        tvButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvButton.setText(String.format(getContext().getString(R.string.ys_mt_room_meeting_end_btn_format),3));
        //从 1 开始发射 COUNT_DOWN_TIME 个数字为：0-COUNT_DOWN_TIME 依次输出，延时0s执行，每1s发射一次
        Observable.intervalRange(0, 4, 0, 1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        tvButton.setText(String.format(getContext().getString(R.string.ys_mt_room_meeting_end_btn_format),3-aLong));
                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        dismiss();
                    }
                })
                .subscribe(new YsCallback<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        countDownDisposable = d;
                    }

                    @Override
                    public void onNext(Object o) {

                    }
                });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (countDownDisposable != null && !countDownDisposable.isDisposed()){
                    countDownDisposable.dispose();
                }
                onConfirmListener.onConfirm();
            }
        });
    }

    public interface OnConfirmListener {
        void onConfirm();
    }

}
