package com.zj365.mime.vm

import androidx.lifecycle.MutableLiveData
import com.xty.base.vm.BaseVm
import com.xty.network.model.*
import okhttp3.MultipartBody
import org.json.JSONArray
import org.json.JSONObject
import java.io.File

class WearManagerVm : BaseVm() {

    val wearManagerLive by lazy { MutableLiveData<RespBody<WearManagerBean>>() }
    val settingLive by lazy { MutableLiveData<RespBody<Any>>() }
    val smsListLive by lazy { MutableLiveData<RespBody<SmsBean>>() }
    val smsSettingLive by lazy { MutableLiveData<RespBody<Any>>() }

    //佩戴设置列表
    fun getWearManagerList() {
        startHttp {
            var response = apiInterface().wearManagerList()
            response.getCodeStatus(wearManagerLive, nowData)
        }
    }

    /**
     * 短信开关列表
     */
    fun getSmsList(){
        startHttp {
            var response = apiInterface().smsList()
            response.getCodeStatus(smsListLive, nowData)
        }
    }


    /**
     * 短信开关设置
     *
     * @param path 签名文件地址
     * @param type 1:sms预警短信
     * @param state 1:打开 0：关闭
     *
     */
    fun smsSetting(path: String?, type: Int, state: Int){
        startHttp {
            if(path.isNullOrEmpty()){
                var body=  apiInterface().smsSetting(type, state)
                body.getCodeStatus(smsSettingLive,nowData)
            }else{
                var file = File(path)
                var part = MultipartBody.Part.createFormData(
                    "file",
                    file.name,
                    retrofits.getMultiPartFileRb(file)
                )
                var body=  apiInterface().smsSetting(part, type, state)
                body.getCodeStatus(smsSettingLive,nowData)
            }
        }
    }

    /**
     * 4G手表设置
     * @param type 0：勿扰 1：喝水 2：久坐 3，血氧，4，体温 5：午休勿扰
     * @param state 0:开启 1:关闭
     */
    fun lite4GSetting(type: Int, state: Int) {
        startHttp {
            var response = apiInterface().lite4GSetting(type, state)
            response.getCodeStatus(settingLive, nowData)
        }
    }

    /**
     * 4G手表亮度设置
     * @param type 1:节能 2:均衡 3：高亮
     * @return
     */
    fun lightSetting(type: Int) {
        startHttp {
            var response = apiInterface().lightSetting(type, 0)
            response.getCodeStatus(settingLive, nowData)
        }
    }

    /**
     * 4G手表熄屏时间设置
     * @param type 1:5秒 2:10秒 3：15秒 4:30秒
     * @return
     */
    fun screenSetting(type: Int) {
        startHttp {
            var response = apiInterface().screenSetting(type, 0)
            response.getCodeStatus(settingLive, nowData)
        }
    }

    /**
     * 4G手表用药提醒设置
     * @param medications
     */
    fun useMedicineSetting(medications: List<WearManagerBean.Medicine>) {
        startHttp {
            var json = JSONObject()
            var jsonArray = JSONArray()
            medications?.forEach {
                var jsonObject = JSONObject()
                jsonObject.put("state", it.state)
                jsonObject.put("medicationName", it.medicationName)
                var medicationJsonArray = JSONArray()
                it.medicationRemindTime?.forEach {
                    medicationJsonArray.put(it)
                }
                jsonObject.put("medicationRemindTime", medicationJsonArray)
                jsonArray.put(jsonObject)
            }
            json.put("medicationList", jsonArray)
            var response = apiInterface().useMedicineSetting(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(settingLive, nowData)
        }
    }

    /**
     * 运动目标提醒
     * @param state 0:开启 1:关闭
     * @param value 步数
     */
    fun sportSetting(showLoading: Boolean = true, state: Int, value: Int) {
        startHttp(showLoading) {
            var response = apiInterface().sportSetting(state, value)
            response.getCodeStatus(settingLive, nowData)
        }
    }

    /**
     * 心率预警提醒
     * @param state 0:开启 1:关闭
     * @param hrState 0:过速 1：过缓 2：运动
     * @param value 心率值
     * @param times  提醒次数
     * @param intervalTime 提醒间隔时间
     */
    fun heartRateSetting(state: Int, heartRate: WearManagerBean.HeartRate) {
        startHttp {
            var response = apiInterface().heartRateSetting(state, heartRate.hrState, heartRate.value, heartRate.remindCount, heartRate.intervalTime)
            response.getCodeStatus(settingLive, nowData)
        }
    }
}