package com.bc_chat.contacts.ui.friend

import android.app.Activity
import android.text.InputFilter
import android.text.TextUtils
import android.view.Menu
import android.view.MenuItem
import com.alibaba.android.arouter.facade.annotation.Autowired
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.bc_base.utils.Const
import com.bc_chat.contacts.R
import com.bc_chat.contacts.databinding.ActivityUserRemarkBinding
import com.bc_chat.contacts.task.SetRemarkTask
import com.google.gson.JsonObject
import com.zhaohaoting.framework.abs.BaseActivity
import com.zhaohaoting.framework.abs.contract.BaseContract
import com.zhaohaoting.framework.requset.NetWorkCallBack
import com.zhaohaoting.framework.utils.RealNameInputFilter
import com.zhaohaoting.framework.utils.SimpleTextWatcher
import com.zhaohaoting.framework.utils.ToastUtils
import com.zhaohaoting.framework.utils.observer.ObserverManager
import io.rong.imkit.RongIM
import io.rong.imkit.userInfoCache.RongUserInfoManager

/**
 * Created by zhaoting on 2019/3/2.
 * Desc：备注
 */
@Route(path = RouteConfig.USER_REMARK_ACTIVITY)
class UserRemarkActivity : BaseActivity<BaseContract.BasePresenter>() {

    @Autowired
    @JvmField
    var userId: String? = null

    @Autowired
    @JvmField
    var remark: String? = ""

    private val binding by lazy {
        rootDataBinding as ActivityUserRemarkBinding
    }

    override fun initialize() {
        ARouter.getInstance().inject(this)
        val nameInputFilter = RealNameInputFilter()
        val lengthFilter = InputFilter.LengthFilter(15)
        binding.etRemark.filters = arrayOf(lengthFilter, nameInputFilter)
        binding.etRemark.addTextChangedListener(object : SimpleTextWatcher() {
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                invalidateOptionsMenu()
            }
        })
        binding.etRemark.setText(remark)
    }

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


    override fun initPresenter(): BaseContract.BasePresenter? = null

    override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
        super.onPrepareOptionsMenu(menu)
        menu?.clear()
        val content = binding.etRemark.text.toString()
        if (this.remark==null) this.remark = ""
        if (!TextUtils.isEmpty(content) && !remark.equals(content)) {
            menu?.add(100, 101, 0, "提交")?.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
        }
        return super.onPrepareOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        commit()
        return super.onOptionsItemSelected(item)
    }

    fun commit() {
        val content = binding.etRemark.text.toString()
        taskHelper.execute<Any>(SetRemarkTask(userId!!, content), object : NetWorkCallBack<JsonObject>() {
            override fun onPostExecuteSuccess(task: Any?, data: JsonObject?) {
                ToastUtils.show("修改成功")
                val userInfo = RongUserInfoManager.getInstance().getUserInfo(userId)
                if (userInfo != null) {
                    userInfo.name = content
                    RongIM.getInstance().refreshUserInfoCache(userInfo)
                }
                ObserverManager.getInstance().notifyObserver(Const.ObserverKey.UPDATE_FRIEND_LIST, userId)
                ObserverManager.getInstance().notifyObserver(Const.ObserverKey.UPDATE_FRIEND_INFO, userId)
                setResult(Activity.RESULT_OK)
                finish()
            }
        })
    }
}