package com.bc_chat.contacts.presenter

import com.bc_chat.contacts.contract.ChooseMemberContact
import com.bc_chat.contacts.task.AddGroupAdminTask
import com.bc_chat.contacts.task.AddGroupMemberTask
import com.bc_chat.contacts.task.AssignmentTask
import com.bc_chat.contacts.task.KickoutGroupMemberTask
import com.google.gson.JsonObject
import com.zhaohaoting.framework.abs.presenter.BasePresenter
import com.zhaohaoting.framework.requset.NetWorkCallBack
import java.lang.Exception

/**
 * author：Created by zhaoaoting on 2019/3/6
 * email：526309416@qq.com
 * desc：
 */
open class ChooseMemberPresenter<VIEW : ChooseMemberContact.View>(view: VIEW) : BasePresenter<VIEW>(view), ChooseMemberContact.Presenter {

    override fun addGroupMember(groupId: String, groupName: String, userNames: String, userName: String, uids: String) {

        taskHelper.execute<Any>(AddGroupMemberTask(groupId, groupName, userNames, userName, uids), object : NetWorkCallBack<JsonObject>() {
            override fun onPreExecute(task: Any?) {
                super.onPreExecute(task)
                view.showLoading(task)
            }

            override fun onPostExecuteSuccess(task: Any?, data: JsonObject?) {
                view.dismissLoading(task)
                view.addGroupMemberSuccess()

            }

            override fun onPostExecuteFailure(task: Any?, exception: Exception) {
                super.onPostExecuteFailure(task, exception)
                view.dismissLoading(task)
                view.addGroupMemberFailure(exception)
            }
        })
    }

    /**
     * 踢人
     */
    override fun kickoutGroupMember(groupId: String, userName: String?, userNames: String?, uids: String) {
        taskHelper.execute<Any>(KickoutGroupMemberTask(groupId, userName, userNames, uids), object : NetWorkCallBack<JsonObject>() {
            override fun onPreExecute(task: Any?) {
                super.onPreExecute(task)
                view.showLoading(task)
            }

            override fun onPostExecuteSuccess(task: Any?, data: JsonObject?) {
                view.dismissLoading(task)
                view.kickoutGroupMemberSuccess()
            }

            override fun onPostExecuteFailure(task: Any?, exception: Exception) {
                super.onPostExecuteFailure(task, exception)
                view.dismissLoading(task)
                view.kickoutGroupMemberFailure(exception)
            }
        })
    }

    /**
     * 转让群
     */
    override fun assignmentGroup(groupId: String, uid: String,nickname:String) {
        taskHelper.execute<Any>(AssignmentTask(uid, groupId,nickname), object : NetWorkCallBack<JsonObject>() {
            override fun onPreExecute(task: Any?) {
                super.onPreExecute(task)
                view.showLoading(task)
            }

            override fun onPostExecuteSuccess(task: Any?, data: JsonObject?) {
                view.dismissLoading(task)
                view.assignmentGroupSuccess()
            }

            override fun onPostExecuteFailure(task: Any?, exception: Exception) {
                super.onPostExecuteFailure(task, exception)
                view.dismissLoading(task)
                view.assignmentGroupFailure(exception)
            }
        })
    }

    /**
     * 添加群管理
     */
    override fun addGroupAdmin(groupId: String, nicknames: String, nickname: String, uids: String) {
        taskHelper.execute<Any>(AddGroupAdminTask(groupId, nicknames, nickname, uids), object : NetWorkCallBack<JsonObject>() {
            override fun onPreExecute(task: Any?) {
                super.onPreExecute(task)
                view.showLoading(task)
            }

            override fun onPostExecuteSuccess(task: Any?, data: JsonObject?) {
                view.dismissLoading(task)
                view.addGroupAdminSuccess()
            }

            override fun onPostExecuteFailure(task: Any?, exception: Exception) {
                super.onPostExecuteFailure(task, exception)
                view.dismissLoading(task)
                view.addGroupAdminFailure(exception)
            }
        })
    }
}
