package com.common.im.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

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.JoinGroupApplyAdapter;
import com.common.im.bean.GroupApplyBean;
import com.common.im.contract.JoinGroupApplyContract;
import com.common.im.presenter.JoinGroupApplyPresenter;
import com.common.im_ui.R;
import com.common.im_ui.databinding.ActivityJoinGroupApplyBinding;
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.EmptyViewLayout;

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

/**
 * 创建日期：2022/6/13 16:44
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.ChatCenter.CHAT_GROUP_JOIN_APPLY)
public class JoinGroupApplyActivity extends BaseMVPActivity<ActivityJoinGroupApplyBinding, JoinGroupApplyPresenter> implements JoinGroupApplyContract.JoinGroupApplyView, View.OnClickListener {

    private JoinGroupApplyAdapter joinGroupApplyAdapter;
    private List<GroupApplyBean> dataList = new ArrayList<>();
    private String targetId;
    private LinearLayout ll_bottom;

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

    @Override
    protected void initView() {
        targetId = getIntent().getStringExtra("targetId");
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "入群申请");
        viewBinding.toolbarInclude.tvRightText.setText("批量操作");
        viewBinding.toolbarInclude.tvRightText.setVisibility(View.VISIBLE);
        viewBinding.toolbarInclude.tvRightText.setTextColor(getResources().getColor(com.cooleshow.base.R.color.common_black));
        viewBinding.toolbarInclude.tvRightText.setOnClickListener(this);
        ll_bottom = viewBinding.llBottom;
        viewBinding.tvRefuse.setOnClickListener(this);
        viewBinding.tvPass.setOnClickListener(this);
        RecyclerView rvAddress = viewBinding.recyclerView;
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rvAddress.setLayoutManager(manager);
        joinGroupApplyAdapter = new JoinGroupApplyAdapter(dataList);
        EmptyViewLayout emptyViewLayout = new EmptyViewLayout(this);
        emptyViewLayout.setContent(com.cooleshow.base.R.drawable.icon_empty_content, "暂无入群申请");
        joinGroupApplyAdapter.setEmptyView(emptyViewLayout);
        rvAddress.setAdapter(joinGroupApplyAdapter);
        joinGroupApplyAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (joinGroupApplyAdapter.isSelect()) {
                    GroupApplyBean item = (GroupApplyBean) adapter.getItem(position);
                    item.setSelect(!item.isSelect());
                    joinGroupApplyAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void initData() {
        super.initData();

    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.groupApplyList(targetId);
    }

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

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

    @Override
    public void onClick(View v) {
        if (v.getId() == com.cooleshow.base.R.id.tv_right_text) {
            if (viewBinding.toolbarInclude.tvRightText.getText().toString().equals("批量操作")) {
                viewBinding.toolbarInclude.tvRightText.setText("完成");
                joinGroupApplyAdapter.setSelect(true);
                ll_bottom.setVisibility(View.VISIBLE);
            } else {
                viewBinding.toolbarInclude.tvRightText.setText("批量操作");
                joinGroupApplyAdapter.setSelect(false);
                ll_bottom.setVisibility(View.GONE);
            }
            joinGroupApplyAdapter.notifyDataSetChanged();
        } else if (v.getId() == R.id.tv_refuse) {
            //拒绝
            String auditIds = "";
            for (GroupApplyBean groupApplyBean : dataList) {
                if (groupApplyBean.isSelect()) {
                    auditIds = auditIds + groupApplyBean.getId() + ",";
                }
            }
            if (!TextUtils.isEmpty(auditIds)) {
                auditIds = auditIds.substring(0, auditIds.length() - 1);
                presenter.doGroupApply(auditIds, "REJECT", targetId);
                updateNormalStyle();
            }

        } else if (v.getId() == R.id.tv_pass) {
            //同意
            String auditIds = "";
            for (GroupApplyBean groupApplyBean : dataList) {
                if (groupApplyBean.isSelect()) {
                    auditIds = auditIds + groupApplyBean.getId() + ",";
                }
            }
            if (!TextUtils.isEmpty(auditIds)) {
                auditIds = auditIds.substring(0, auditIds.length() - 1);
                presenter.doGroupApply(auditIds, "OPEN", targetId);
                updateNormalStyle();
            }
        }
    }

    private void updateNormalStyle() {
        viewBinding.toolbarInclude.tvRightText.setText("批量操作");
        joinGroupApplyAdapter.setSelect(false);
        ll_bottom.setVisibility(View.GONE);
    }

    @Override
    public void groupApplyListSuccess(List<GroupApplyBean> data) {
        dataList.clear();
        dataList.addAll(data);
        joinGroupApplyAdapter.notifyDataSetChanged();
    }

    @Override
    public void doGroupApplySuccess() {
        presenter.groupApplyList(targetId);
    }
}
