package com.bc_chat.bc_base.presenter

import android.arch.lifecycle.LifecycleOwner
import com.bc_chat.bc_base.contract.GroupInfoContract
import com.bc_chat.bc_base.contract.MainContract
import com.bc_chat.bc_base.contract.UpdateVersonContract
import com.bc_chat.bc_base.contract.UserInfoContract
import com.bc_chat.bc_base.entity.FriendApplyRecordBean
import com.bc_chat.bc_base.entity.NavEntity
import com.bc_chat.bc_base.entity.wallet.NoticeEntity
import com.bc_chat.bc_base.task.*
import com.bc_chat.bc_base.utils.Const
import com.google.gson.JsonObject
import com.zhaohaoting.framework.abs.presenter.BasePresenter
import com.zhaohaoting.framework.requset.NetWorkCallBack
import com.zhaohaoting.framework.utils.observer.ObserverManager

/**
 * author：Created by zhaohaoting on 2019/3/12
 * email：526309416@qq.com
 * desc：
 */
class MainPresenter constructor(view: MainContract.View) : BasePresenter<MainContract.View>(view), MainContract.Presenter {

    private var inviteName: String? = null

    override fun addGroupMember(groupId: String, groupName: String, userNames: String, uids: String) {
        taskHelper.execute<Any>(AddGroupMemberTask(groupId, groupName, userNames, inviteName, 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.addGroupSuccess(groupId, groupName)

            }

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

    override fun getUpdateVerson(isShowDialog: Boolean?) {
//        updateVersonPresenter?.getUpdateVerson(isShowDialog)
    }

    override fun isSetPayKey() {
        taskHelper.execute<Any>(PayPasswordIsSetTask(), object : NetWorkCallBack<JsonObject>() {
            override fun onPreExecute(task: Any?) {
                super.onPreExecute(task)
            }

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

            override fun onPostExecuteFailure(task: Any?, exception: Exception?) {
//                super.onPostExecuteFailure(task, exception)  //这个接口有问题，这里不能super，不然会弹窗
                view.isSetPayKeySuccess(false)
            }
        })
    }

    override fun getLastestNotice() {
        taskHelper.execute<Any>(NoticeTask(), object : NetWorkCallBack<NoticeEntity>() {
            override fun onPostExecuteSuccess(task: Any?, data: NoticeEntity?) {
                view.getLastestNoticeSuccess(data)
            }
        })
    }

    override fun getNavData() {
        taskHelper.execute<Any>(NavTask(), object : NetWorkCallBack<List<NavEntity>>() {
            override fun onPostExecuteSuccess(task: Any?, data: List<NavEntity>?) {
                view.getNavDataSuccess(data)
            }

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

    override fun applyFriendList(page_num: Int, page_size: Int) {
        taskHelper.execute<Any>(FriendApplyRecordTask(page_num, page_size), object : NetWorkCallBack<List<FriendApplyRecordBean>>() {
            override fun onPreExecute(task: Any?) {
                super.onPreExecute(task)
                view.showLoading(task)
            }

            override fun onPostExecuteSuccess(task: Any?, data: List<FriendApplyRecordBean>?) {
                view.dismissLoading(task)
                view.applyFriendListSuccess(data)
            }

        })
    }

    private var groupInfoPresenter: GroupInfoPresenter<GroupInfoContract.View>? = null
    private var userInfoPresenter: UserInfoPresenter<UserInfoContract.View>? = null
    private var updateVersonPresenter: UpdateVersonPresenter<UpdateVersonContract.View>? = null


    init {
        groupInfoPresenter = GroupInfoPresenter(view)
        userInfoPresenter = UserInfoPresenter(view)
        updateVersonPresenter = UpdateVersonPresenter(view)
    }

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

    override fun groupinfoTask(groupId: String, name: String?) {
        inviteName = name
        groupInfoPresenter?.groupinfoTask(groupId, false)
    }

    override fun userInfoTask(cuid: String) {
        userInfoPresenter?.userInfoTask(cuid)
    }

}
