package com.bc_chat.contacts.ui.friend

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.UserManagerContract
import com.bc_chat.contacts.databinding.ActivityMoreSettingBinding
import com.bc_chat.contacts.presenter.UserManagerPresenter
import com.bc_chat.contacts.view.ReportDialogFragment
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/7
 * email：526309416@qq.com
 * desc：更多设置
 */

@Route(path = RouteConfig.MORE_SETTING_ACTIVITY)
class MoreSettingActivity : BaseActivity<UserManagerPresenter>(), UserManagerContract.View {

    @Autowired
    @JvmField
    var is_report: Int? = 0

    @Autowired(name = "is_block")
    @JvmField
    var is_black: Int? = 0

    @Autowired(name = "userId")
    @JvmField
    var userId: String? = ""

    private val binding by lazy {
        rootDataBinding as ActivityMoreSettingBinding
    }

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

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

    override fun initialize() {
        ARouter.getInstance().inject(this)
        binding.cbBlack.isChecked = (is_black == 1)
        binding.cbBlack.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
//                CustomDialog.Builder(this).setTitle("加入黑名单").setMessage("确定要将该用户加入黑名单吗?")
//                        .setNegativeButton("取消") { dialog, which ->
//                            binding.cbBlack.isChecked = false
//                        }.setPositiveButton("确定") { dialog, which ->
//                        }.create().show()
                presenter.blackSetting(userId, 1)
            } else {
                presenter.blackSetting(userId, 0)
            }
        }
        binding.teReport.setOnClickListener {
            val reportDialogFragment = ReportDialogFragment()
            reportDialogFragment.setOnItemSelectListener {
                report(it)
            }
            reportDialogFragment.show(fragmentManager)
        }
        binding.tvDeleteFriend.setOnClickListener {
            CustomDialog.Builder(this)
                    .setTitle("删除好友")
                    .setMessage("您确定要删除该好友?")
                    .setNegativeButton("取消", null)
                    .setPositiveButton("确定") { dialog, which ->
                        presenter.deleteFriend(userId)
                    }
                    .create().show()
        }
    }

    private fun report(reason: String) {
        presenter.report(userId, reason)
    }

    override fun blackSettingfSuccess(isBlack: Int) {
        ToastUtils.show("设置成功")
        is_black = isBlack
        binding.cbBlack.isChecked = (is_black == 1)
        ObserverManager.getInstance().notifyObserver(Const.ObserverKey.BLACK_SETTING, userId)
    }

    override fun blackSettingFailure(exception: Exception) {

    }

    override fun reportSuccess() {
        ToastUtils.show("举报成功")
    }

    override fun reportFailure(exception: Exception) {
    }

    override fun deleteFriendSuccess() {
        ToastUtils.show("移除成功")
        RongIM.getInstance().clearMessages(Conversation.ConversationType.PRIVATE, userId, object : RongIMClient.ResultCallback<Boolean>() {
            override fun onSuccess(aBoolean: Boolean?) {
                RongIM.getInstance().removeConversation(Conversation.ConversationType.PRIVATE, userId, null)
            }

            override fun onError(e: RongIMClient.ErrorCode) {
            }
        })
        ObserverManager.getInstance().notifyObserver(Const.ObserverKey.DELETE_FRIEND_LIST, userId)
        ObserverManager.getInstance().notifyObserver(Const.ObserverKey.UPDATE_FRIEND_LIST, userId)
        finish()
    }

    override fun deleteFriendFailure(exception: Exception) {
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        if (requestCode == 241) {
            val reportContent = data?.getStringExtra("report_content")
            report(reportContent!!)
        }
    }
}