package com.cooleshow.teacher.ui.live;

import android.text.TextUtils;
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.widgets.dialog.CommonConfirmDialog;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.databinding.FragmentLiveOnMicLayoutBinding;
import com.cooleshow.teacher.widgets.dialog.LiveMicManagerDialog;
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 LiveOnMicFragment extends BaseFragment<FragmentLiveOnMicLayoutBinding> implements View.OnClickListener {
    RecyclerView recyclerView;
    private LiveMicManagerDialog.OnEventListener mEventListener;
    private ArrayList<User> mList = new ArrayList<>();
    private Adapter mAdapter;
    private CommonConfirmDialog mConfirmDialog;

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

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

    @Override
    protected void initData() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        mViewBinding.tvDownAllMic.setOnClickListener(this);
        mAdapter = new Adapter(R.layout.item_live_mic_manager_layout, mList);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                int id = view.getId();
                if (id == R.id.tv_handle) {
                    //下麦操作
                    if (mAdapter != null && position < mAdapter.getData().size()) {
                        User user = mAdapter.getData().get(position);
                        if (mEventListener != null) {
                            mEventListener.onUnderMic(user);
                        }
                    }
                }
            }
        });
        recyclerView.setAdapter(mAdapter);
    }

    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_down_all_mic) {
            //全部下麦
            showCommonTipDialog("确认后所有连麦中学员都将下麦？", "确认", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mConfirmDialog != null) {
                        mConfirmDialog.dismiss();
                    }
                    if (mEventListener != null) {
                        mEventListener.onUnderAllMic();
                    }
                }
            });
            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, Adapter.ViewHolder> {

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

        @Override
        public int getItemCount() {
            return mList != null ? mList.size() : 0;
        }

        @Override
        protected void convert(@NonNull ViewHolder viewHolder, User user) {
            //创建人头像
            Glide.with(getContext()).load(user.getPortraitUrl()).placeholder(R.drawable.icon_teacher_default_head).error(R.drawable.icon_teacher_default_head).into(viewHolder.ivAvatar);
            //名称
            String userName = user.getUserName();
            viewHolder.tvName.setText(!TextUtils.isEmpty(userName) ? userName : "连麦用户");

            viewHolder.tvTip.setText("连麦中");
            viewHolder.tvHandle.setText("下麦");
        }

        public class ViewHolder extends BaseViewHolder {
            ImageView ivAvatar;
            TextView tvName;
            TextView tvTip;
            TextView tvHandle;

            public ViewHolder(View itemView) {
                super(itemView);
                ivAvatar = itemView.findViewById(R.id.iv_avatar);
                tvName = itemView.findViewById(R.id.tv_name);
                tvTip = itemView.findViewById(R.id.tv_tip);
                tvHandle = itemView.findViewById(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);
    }
}
