package com.common.im.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.common.im.adapter.GroupMemberAdapter;
import com.common.im.bean.GroupMemberBean;
import com.common.im.contract.GroupMemberContract;
import com.common.im.presenter.GroupMemberPresenter;
import com.common.im_ui.databinding.ActivityGroupMemberBinding;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.base.widgets.DensityUtil;
import com.mcxtzhang.indexlib.IndexBar.widget.IndexBar;
import com.mcxtzhang.indexlib.suspension.SuspensionDecoration;

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

import io.rong.imkit.RongIM;

/**
 * 创建日期：2022/6/13 15:25
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.ChatCenter.CHAT_GROUP_MEMBER)
public class GroupMemberActivity extends BaseMVPActivity<ActivityGroupMemberBinding, GroupMemberPresenter> implements GroupMemberContract.GroupMemberView, View.OnClickListener {
    @Override
    public void onClick(View v) {

    }

    EditText etSearch;
    ImageView ivSearch;
    RecyclerView recyclerview;
    IndexBar livLetters;
    TextView tvHint;
    private String search;
    private LinearLayoutManager mManager;
    private GroupMemberAdapter adapter;
    private SuspensionDecoration mDecoration;
    private List<GroupMemberBean> mDatas = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
    }

    @Override
    protected void initView() {
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "群聊成员");
        etSearch = viewBinding.etSearch;
        ivSearch = viewBinding.ivSearch;
        recyclerview = viewBinding.expendList;
        livLetters = viewBinding.livLetters;
        tvHint = viewBinding.tvHint;


        recyclerview.setLayoutManager(mManager = new LinearLayoutManager(this));
        RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        recyclerview.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 100);

        adapter = new GroupMemberAdapter(mDatas);
        recyclerview.setAdapter(adapter);
        mDecoration = new SuspensionDecoration(this, mDatas);
        mDecoration.setColorTitleBg(getResources().getColor(com.cooleshow.base.R.color.color_f6f8f9));
        mDecoration.setmTitleHeight(DensityUtil.dp2px(this, 30));
        recyclerview.addItemDecoration(mDecoration);
        livLetters.setmPressedShowTextView(tvHint)//设置HintTextView
                .setNeedRealIndex(false)//设置需要真实的索引
                .setmLayoutManager(mManager);//设置RecyclerView的LayoutManager
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                GroupMemberBean item = (GroupMemberBean) adapter.getItem(position);
                RongIM.getInstance().startPrivateChat(GroupMemberActivity.this, item.getUserId() + "", item.getNickname());

            }
        });

    }

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

    @Override
    protected GroupMemberPresenter createPresenter() {
        return new GroupMemberPresenter();
    }

    @Override
    public void initData() {
        super.initData();
        List<GroupMemberBean> list = (List<GroupMemberBean>) getIntent().getSerializableExtra("list");
        if (list == null) {
            list = new ArrayList<>();
        }
        mDatas.clear();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (null != list.get(i)) {
                    mDatas.add(list.get(i));
                }
            }
        }
        try {

            livLetters.setmSourceDatas(mDatas)//设置数据
                    .invalidate();
            mDecoration.setmDatas(mDatas);
            adapter.notifyDataSetChanged();
        } catch (Exception e) {

        }
    }
}
