package com.bc_chat.bc_base.presenter

import com.bc_chat.bc_base.contract.MucRedPacketContract
import com.bc_chat.bc_base.entity.wallet.MucRedPacketDetailEntity
import com.bc_chat.bc_base.entity.wallet.OpenRedPacketEntity
import com.bc_chat.bc_base.task.MucRedPacketDetailTask
import com.bc_chat.bc_base.task.OpenRedPacketTask
import com.bc_chat.bc_base.task.RedPacketIsOpenTask
import com.google.gson.JsonObject
import com.zhaohaoting.framework.abs.presenter.BasePresenter
import com.zhaohaoting.framework.requset.NetWorkCallBack

/**
 * author：Created by zhaohaoting on 2019/7/19
 * email：526309416@qq.com
 * desc：
 */
class MucRedPacketPresenter<VIEW : MucRedPacketContract.View>(view: VIEW) : BasePresenter<VIEW>(view), MucRedPacketContract.Presenter {

    override fun openRedPacket(packet_id: String) {
        taskHelper.execute<Any>(OpenRedPacketTask(packet_id), object : NetWorkCallBack<OpenRedPacketEntity>() {
            override fun onPreExecute(task: Any?) {
                super.onPreExecute(task)
                view.showLoading(task)
            }

            override fun onPostExecuteSuccess(task: Any?, data: OpenRedPacketEntity?) {
                view.dismissLoading(task)
                redPacketDetail(packet_id)
            }

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

    override fun redPacketDetail(packet_id: String) {
        taskHelper.execute<Any>(MucRedPacketDetailTask(packet_id), object : NetWorkCallBack<MucRedPacketDetailEntity>() {
            override fun onPreExecute(task: Any?) {
                super.onPreExecute(task)
                view.showLoading(task)
            }

            override fun onPostExecuteSuccess(task: Any?, data: MucRedPacketDetailEntity?) {
                view.dismissLoading(task)
                view.redPacketDetailSuccess(data);
            }

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

//    override fun isOpenRedPacket(packet_id: String) {
//        taskHelper.execute<Any>(RedPacketIsOpenTask(packet_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)
//                redPacketDetail(packet_id);
//            }
//
//            override fun onPostExecuteFailure(task: Any?, exception: Exception?) {
//                view.dismissLoading(task)
//                if (exception?.message.equals("未领取过该红包")) {
//                    openRedPacket(packet_id);
//                }
//            }
//        })
//    }

}
