package com.bc_chat.contacts.ui.group

import android.app.Activity
import android.content.Intent
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.entity.dao.GroupMemberDao
import com.bc_chat.bc_base.entity.group.Group
import com.bc_chat.bc_base.utils.Const
import com.bc_chat.contacts.R
import com.bc_chat.contacts.contract.SetGroupInfoContract
import com.bc_chat.contacts.databinding.ActivityGroupNameBinding
import com.bc_chat.contacts.presenter.SetGroupInfoPresenter
import com.zhaohaoting.framework.abs.BaseActivity
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

/**
 * Created by zhaoting on 2019/3/2.
 * Desc：群名称
 */
@Route(path = RouteConfig.GROUP_NAME_ACTIVITY)
class GroupNameActivity : BaseActivity<SetGroupInfoPresenter>(), SetGroupInfoContract.View {

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

    @Autowired
    @JvmField
    var groupId: String? = null

    @Autowired
    @JvmField
    var ids: String? = null

    @Autowired
    @JvmField
    var userName: String? = null

    private val binding by lazy {
        rootDataBinding as ActivityGroupNameBinding
    }

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

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


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

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

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        val content = binding.etGroupName.text.toString()
        if (content.isNullOrEmpty()) {
            ToastUtils.show("请输入内容")
            return true
        }
        presenter.setGroupInfo(groupId!!, ids!!, userName!!, group_name = content)
        return super.onOptionsItemSelected(item)
    }

    override fun setGroupInfoSuccess() {
        ToastUtils.show("修改成功")
        ObserverManager.getInstance().notifyObserver(Const.ObserverKey.REFRESH_GROUP_INFO, groupId)
        GroupMemberDao.instance.updateGroupName(groupId!!, binding.etGroupName.text.toString())
        setResult(Activity.RESULT_OK, Intent().putExtra("content", binding.etGroupName.text.toString()))
        finish()
    }

    override fun setGroupInfoFailure(exception: Exception) {
    }

    override fun groupinfoSuccess(groupInfo: Group) {
    }

    override fun groupinfoFailure(exception: Exception) {
    }

    override fun exitGroupSuccess() {
    }

    override fun exitGroupFailure(exception: Exception) {
    }

    override fun uploadSuccess(paths: MutableList<String>?) {
    }

    override fun uploadFailure(e: java.lang.Exception?) {
    }
}