package com.daya.live_teaching.utils.helper;

import android.Manifest;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.utils.PermissionUtils;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.base.widgets.dialog.CommonConfirmDialog;
import com.cooleshow.usercenter.helper.UserHelper;
import com.daya.live_teaching.common.ResultCallback;
import com.daya.live_teaching.model.LoginResult;
import com.daya.live_teaching.repository.UserRepository;
import com.daya.live_teaching.rtc.RtcManager;
import com.daya.live_teaching.ui.LiveActivity;
import com.tbruyelle.rxpermissions3.RxPermissions;

import androidx.fragment.app.FragmentActivity;
import io.reactivex.rxjava3.disposables.Disposable;
import io.rong.imlib.RongIMClient;

/**
 * Author by pq, Date on 2022/6/29.
 * 网络教室进入helper
 */
public class CourseEnterHelper {
    public static final String SNARE_DRUM = "小军鼓";
    public static final String FLUTE = "长笛";

    public static void start(FragmentActivity activity, String sealClassId, String subjectName) {
        if (UiUtils.isFastClick()) {
            return;
        }
        checkPermission(activity, sealClassId, subjectName);
    }

    private static void checkPermission(FragmentActivity activity, String sealClassId, String subjectName) {
        Disposable subscribe = new RxPermissions(activity)
                .request(Manifest.permission.MODIFY_AUDIO_SETTINGS,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.CAMERA)
                .subscribe(isAllowPermission -> {
                    if (isAllowPermission) {
                        prepareOpenCourseRoom(activity, sealClassId, subjectName);
                    } else {
                        showTipDialog(activity);
                    }
                });
    }

    private static void prepareOpenCourseRoom(Activity activity, String sealClassId, String subjectName) {
        RongIMClient.ConnectionStatusListener.ConnectionStatus currentConnectionStatus = RongIMClient.getInstance().getCurrentConnectionStatus();
        if (currentConnectionStatus == RongIMClient.ConnectionStatusListener.ConnectionStatus.CONNECTED) {
            if (!TextUtils.isEmpty(subjectName) && (subjectName.equals(SNARE_DRUM) || subjectName.equals(FLUTE))) {
                RtcManager.getInstance().setVideoResolution(0, activity);
            } else {
                RtcManager.getInstance().setVideoResolution(3, activity);
            }
            UserRepository userRepository = new UserRepository(activity);
            String userName = UserHelper.getUserName();
            userRepository.login(sealClassId, false, userName, new ResultCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    ARouter.getInstance().build(RouterPath.OnlineClassroom.PATH_LIVE)
                            .withSerializable(LiveActivity.EXTRA_LOGIN_RESULT, loginResult)
                            .withBoolean(LiveActivity.EXTRA_CLOSE_CAMERA, false)
                            .navigation();
                }

                @Override
                public void onFail(int errorCode, String errorStr) {
                    if (activity != null) {
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (!TextUtils.isEmpty(errorStr)) {
                                    ToastUtil.getInstance().show(activity, errorStr);
                                } else {
                                    ToastUtil.getInstance().show(activity, "加入课堂失败,请重试；错误码:" + errorCode);
                                }
                            }
                        });
                    }
                }
            });
        } else {
            ToastUtil.getInstance().show(activity, "聊天服务未连接,进入教室异常");
        }
    }

    private static void showTipDialog(Activity activity) {
        CommonConfirmDialog confirmDialog = new CommonConfirmDialog(activity);
        confirmDialog.show();
        confirmDialog.setContent("加入课堂需要开启音视频相关权限");
        confirmDialog.setConfirmText("去设置");
        confirmDialog.setOnConfirmClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionUtils.toSelfSetting(activity);
            }
        });
    }
}
