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.viewholder.BaseViewHolder;
import com.cooleshow.base.ui.fragment.BaseFragment;
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 java.util.ArrayList;

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

/**
 * Author by pq, Date on 2022/6/13.
 * 直播间连麦中列表页面
 */
public class LiveOnMicFragment extends BaseFragment<FragmentLiveOnMicLayoutBinding> {
    RecyclerView recyclerView;
    private LiveMicManagerDialog.OnEventListener mEventListener;
    private ArrayList<User> mList = new ArrayList<>();
    ;
    private Adapter mAdapter;


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

    @Override
    protected void initView(View rootView) {

    }

    @Override
    protected void initData() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);

        mAdapter = new Adapter(R.layout.item_live_mic_manager_layout);
//        mAdapter.setOnSubViewClickListener(new BaseRecyclerAdapter.OnSubViewClickListener() {
//            @Override
//            public void onSubViewClick(View v, int position) {
//                if (v.getId() == R.id.tv_handle) {
//                    //下麦操作
//                    User user = mList.get(position);
//                    if (mEventListener != null) {
//                        mEventListener.onAgreeApply(user);
//                    }
//                }
//            }
//        });
        recyclerView.setAdapter(mAdapter);
    }

    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_down_all_mic) {
            //全部下麦
            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) {
            super(layoutResId);
        }

        @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);
            //名称

            viewHolder.tvName.setText(user.getUserName());
            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);
            }

        }
    }
}
