package com.bc_chat.contacts.ui.friend

import android.text.InputFilter
import android.view.Menu
import android.view.MenuItem
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bc_chat.bc_base.config.RouteConfig
import com.bc_chat.contacts.R
import com.bc_chat.contacts.contract.PhoneAddFriendContract
import com.bc_chat.contacts.databinding.ActivityPhoneSearchFriendBinding
import com.bc_chat.contacts.presenter.PhoneAddFriendPresenter
import com.zhaohaoting.framework.abs.BaseActivity
import com.zhaohaoting.framework.utils.RealNameInputFilter
import com.zhaohaoting.framework.utils.ToastUtils
import io.rong.imkit.userInfoCache.RongUserInfoManager
import io.rong.imlib.RongIMClient

/**
 * author：Created by zhaohaoting on 2019/3/7
 * email：526309416@qq.com
 * desc：电话号码 添加联系人
 */
@Route(path = RouteConfig.PHONE_SEARCH_FRIEND_ACTIVITY)
class PhoneSearchFriendActivity : BaseActivity<PhoneAddFriendPresenter>(), PhoneAddFriendContract.View {

    private val binding by lazy {
        rootDataBinding as ActivityPhoneSearchFriendBinding
    }
    var phone: String? = null
    var remark: String? = null

    override fun initPresenter(): PhoneAddFriendPresenter? = PhoneAddFriendPresenter(this)

    override fun getLayoutResId(): Int = R.layout.activity_phone_search_friend

    override fun initialize() {
        val nameInputFilter = RealNameInputFilter()
        val lengthFilter = InputFilter.LengthFilter(15)
        binding.erRemark.filters = arrayOf(lengthFilter, nameInputFilter)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menu?.add("确定")?.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        phone = binding.erPhone.text.toString().trim()
//        remark = RongUserInfoManager.getInstance().getUserInfo(RongIMClient.getInstance().currentUserId).name
//        remark = binding.erRemark.text.toString().trim()
//        if (remark.isNullOrEmpty()) {
//            ToastUtils.show("请输入姓名")
//            return true
//        }
        if (phone.isNullOrEmpty()) {
            ToastUtils.show("请输入账号")
            return true
        }
        presenter.searchPhone(phone!!)
//        ARouter.getInstance().build(RouteConfig.PHONE_ADD_FRIEND_ACTIVITY).withString("phone", phone).withString("remark", remark).navigation()
        return true
    }

    override fun searchSuccess(userId: String) {
        presenter.addFriend(userId, phone!!)
    }

    override fun searchNotMember() {
        ARouter.getInstance().build(RouteConfig.PHONE_INVITATION_FRIEND_ACTIVITY).withString("remark", phone)
                .withString("phone", phone).navigation(this)
        finish()
    }

    override fun searchFailure(exception: Exception) {
    }

    override fun addFriendSuccess(cuid: String) {
        ARouter.getInstance().build(RouteConfig.USER_INFO_DETAILS_ACTIVITY).withString("userId", cuid).navigation()
        finish()
    }

    override fun addFriendFailure(exception: Exception) {
    }
}
