package com.zj365.mime.vm

import androidx.lifecycle.MutableLiveData
import com.xty.base.vm.BaseVm
import com.xty.common.Const
import com.xty.network.model.FamilyBean
import com.xty.network.model.InviteCustodyBean
import com.xty.network.model.MainBean
import com.xty.network.model.ProgrammeBean
import com.xty.network.model.RespBody
import com.xty.network.model.ScoreBean
import com.xty.network.model.SettingBean
import com.xty.network.model.UnReadMessageBean
import com.xty.network.model.Update
import com.xty.network.model.UserEquipmentInformationizationBean
import com.xty.network.model.UserOtherInfo
import org.json.JSONObject

class SettingVm : BaseVm() {
    val unbindLive by lazy { MutableLiveData<RespBody<String>>() }
    val destLive by lazy { MutableLiveData<RespBody<Any>>() }
    val signLive by lazy { MutableLiveData<RespBody<Any>>() }
    val countLive by lazy { MutableLiveData<RespBody<UnReadMessageBean>>() }
    val settingLive by lazy { MutableLiveData<RespBody<SettingBean>>() }
    val scoreLive by lazy { MutableLiveData<RespBody<ScoreBean>>() }
    val progLive by lazy { MutableLiveData<RespBody<MutableList<ProgrammeBean>>>() }
    val familyLive by lazy { MutableLiveData<RespBody<MutableList<FamilyBean>>>() }
    val deviceInfo by lazy { MutableLiveData<RespBody<UserEquipmentInformationizationBean>>() }
    val inviteCountLive by lazy { MutableLiveData<RespBody<String>>() }

    val updateLive by lazy { MutableLiveData<RespBody<Update>>() }

    val allData by lazy { MutableLiveData<RespBody<MainBean>>() }
    val findInfoLiveData by lazy { MutableLiveData<RespBody<InviteCustodyBean>>() }

    val confirmInviteLiveData by lazy { MutableLiveData<RespBody<Any>>() }

    val useOtherInfoLiveData by lazy { MutableLiveData<RespBody<UserOtherInfo>>() }


    fun getNesProg(id: String) {
        startHttp {
            val json = JSONObject()
            json.put("type", Const.PROGRAMME_2)
            json.put("id", id)
            val response = apiInterface().getProg(
                retrofits.getRequestBody(
                    json.toString()
                )
            )
            response.getCodeStatus(progLive, nowData)
        }
    }

    fun getScore(pageNum: Int, pageSize: Int = 20) {
        startHttp {
            val json = JSONObject()
            json.put("pageSize", pageSize)
            json.put("pageNum", pageNum)
            val body = apiInterface().getScore(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(scoreLive, nowData)
        }
    }

    fun logOut() {
        startHttp {
            apiInterface().logOut()
        }
    }

    /**
     * 个人设置接口
     */
    fun getMySetting() {
        startHttp {
            val body = apiInterface().getMySetting()
            body.getCodeStatus(settingLive, nowData)
        }
    }

    //设备解绑
    fun unBind() {
        startHttp {
            val response = apiInterface().unBindDev()
            response.getCodeStatus(unbindLive, nowData)
        }
    }

    //签到
    fun sign() {
        startHttp {
            val response = apiInterface().sign()
            response.getCodeStatus(signLive, nowData)
        }
    }

    fun getNotReadCount() {
        startHttp(false) {
            val respnse = apiInterface().getNotReadCount()
            respnse.getCodeStatus(countLive, nowData)
        }
    }

    fun getMyFamilyList() {
        startHttp(false) {
            val json = JSONObject()
            /* json.put("pageNum","1")
             json.put("pageSize",Const.PAGE_SIZE)*/
            val request = apiInterface().getMyFamily(
                retrofits.getRequestBody(
                    json.toString()
                )
            )
            request.getCodeStatus(familyLive, nowData)
        }
    }

    //请求用户设备信息
    fun getUserDeviceInfo(userId: String) {
        startHttp(false) {
            val json = JSONObject()
            json.put("id", userId)
            val response = apiInterface().getUserEquipmentInformationization(
                retrofits.getRequestBody(
                    json.toString()
                )
            )
            response.getCodeStatus(deviceInfo, nowData)
        }
    }

    //统计用户邀请个数
    fun getInviteCountRequest(id: String) {
        startHttp {
            val json = JSONObject()
            json.put("id", id)
            val inviteCount = apiInterface().getInviteCount(
                retrofits.getRequestBody(
                    json.toString()
                )
            )
            inviteCount.getCodeStatus(inviteCountLive, nowData)
        }
    }

    fun destAccount(id:String){
        startHttp {
            var response= apiInterface().destAccount(id)
            response.getCodeStatus(destLive,nowData)
        }
    }

    fun getVersionNew(versionCode:Int) {
        startHttp {
            var response = apiInterface().getVersionNew(versionCode)
            response.getCodeStatus(updateLive, nowData)
        }
    }

    /**
     * 获取所有信息
     */
    fun getAllInfo(id:String,isSHowDialog:Boolean) {
        startHttp(isSHowDialog) {
            var json=JSONObject()
            json.put("id",id)
            var body = apiInterface().getMainInfo(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(allData,nowData)
        }
    }

    fun searchInviteInfo(){
        startHttp(false) {
            var json = JSONObject()
            val response = apiInterface().findConfirmInfo(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(findInfoLiveData, nowData)
        }
    }

    fun confirmInvite(id:String, status:String){
        startHttp(false) {
            var json = JSONObject()
            json.put("id",id)
            json.put("status",status)
            val response = apiInterface().confirmInvited(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(confirmInviteLiveData,nowData)
        }

    }

    fun getUserOtherInfo(){
        startHttp {
            val response = apiInterface().getUserOtherInfo()
            response.getCodeStatus(useOtherInfoLiveData,nowData)
        }
    }
}