package com.zztrapp.zztrapp.ui.currency.activity;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.netease.yunxin.kit.meeting.sdk.NECallback;
import com.netease.yunxin.kit.meeting.sdk.NEJoinMeetingOptions;
import com.netease.yunxin.kit.meeting.sdk.NEJoinMeetingParams;
import com.netease.yunxin.kit.meeting.sdk.NEMeetingError;
import com.netease.yunxin.kit.meeting.sdk.NEMeetingIdDisplayOption;
import com.netease.yunxin.kit.meeting.sdk.NEMeetingKit;
import com.netease.yunxin.kit.meeting.sdk.NEWindowMode;
import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.bean.MeetingBean;
import com.zztrapp.zztrapp.databinding.ActivityQuickMeetingBinding;
import com.zztrapp.zztrapp.http.HttpMethods;
import com.zztrapp.zztrapp.http.ISubscriberOnNextListener;
import com.zztrapp.zztrapp.http.ProgressSubscriber;
import com.zztrapp.zztrapp.http.RxApiManager;
import com.zztrapp.zztrapp.utilcode.klogs.KLog;
import com.zztrapp.zztrapp.utilcode.util.SPUtils;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;

import java.util.HashMap;

/**
 * Describe 快速会议
 * Created by Bby on 2022/6/29
 * Email bby0856@163.com
 */
public class QuickMeetingActivity extends BaseActivity {

    private ActivityQuickMeetingBinding binding;
    private Dialog mDialog;

    @Override
    public View getLayoutId() {
        return binding.getRoot();
    }

    @Override
    protected void initBinding() {
        binding = ActivityQuickMeetingBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {
        mDialog = initProgressDialog(this, "");
        binding.navView.navTitle.setText("快速会议");
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initOnClick() {
        binding.openMeeting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String hyzt = binding.hyzt.getText().toString();
                if (TextUtils.isEmpty(hyzt)) {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show("请输入会议主题");
                } else {
                    if (!mDialog.isShowing()) {
                        mDialog.show();
                    }
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("hymc", hyzt);
                    map.put("bz", binding.bz.getText().toString());
                    createSphy(map);
                }
            }
        });
    }

    //提交
    private void createSphy(HashMap<String, Object> map) {
        ISubscriberOnNextListener<HttpResult<MeetingBean>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<MeetingBean>>() {

            @Override
            public void onNext(HttpResult<MeetingBean> httpResult) {
                if (httpResult.getCode() == 200) {
                    SPUtils.getInstance().put("sphyId", httpResult.getResult().getId());
                    NEJoinMeetingParams params = new NEJoinMeetingParams();     //会议参数
                    params.meetingId = httpResult.getResult().getHyh();                             //会议号
                    params.displayName = httpResult.getResult().getHymc();             //会议昵称
//                    params.password = "123456";                                 //会议密码
                    NEJoinMeetingOptions options = new NEJoinMeetingOptions();   //会议选项，可自定义会中的 UI 显示、菜单、行为等
                    options.noVideo = true;                                      //入会时关闭视频，默认为 true
                    options.noAudio = true;                                      //入会时关闭音频，默认为 true
                    options.noInvite = false;                                    //入会隐藏"邀请"按钮，默认为 false
                    options.noChat = false;                                      //入会隐藏"聊天"按钮，默认为 false
                    options.noWhiteBoard = false;                                //入会隐藏白板入口，默认为 false
                    options.noGallery = false;                                //入会隐藏设置"画廊模式"入口，默认为 false
                    options.noSwitchCamera = false;                                //入会隐藏"切换摄像头"功能入口，默认为 false
                    options.noSwitchAudioMode = false;                               //入会隐藏"切换音频模式"功能入口，默认为 false
                    options.noRename = false;                                //入会隐藏"改名"功能入口，默认为 false
                    options.showMeetingTime = false;                                //设置入会后是否显示会议持续时间，默认为 false
//                    options.noCloudRecord = true;                                //入会隐藏"录制中"显示，默认为 true
                    options.noMinimize = true;                                   //入会是否允许最小化会议页面，默认为 true
                    options.defaultWindowMode = NEWindowMode.normal;             //入会默认会议视图模式
                    options.meetingIdDisplayOption = NEMeetingIdDisplayOption.DISPLAY_ALL;   //设置会议中会议 ID 的显示规则，默认为全部显示

                    NEMeetingKit.getInstance().getMeetingService().joinMeeting(QuickMeetingActivity.this, params, options, new NECallback<Void>() {
                        @Override
                        public void onResult(int resultCode, String resultMsg, Void result) {
                            if (resultCode == NEMeetingError.ERROR_CODE_SUCCESS) {
                                finish();
                                //加入会议成功
                                KLog.e("加入会议成功");
                                if (mDialog != null && mDialog.isShowing()) {
                                    mDialog.dismiss();
                                }
                            } else {
                                //加入会议失败
                                KLog.e("加入会议失败" + resultCode);
                                KLog.e(resultMsg);
                                if (mDialog != null && mDialog.isShowing()) {
                                    mDialog.dismiss();
                                }
                                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show("开启会议失败");
                            }
                        }
                    });
                    if (mDialog != null && mDialog.isShowing()) {
                        mDialog.dismiss();
                    }
                } else {
                    if (mDialog != null && mDialog.isShowing()) {
                        mDialog.dismiss();
                    }
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                if (mDialog != null && mDialog.isShowing()) {
                    mDialog.dismiss();
                }
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<MeetingBean>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, this, false);
        RxApiManager.get().add("createSphy", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).createSphy(httpResultProgressSubscriber, map);
    }

    public Dialog initProgressDialog(Context context, String title) {
        if (mDialog == null) {
            mDialog = new Dialog(context, R.style.LoadingDialogStyle);
            mDialog.setContentView(R.layout.layout_dialog);
            mDialog.setCancelable(true);
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            TextView msg = mDialog.findViewById(R.id.id_tv_loadingmsg);
            msg.setText(title);
            msg.setVisibility(View.GONE);
            mDialog.setCancelable(false);
        }
        return mDialog;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
        RxApiManager.get().cancel("createSphy");
    }
}