package com.bc_chat.contacts.ui.friend

import android.app.Activity
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.TipsMessage
import com.bc_chat.bc_base.config.RouteConfig
import com.bc_chat.bc_base.entity.User
import com.bc_chat.contacts.R
import com.bc_chat.contacts.contract.AddFriendContract
import com.bc_chat.contacts.databinding.ActivityAddFriendBinding
import com.bc_chat.contacts.presenter.AddFriendPresenter
import com.zhaohaoting.framework.abs.BaseActivity
import com.zhaohaoting.framework.imagepicker.camera.util.LogUtil
import com.zhaohaoting.framework.utils.RealNameInputFilter
import com.zhaohaoting.framework.utils.glid.GlideUtils
import io.rong.imkit.RongIM
import io.rong.imlib.IRongCallback
import io.rong.imlib.RongIMClient
import io.rong.imlib.model.Conversation
import io.rong.imlib.model.Message

/**
 * author：Created by zhaohaoting on 2019/3/7
 * email：526309416@qq.com
 * desc：
 */
@Route(path = RouteConfig.ADD_FRIEND_ACTIVITY)
class AddFriendActivity : BaseActivity<AddFriendPresenter<*>>(), AddFriendContract.View {

    private val binding by lazy {
        rootDataBinding as ActivityAddFriendBinding
    }

    private val userInfo by lazy {
        intent.getParcelableExtra<User>("userInfo")
    }

    override fun initPresenter(): AddFriendPresenter<*> = AddFriendPresenter(this)

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

    override fun initialize() {
        GlideUtils.loadCircleImage(this, userInfo.photo, binding.ivHead)
        binding.etNickName.setText(userInfo.nickname)
        val nameInputFilter = RealNameInputFilter()
        val lengthFilter = InputFilter.LengthFilter(15)
        binding.etNickName.filters = arrayOf(lengthFilter, nameInputFilter)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menu?.add("完成")!!.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        presenter.addFriend(userInfo.userId, binding.etNickName.text.toString().trim())
        return true
    }

    override fun addFriendSuccess(cuid: String) {
        ARouter.getInstance().build(RouteConfig.USER_INFO_DETAILS_ACTIVITY).withString("userId", userInfo.userId).withString("userName", userInfo.remark
                ?: userInfo.nickname)
                .navigation()
        setResult(Activity.RESULT_OK)
        finish()
    }

    override fun addFriendFailure(exception: Exception) {
        val tipsMessage: TipsMessage = TipsMessage.obtain(userInfo.userId, "1", "")
        RongIM.getInstance().sendMessage(Message.obtain(userInfo.userId, Conversation.ConversationType.PUSH_SERVICE, tipsMessage),
                "添加好友", null, object : IRongCallback.ISendMessageCallback {
            override fun onAttached(message: Message) {
                LogUtil.d(message.toString())
            }

            override fun onSuccess(message: Message) {
                LogUtil.d(message.toString())
            }

            override fun onError(message: Message, errorCode: RongIMClient.ErrorCode) {
                LogUtil.d(message.toString())
            }
        })
        finish()
    }
}
