package com.bc_chat.contacts.ui.group

import android.app.Activity
import android.content.Intent
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.TipsMessage
import com.bc_chat.bc_base.config.RouteConfig
import com.bc_chat.bc_base.entity.group.Group
import com.bc_chat.bc_base.utils.AppConfig
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.ActivityGroupNoticeBinding
import com.bc_chat.contacts.presenter.SetGroupInfoPresenter
import com.zhaohaoting.framework.abs.BaseActivity
import com.zhaohaoting.framework.imagepicker.camera.util.LogUtil
import com.zhaohaoting.framework.utils.Preferences
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.imlib.IRongCallback
import io.rong.imlib.RongIMClient
import io.rong.imlib.model.Conversation
import io.rong.imlib.model.Message

/**
 * Created by zhaoting on 2019/3/2.
 * Desc：群公告
 */
@Route(path = RouteConfig.GROUP_NOTICE_ACTIVITY)
class GroupNoticeActivity : BaseActivity<SetGroupInfoPresenter>(), SetGroupInfoContract.View {

    @Autowired
    @JvmField
    var type: String? = null

    @Autowired
    @JvmField
    var role: Int? = 3

    @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 ActivityGroupNoticeBinding
    }

    override fun initialize() {
        ARouter.getInstance().inject(this)
        when (type) {
            "notice" -> title = "群公告"
            "introduce" -> title = "群介绍"
        }
        binding.etGroupNotice.isEnabled = role != Const.GROUP_ROLE.ROLE_GROUP_ORDINARY
        binding.etGroupNotice.addTextChangedListener(object : SimpleTextWatcher() {
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                invalidateOptionsMenu()
            }
        })
        binding.etGroupNotice.setText(content)
    }

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


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

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

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

            "introduce" -> presenter.setGroupInfo(groupId!!, ids!!, userName!!, introduction = content)
        }
        return super.onOptionsItemSelected(item)
    }

    override fun setGroupInfoSuccess() {
        ToastUtils.show("修改成功")
        setResult(Activity.RESULT_OK, Intent().putExtra("content", binding.etGroupNotice.text.toString()))
        if (type.equals("notice")) {
            //修改群公告
            Preferences.setGroupNotice(groupId!!, binding.etGroupNotice.text.toString().trim())
            ObserverManager.getInstance().notifyObserver(Const.ObserverKey.GROUP_NOTICE_REFRESH, groupId)
        }
        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?) {
    }
}