package com.bc_chat.contacts.ui.group

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.TextView
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.ListModel
import com.bc_chat.bc_base.entity.group.MemberBean
import com.bc_chat.bc_base.task.GroupFilterDataSource
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.adapter.GroupContactListAdapter
import com.bc_chat.contacts.task.DeleteAdminTask
import com.google.gson.JsonObject
import com.shizhefei.view.coolrefreshview.CoolRefreshView
import com.zhaohaoting.framework.abs.BaseRecyclerActivity
import com.zhaohaoting.framework.abs.adapter.AbsRecyclerAdapter
import com.zhaohaoting.framework.mvchelper.mvc.IAsyncDataSource
import com.zhaohaoting.framework.mvchelper.mvc.IDataAdapter
import com.zhaohaoting.framework.mvchelper.mvc.OnRefreshEndListener
import com.zhaohaoting.framework.requset.NetWorkCallBack
import com.zhaohaoting.framework.ui.dialog.CustomDialog
import com.zhaohaoting.framework.ui.helper.MVCCoolHelper
import com.zhaohaoting.framework.utils.observer.ObserverManager

/**
 * author：Created by zhaohaoting on 2019/3/11
 * email：526309416@qq.com
 * desc：设置管理员
 */
@Route(path = RouteConfig.SET_GROUP_ADMIN_ACTIVITY)
class SetGroupAdminActivity : BaseRecyclerActivity<MemberBean, ListModel<MemberBean>>(), OnRefreshEndListener<ListModel<MemberBean>> {

    companion object {
        const val REQUEST_CODE_SET_ADMIN = 2361
    }

    @Autowired
    @JvmField
    var groupId: String? = null

    @Autowired
    @JvmField
    var userName: String? = null

    @Autowired
    @JvmField
    var groupName: String? = null

    override fun onEndRefresh(adapter: IDataAdapter<ListModel<MemberBean>>?, result: ListModel<MemberBean>?) {
    }

    override fun onInit() {
        ARouter.getInstance().inject(this)
        setOnStateChangeListener(this)
    }

    override fun addHeadViews(adapter: AbsRecyclerAdapter<MemberBean, ListModel<MemberBean>>?) {
        val footerView = View.inflate(this, R.layout.set_admin_footer_view, null)
        val tvAddAdmin = footerView.findViewById<TextView>(R.id.tv_add_group_admin)
        adapter?.addFooter(footerView)
        tvAddAdmin.setOnClickListener {
            ARouter.getInstance().build(RouteConfig.CHOOSE_FRIEND_ACTIVITY)
                    .withString("action", Const.Action.SET_GROUP_ADMIN)
                    .withString("groupId", groupId)
                    .withString("userName", userName)
                    .withString("groupName", groupName)
                    .navigation(this, REQUEST_CODE_SET_ADMIN)
        }
    }

    override fun onItemLongClick(vh: AbsRecyclerAdapter.BaseViewHolder<*>?, position: Int) {
        val list = adapter.data.list
        val memberBean = list[position]
        if (memberBean.userId.equals(AppConfig.instance.getUser().userId)) return
        CustomDialog.Builder(this)
                .setTitle("移除管理员")
                .setMessage("确定要移除该管理员吗？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定") { dialog, which ->
                    taskHelper.execute<Any>(DeleteAdminTask(groupId!!, memberBean.userId!!,memberBean.nickname!!), object : NetWorkCallBack<JsonObject>() {
                        override fun onPostExecuteSuccess(task: Any?, data: JsonObject?) {
                            list.removeAt(position)
                            adapter.notifyItemRemoved(position)
                            adapter.notifyDataSetChanged()
                            ObserverManager.getInstance().notifyObserver(Const.ObserverKey.REFRESH_GROUP_MEMBER,groupId)
                        }
                    })
                }.create().show()

    }

    override fun getMvcHelper(coolRefreshView: CoolRefreshView?): MVCCoolHelper<ListModel<MemberBean>> {
        val mvcCoolHelper = MVCCoolHelper<ListModel<MemberBean>>(coolRefreshView)
        mvcCoolHelper.setmLoadMoreView(null)
        return mvcCoolHelper
    }

    override fun getAdapter(): AbsRecyclerAdapter<MemberBean, ListModel<MemberBean>>? = GroupContactListAdapter(this, GroupContactListAdapter.FROM_SET_GROUP_ADMIN)

    override fun getDataSource(): IAsyncDataSource<ListModel<MemberBean>>? = GroupFilterDataSource(groupId!!, 1)

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        refresh()
    }
}
