package com.ez.ertc.meeting;

import android.app.Activity;
import android.os.Handler;

import com.ez.basertc.RTCConstant;
import com.ez.ertc.meeting.base.YsCallback;
import com.ez.ertc.meeting.ui.ErtcRoomActivity;
import com.ez.ertc.meeting.utils.ErtcGenerateTestUserSig;
import com.ez.ertc.meeting.widget.WaitingDialog;
import com.ez.ertcengine.ERTCEngine;

import org.jetbrains.annotations.NotNull;

import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @author xiezuoyuan
 * @time 2019/11/5 19:16
 */
public class EnterRoomUtil {
    private static final String TAG = "EnterRoomUtil";

    public static void enter(final Activity activity, final String roomNo, final String userId, final Boolean isAudioOn,
                             final Boolean isVideoOn, final EzmCallback callback){

        WaitingDialog.showWaitingDialog(activity,activity.getResources().getString(R.string.ys_mt_join_ing),false);

        //初始化，获取token 加入ertc房间
        ErtcEzmHelper.getInstance().ertcEnterInitDebug(activity)
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
//                .flatMap(new Function<ERTCEngine, ObservableSource<String>>() {
//                    @Override
//                    public ObservableSource<String> apply(@NotNull ERTCEngine engine) throws Exception {
//                        return ErtcEzmHelper.getInstance().getErtcToken(userId, roomNo);
//                    }
//                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new YsCallback<ERTCEngine>() {
                    @Override
                    public void onNext(@NotNull final ERTCEngine engine) {
                        if (callback != null) {
//                            callback.onSuccess(ErtcGenerateTestUserSig.genTestUserSig(
//                                    ErtcEzmHelper.getInstance().getErtcTestAppIdDebug(),
//                                    ErtcEzmHelper.getInstance().getErtcTestSecretDebug(),
//                                    userId, roomNo
//                            ));
                            callback.onSuccess(ErtcEzmHelper.getInstance().getErtcToken());
                        }

                        new Handler().post(new Runnable() {
                            @Override
                            public void run() {
                                WaitingDialog.dismissWaitingDialog(activity);
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callback != null) {
                            callback.onError(e.getMessage(), e.getMessage());
                        }
                        new Handler().post(new Runnable() {
                            @Override
                            public void run() {
                                WaitingDialog.dismissWaitingDialog(activity);
                            }
                        });
                    }
                });
    }

}
