package com.bc_chat.contacts.presenter

import android.arch.lifecycle.LifecycleOwner
import com.bc_chat.bc_base.presenter.GroupInfoPresenter
import com.bc_chat.bc_base.presenter.UpLoadPicturePresenter
import com.bc_chat.contacts.contract.SetGroupInfoContract
import com.bc_chat.contacts.task.ExitGroupTask
import com.bc_chat.contacts.task.SetGroupInfoTask
import com.google.gson.JsonObject
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 SetGroupInfoPresenter(view: SetGroupInfoContract.View) : GroupInfoPresenter<SetGroupInfoContract.View>(view), SetGroupInfoContract.Presenter {

    private var uploadPresenter: UpLoadPicturePresenter<SetGroupInfoContract.View>? = null

    init {
        uploadPresenter = UpLoadPicturePresenter(view)
    }

    override fun onCreate(owner: LifecycleOwner?) {
        super.onCreate(owner)
        owner?.lifecycle?.addObserver(uploadPresenter!!)
    }

    override fun setGroupInfo(groupId: String, uids: String, userName: String, group_name: String?, photo: String?, introduction: String?, notice: String?, type: String?) {
        taskHelper.execute<Any>(SetGroupInfoTask(groupId, uids, userName, group_name, photo, introduction, notice, type), object : NetWorkCallBack<JsonObject>() {

            override fun onPreExecute(task: Any?) {
                super.onPreExecute(task)
                view.showLoading(task)
            }

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

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

    /**
     * 退出
     */
    override fun exitGroup(groupId: String, userNames: String?, userName: String?, uids: String) {
        taskHelper.execute<Any>(ExitGroupTask(groupId, 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.exitGroupSuccess()
            }

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

    override fun upload(paths: List<String>) {
        uploadPresenter?.upload(paths)
    }
}
