package com.bc_chat.contacts.ui.group

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
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.contract.GroupManagerContract
import com.bc_chat.contacts.databinding.ActivityGroupManagerBinding
import com.bc_chat.contacts.presenter.GroupManagerPresenter
import com.zhaohaoting.framework.abs.BaseActivity
import com.zhaohaoting.framework.ui.dialog.CustomDialog
import com.zhaohaoting.framework.utils.ToastUtils
import com.zhaohaoting.framework.utils.observer.ObserverManager
import io.rong.imkit.RongIM
import io.rong.imlib.RongIMClient
import io.rong.imlib.model.Conversation

/**
 * author：Created by zhaohaoting on 2019/3/9
 * email：526309416@qq.com
 * desc：
 */
@Route(path = RouteConfig.GROUP_MANAGER_ACTIVITY)
class GroupManagerActivity : BaseActivity<GroupManagerPresenter>(), GroupManagerContract.View {

    companion object {
        const val REQUEST_CODE_SET_ADMIN = 2341
        const val REQUEST_CODE_ASSIGNMENT = 2342
        const val REQUEST_CODE_KICK = 2343
    }

    private val binding by lazy {
        rootDataBinding as ActivityGroupManagerBinding
    }

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

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

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

    @JvmField
    @Autowired
    var ordNoticeTop: String? = "0"

    @JvmField
    @Autowired
    var forbidden: String? = "0"

    override fun initPresenter(): GroupManagerPresenter = GroupManagerPresenter(this)

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

    override fun initialize() {
        ARouter.getInstance().inject(this)
        binding.tvAdmin.setOnClickListener {
            ARouter.getInstance().build(RouteConfig.SET_GROUP_ADMIN_ACTIVITY)
                    .withString("action", Const.Action.SET_GROUP_ADMIN)
                    .withString("groupId", groupId)
                    .withString("userName", userName)
                    .withString("groupName", groupName)
                    .navigation(this, REQUEST_CODE_SET_ADMIN)
        }
        binding.tvKick.setOnClickListener {
            ARouter.getInstance().build(RouteConfig.CHOOSE_FRIEND_ACTIVITY)
                    .withString("action", Const.Action.KICK_GROUP_MEMBER)
                    .withString("groupId", groupId)
                    .withString("userName", userName)
                    .withString("groupName", groupName)
                    .navigation(this, REQUEST_CODE_KICK)
        }
        binding.tvAssignment.setOnClickListener {
            ARouter.getInstance().build(RouteConfig.CHOOSE_FRIEND_ACTIVITY)
                    .withString("action", Const.Action.GROUP_ASSIGNMENT)
                    .withString("groupId", groupId)
                    .withString("userName", userName)
                    .withString("groupName", groupName)
                    .navigation(this, REQUEST_CODE_ASSIGNMENT)
        }
        binding.tvDissolve.setOnClickListener {
            CustomDialog.Builder(this)
                    .setTitle("解散群")
                    .setMessage("群解散后无法恢复，确定要解散该群吗？")
                    .setNegativeButton("取消", null)
                    .setPositiveButton("确定") { dialog, which ->
                        presenter.dissolutionGroup(groupId!!, userName)
                    }.create().show()
        }
        binding.cbMsgTop.isChecked = "1".equals(ordNoticeTop)
        binding.cbMsgTop.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
//                0-取消置顶 1-置顶
                presenter.lordNoticeTop(groupId!!, 1)
            } else {
                presenter.lordNoticeTop(groupId!!, 0)
            }
        }
        binding.cbMsgForbidden.isChecked = "1".equals(forbidden)
        binding.cbMsgForbidden.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
//                0-取消禁言 1-禁言
                presenter.forbidden(groupId!!, 1)
            } else {
                presenter.forbidden(groupId!!, 0)
            }
        }
    }

    @SuppressLint("WrongConstant")
    override fun dissolutionGroupSuccess() {
        ToastUtils.show("解散成功")
        RongIM.getInstance().clearMessages(Conversation.ConversationType.GROUP, groupId, object : RongIMClient.ResultCallback<Boolean>() {
            override fun onSuccess(aBoolean: Boolean?) {
                RongIM.getInstance().removeConversation(Conversation.ConversationType.GROUP, groupId, null)
            }

            override fun onError(e: RongIMClient.ErrorCode) {

            }
        })
//        ObserverManager.getInstance().notifyObserver(Const.ObserverKey.DISSOLUTION_GROUP, groupId)
        ARouter.getInstance().build(RouteConfig.MAIN_ACTIVITY)
                .withString("action", Const.Action.DISSOLUTION_GROUP)
                .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
                .navigation()
    }

    override fun dissolutionGroupFailure(exception: Exception) {
    }

    override fun forbiddenSuccess(is_banned: Int?) {
        ToastUtils.show("设置成功")
        ObserverManager.getInstance().notifyObserver(Const.ObserverKey.REFRESH_GROUP_INFO, groupId)
    }

    override fun forbiddenFailure(exception: Exception) {
        val checked = binding.cbMsgForbidden.isChecked
        binding.cbMsgForbidden.isChecked = !checked
    }

    override fun lordNoticeTopSuccess(isTop: Int) {
        ToastUtils.show("设置成功")
        ObserverManager.getInstance().notifyObserver(Const.ObserverKey.REFRESH_GROUP_INFO, groupId)
    }

    override fun lordNoticeTopFailure(exception: Exception) {
        val checked = binding.cbMsgTop.isChecked
        binding.cbMsgTop.isChecked = !checked
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        when (requestCode) {
            REQUEST_CODE_KICK -> ObserverManager.getInstance().notifyObserver(Const.ObserverKey.REFRESH_GROUP_MEMBER, groupId)
            REQUEST_CODE_ASSIGNMENT -> {
                ObserverManager.getInstance().notifyObserver(Const.ObserverKey.REFRESH_GROUP_MEMBER, groupId)
                finish()
            }
            REQUEST_CODE_SET_ADMIN -> ObserverManager.getInstance().notifyObserver(Const.ObserverKey.REFRESH_GROUP_MEMBER, groupId)
        }
    }
}