package com.cooleshow.teacher.ui.live;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cooleshow.base.ui.fragment.BaseFragment;
import com.cooleshow.base.utils.LogUtils;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.base.widgets.dialog.CommonConfirmDialog;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.databinding.FragmentLiveApplyMicLayoutBinding;
import com.cooleshow.teacher.widgets.dialog.LiveMicManagerDialog;
import com.huawei.hms.utils.UIUtil;
import com.rong.io.live.bean.User;
import com.rong.io.live.config.LiveConfig;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Author by pq, Date on 2022/6/13.
 * 直播间申请连麦列表页面
 */
public class LiveApplyMicFragment extends BaseFragment<FragmentLiveApplyMicLayoutBinding> implements View.OnClickListener {
    RecyclerView recyclerView;

    private LiveMicManagerDialog.OnEventListener mEventListener;

    private ArrayList<User> mList = new ArrayList<>();
    private Adapter mAdapter;
    private CommonConfirmDialog mConfirmDialog;


    @Override
    protected FragmentLiveApplyMicLayoutBinding getLayoutView() {
        return FragmentLiveApplyMicLayoutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView(View rootView) {
        recyclerView = mViewBinding.recyclerView;
    }

    @Override
    protected void initData() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        mViewBinding.tvEnableMic.setOnClickListener(this);
        mViewBinding.tvRefuseAll.setOnClickListener(this);
        mAdapter = new Adapter(R.layout.item_live_mic_manager_layout, mList);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (UiUtils.isFastClick(500)) {
                    ToastUtil.getInstance().showShort("请勿频繁操作~");
                    return;
                }
                if (mAdapter != null && position < mAdapter.getData().size()) {
                    User user = mAdapter.getData().get(position);
                    if (user != null && mEventListener != null) {
                        mEventListener.onAgreeApply(user);
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_refuse_all) {
            //全部拒绝
            showCommonTipDialog("确认后所有连麦中学员都将下麦", "确认", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mEventListener != null) {
                        if (mConfirmDialog != null) {
                            mConfirmDialog.dismiss();
                        }
                        mEventListener.onRefuseAllMicApply();
                    }
                }
            });
            return;
        }

        if (id == R.id.tv_enable_mic) {
            //禁止连麦
            if (LiveConfig.MODE_LIVE_IS_ENABLE_MIC == 0) {
                //当前允许连麦,触发禁止连麦
                showCommonTipDialog("是否确认禁止学员连麦？", "确认", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mConfirmDialog != null) {
                            mConfirmDialog.dismiss();
                        }
                        if (mEventListener != null) {
                            LiveConfig.MODE_LIVE_IS_ENABLE_MIC = 1;
                            mViewBinding.tvEnableMic.setText("开启连麦");
                            mEventListener.onSwitchMicMode(true);
                        }
                    }
                });
            } else {
                //当前禁止连麦,触发允许连麦
                if (mEventListener != null) {
                    LiveConfig.MODE_LIVE_IS_ENABLE_MIC = 0;
                    mViewBinding.tvEnableMic.setText("禁止连麦");
                    mEventListener.onSwitchMicMode(false);
                }
            }
            return;
        }
    }

    /**
     * 刷新
     *
     * @param applyListData
     */
    public void refresh(ArrayList<User> applyListData) {
        mList.clear();
        mList.addAll(applyListData);
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    public void setOnEventListener(LiveMicManagerDialog.OnEventListener listener) {
        this.mEventListener = listener;
    }

    private class Adapter extends BaseQuickAdapter<User, BaseViewHolder> {


        public Adapter(int layoutResId, @Nullable List<User> data) {
            super(layoutResId, data);
            addChildClickViewIds(R.id.tv_handle);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, User user) {
            //创建人头像
            ImageView iv_avatar = helper.getView(R.id.iv_avatar);
            Glide.with(getContext()).load(user.getPortraitUrl()).placeholder(R.drawable.icon_teacher_default_head).error(R.drawable.icon_teacher_default_head).into(iv_avatar);
            //名称
            helper.setText(R.id.tv_name, user.getUserName());
            helper.setText(R.id.tv_tip, "申请连麦中");
            helper.setText(R.id.tv_handle, "上麦");
        }
    }

    private void showCommonTipDialog(String content, String confirmText, View.OnClickListener listener) {
        if (mConfirmDialog == null) {
            mConfirmDialog = new CommonConfirmDialog(getContext());
        }
        if (!mConfirmDialog.isShowing()) {
            mConfirmDialog.show();
        }
        mConfirmDialog.setContent(content);
        mConfirmDialog.setConfirmText(confirmText);
        mConfirmDialog.setOnConfirmClickListener(listener);
    }

}
