package com.bc_chat.circle.presenter

import com.bc_chat.circle.contract.TopicDetailsContract
import com.bc_chat.contacts.task.CommentTask
import com.bc_chat.contacts.task.DeleteCommentTask
import com.bc_chat.contacts.task.LikeTask
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 zhaohaoting on 2019/8/29
 * email：526309416@qq.com
 * desc：
 */
class TopicDetailsPresenter(view: TopicDetailsContract.View) : BasePresenter<TopicDetailsContract.View>(view), TopicDetailsContract.Presenter {

    override fun comment(cid: String, cuid: String, pid: String, content: String, type: Int) {
        taskHelper.execute<Any>(CommentTask(cid, cuid, pid, content, type), 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.commentSuccess()
            }

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

    override fun like(cuid: String, type: Int) {

        taskHelper.execute<Any>(LikeTask(cuid, type), 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.likeSuccess(type)
            }

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

    override fun delete(comment_id: String, position: Int) {
        taskHelper.execute<Any>(DeleteCommentTask(comment_id), 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.deleteSuccess(position)
            }

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