package io.rong.contactcard.choose;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bc_chat.bc_base.entity.UserInfo;
import com.zhaohaoting.framework.abs.BaseActivity;
import com.zhaohaoting.framework.abs.contract.BaseContract;

import java.util.ArrayList;

import io.rong.contactcard.R;
import io.rong.contactcard.databinding.ContactActivityChooseFriendBinding;

/**
 * author：Created by zhaohaoting on 2019/2/28
 * email：526309416@qq.com
 * desc：选择好友
 */
public class ChooseFriendActivity extends BaseActivity<BaseContract.BasePresenter> implements ChooseFriendFragment.OnSelectListener, TextWatcher {

    private ChooseFriendFragment chooseFriendFragment = null;
    private ArrayList<UserInfo> selectList = null;

    @Override
    public BaseContract.BasePresenter initPresenter() {
        return null;
    }

    @Override
    public void initialize() {
        ARouter.getInstance().inject(this);
        ContactActivityChooseFriendBinding binding = (ContactActivityChooseFriendBinding) rootDataBinding;
        chooseFriendFragment = new ChooseFriendFragment();
        chooseFriendFragment.setSelectListener(this);
        addFragment(chooseFriendFragment, R.id.fl_container);
        binding.etAutoSearch.addTextChangedListener(this);
    }

    @Override
    public void onItemSelect(UserInfo contacts, int size) {
        Intent intent = new Intent();
        io.rong.imlib.model.UserInfo userInfo = new io.rong.imlib.model.UserInfo(contacts.getUserId(), contacts.getNickname(), Uri.parse(contacts.getPhoto()));
        intent.putExtra("contact", userInfo);
        intent.putExtra("size", size);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @Override
    public void onItemSelectList(ArrayList<UserInfo> contactList) {
        selectList = contactList;
    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (chooseFriendFragment != null && chooseFriendFragment.isAdded()) {
            chooseFriendFragment.filter(s);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }


    @Override
    public int getLayoutResId() {
        return R.layout.contact_activity_choose_friend;
    }

}
