package com.wisewoods.eduandroid.ui;


import static com.tencent.liteav.meeting.ui.CreateMeetingActivity.VIDEO_QUALITY_HD;
import static com.tencent.trtc.TRTCCloudDef.TRTC_AUDIO_QUALITY_SPEECH;

import android.content.Intent;
import android.text.TextUtils;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tencent.liteav.meeting.model.TRTCMeeting;
import com.tencent.liteav.meeting.ui.MeetingMainActivity;
import com.wisewoods.base.app.UserManager;
import com.wisewoods.base.bean.BaseEvent;
import com.wisewoods.base.bean.RoomLiveBean;
import com.wisewoods.base.net.JCallback;
import com.wisewoods.base.page.BaseListActivity;
import com.wisewoods.base.utils.$;
import com.wisewoods.eduandroid.R;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.databinding.ActivityRoomLiveBinding;
import com.wisewoods.eduandroid.ui.adapter.RoomLiveAdapter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class RoomLiveActivity extends BaseListActivity<ActivityRoomLiveBinding> {

    private RoomLiveAdapter roomLiveAdapter;
    private int appId;
    private String userSig;
    private boolean mOpenAudio;
    private boolean mOpenCamera;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void reset(BaseEvent.RoomRefreshEvent event) {
        onRefresh();
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    public static void start(int appId, String userSig) {
        Intent intent = new Intent(ActivityUtils.getTopActivity(), RoomLiveActivity.class);
        intent.putExtra("appId", appId);
        intent.putExtra("userSig", userSig);
        ActivityUtils.startActivity(intent);
    }


    @Override
    protected void initListData() {
        showBarBack(binding.bar.tvTitle, "教室互动", binding.bar.ivBack);
        appId = getIntent().getIntExtra("appId", 0);
        userSig = getIntent().getStringExtra("userSig");
        srf = binding.srf;
        rv = binding.rvRoomLive;
        roomLiveAdapter = new RoomLiveAdapter();

        roomLiveAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.tv_join) {
                TRTCMeeting.sharedInstance(Utils.getApp())
                        .login(appId, UserManager.getInterlocutorsUid(), userSig,
                                (code, msg) -> {
                                    LogUtils.d("code: " + code + " msg:" + msg);
                                    if (code == 0) {
                                        if (roomLiveAdapter.getItem(position).createRoomId == 0) {
                                            $.toast("互动号无效");
                                            return;
                                        }
//                                        Intent intent = new Intent(ActivityUtils.getTopActivity(), CreateMeetingActivity.class);
//                                        intent.putExtra("TITLE", "教室互动");
//                                        ActivityUtils.startActivity(intent);
                                        String userId = UserManager.getInterlocutorsUid();
                                        String userAvatar = UserManager.getAvatar();
                                        String userName =
                                                TextUtils.isEmpty(UserManager.getUser().nickName) ?
                                                        UserManager.getUser().userName : UserManager.getUser().nickName;
                                        RoomLiveBean.DataBean item = roomLiveAdapter.getItem(position);
                                        boolean flag = false;
                                        String perUserIds = item.perUserIds;
                                        if (!TextUtils.isEmpty(perUserIds)) {
                                            String[] ids = perUserIds.split(",");
                                            List<String> stringList = Arrays.asList(ids);
                                            if (stringList.contains(UserManager.getUid())) {
                                                flag = true;
                                            }
                                        }
                                        MeetingMainActivity.enterRoom(this,
                                                item.createRoomId,
                                                userId,
                                                userName,
                                                userAvatar,
                                                mOpenCamera,
                                                mOpenAudio,
                                                TRTC_AUDIO_QUALITY_SPEECH,
                                                VIDEO_QUALITY_HD, flag);

                                    } else {
                                        $.toast(msg);
                                    }
                                });
            }
        });
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        return roomLiveAdapter;
    }

    @Override
    protected void getData(long page, long size) {
        App.api.getRoomLiveList(0, page, size)
                .enqueue(new JCallback<RoomLiveBean>() {
                    @Override
                    public void onS(Response<RoomLiveBean> response, RoomLiveBean roomLiveBean) {
                        getDataSuccess(roomLiveBean.data.rows, roomLiveBean.data.total);
                    }

                    @Override
                    public void onFailure(Call<RoomLiveBean> call, Throwable t) {
                        super.onFailure(call, t);
                        getDataFail();
                    }
                });
    }
}
