package com.common.im.ui.contact;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

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

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.common.im.adapter.ContactListAdapter;
import com.common.im.adapter.ContactRoomListAdapter;
import com.common.im.bean.ContactListBean;
import com.common.im.bean.ContactRoomListBean;
import com.common.im.contract.ContactListFragmentContract;
import com.common.im.contract.ContactRoomListFragmentContract;
import com.common.im.helper.ConversationHelper;
import com.common.im.presenter.ContactListPresenter;
import com.common.im.presenter.ContactRoomListPresenter;
import com.common.im_ui.R;
import com.common.im_ui.databinding.FragmentContactListLayoutBinding;
import com.common.im_ui.databinding.FragmentContactRoomListBinding;
import com.cooleshow.base.ui.fragment.BaseMVPFragment;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.widgets.EmptyViewLayout;
import com.mcxtzhang.indexlib.suspension.SuspensionDecoration;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

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

import io.rong.imkit.RongIM;
import io.rong.imkit.config.ConversationListBehaviorListener;
import io.rong.imkit.config.RongConfigCenter;
import io.rong.imkit.userinfo.RongUserInfoManager;
import io.rong.imkit.utils.RouteUtils;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Group;

/**
 * 创建日期：2022/6/10 10:42
 *
 * @author Ryan
 * 类说明：
 */
public class ContactRoomListFragment extends BaseMVPFragment<FragmentContactRoomListBinding, ContactRoomListPresenter> implements ContactRoomListFragmentContract.ContactRoomListFragmentView, View.OnClickListener {

    private List<ContactRoomListBean> mDatas = new ArrayList<>();
    private ContactRoomListAdapter mListAdapter;
    private String searchCondition;
    private EmptyViewLayout mEmptyViewLayout;
    private boolean isSelectContact = false;//是否是选择联系人

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

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

    @Override
    protected void initView(View rootView) {
        if (getArguments() != null) {
            isSelectContact = getArguments().getBoolean(ContactListTabFragment.IS_SELECT_CONTACT, false);
        }
        mViewBinding.tvSearch.setOnClickListener(this);
        mListAdapter = new ContactRoomListAdapter(R.layout.item_contact_list_layout);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mViewBinding.recyclerView.setLayoutManager(layoutManager);
        mViewBinding.recyclerView.setAdapter(mListAdapter);
        mListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (isSelectContact) {
                    ContactRoomListBean item = (ContactRoomListBean) adapter.getItem(position);
                    //选择联系人模式
                    if (getActivity() != null) {
                        Intent intent = new Intent();
                        intent.putExtra(ConversationHelper.TARGET_ID_KEY, item.getId());
                        intent.putExtra(ConversationHelper.CONVERSATION_TYPE_KEY, Conversation.ConversationType.GROUP.getValue());
                        getActivity().setResult(Activity.RESULT_OK, intent);
                        getActivity().finish();
                    }
                    return;
                }
                ContactRoomListBean item = (ContactRoomListBean) adapter.getItem(position);
                goChat(item.getId() + "", item.getName());
            }
        });
        mViewBinding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                presenter.queryGroupList(getContext(), searchCondition);
            }
        });
    }

    private void goChat(String targetId, String nickName) {
        RongIM.getInstance().startGroupChat(getContext(), targetId, nickName);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.queryGroupList(getContext(), searchCondition);
    }

    @Override
    public void getGroupListSuccess(List<ContactRoomListBean> contactListBeans) {
        if (isDetached()) {
            return;
        }
        for (ContactRoomListBean contactListBean : contactListBeans) {
            Group group = RongUserInfoManager.getInstance().getGroupInfo(contactListBean.getId());
        }
        mViewBinding.refreshLayout.finishRefresh();
        if (mListAdapter != null) {
            if (contactListBeans != null && contactListBeans.size() > 0) {
                mDatas = contactListBeans;
                mListAdapter.setNewInstance(contactListBeans);
            } else {
                mListAdapter.getData().clear();
                mListAdapter.notifyDataSetChanged();
                showEmptyView();
            }
        }
    }

    @Override
    public void getGroupListError() {
        if (isDetached()) {
            return;
        }
        if (mListAdapter != null) {
            mListAdapter.getData().clear();
            mListAdapter.notifyDataSetChanged();
        }
        mViewBinding.refreshLayout.finishRefresh();
    }

    private void showEmptyView() {
        if (mEmptyViewLayout == null) {
            mEmptyViewLayout = new EmptyViewLayout(getContext());
        }
        mEmptyViewLayout.setContent(com.cooleshow.base.R.drawable.icon_empty_content, "暂无内容");
        mListAdapter.setEmptyView(mEmptyViewLayout);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_search) {
            String targetSearchStr = mViewBinding.etTargetName.getText().toString().trim();
//            if (TextUtils.isEmpty(targetSearchStr)) {
//                ToastUtil.getInstance().showShort("请输入群聊名称");
//                return;
//            }
            searchCondition = targetSearchStr;
            if (presenter != null) {
                presenter.queryGroupList(getContext(), searchCondition);
            }
        }
    }
}