package com.zj365.health.vm

import androidx.lifecycle.MutableLiveData
import com.xty.base.vm.BaseVm
import com.xty.common.Const
import com.xty.network.model.SelectedAnswerBean
import com.xty.network.model.CancerGoodsBean
import com.xty.network.model.PayBean
import com.xty.network.model.PayGoodsBean
import com.xty.network.model.RespBody
import okhttp3.MultipartBody
import org.json.JSONArray
import org.json.JSONObject
import java.io.File

class TongueDiagnosisVm: BaseVm() {

    val payGoodsObservable by lazy { MutableLiveData<RespBody<PayGoodsBean>>() }

    val payObservable by lazy { MutableLiveData<RespBody<PayBean>>() }

    val cancerGoodsBeanObservable by lazy { MutableLiveData<RespBody<MutableList<CancerGoodsBean>>>() }

    val orderInfo by lazy { MutableLiveData<RespBody<CancerGoodsBean>>() }

    val haveOrder by lazy { MutableLiveData<RespBody<Boolean>>() }

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

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

    val imagePath by lazy { MutableLiveData<RespBody<MutableList<String>>>() }
    //获取舌诊商品信息
    fun getTongueGoods(){
        startHttp {
            apiInterface().getTongueGoods()
            var response = apiInterface().getTongueGoods()
            response.getCodeStatus(payGoodsObservable, nowData)
        }
    }

    //获取支付信息
    fun payTongueOrder(goodsId:String,isWeChatPay:Boolean){
        startHttp {
            var json = JSONObject()
            json.put("goodsId",goodsId)
            json.put("channelName", if (isWeChatPay) "WEIXIN_APP" else "ALIPAY_APP")
            var response = apiInterface().payTongueOrder(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(payObservable, nowData)
        }
    }

    //获取舌诊订单列表
    fun tongueOrderList(page: Int) {
        startHttp {
            var json = JSONObject()
            json.put("pageNum",page)
            json.put("pageSize", Const.PAGE_SIZE)
            var response =
                apiInterface().tongueOrderList(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(cancerGoodsBeanObservable, nowData)
        }
    }

    //填写问诊信息
    fun tongueInquiry(outId: String,datas : List<SelectedAnswerBean>) {
        startHttp {
            var json = JSONObject()
            json.put("outId",outId)
            json.put("inquiryAnswers", JSONArray(datas.map {
                val answerJson = JSONObject()
                answerJson.put("questionIndex", it.questionIndex)
                answerJson.put("answerCode", it.answerCode)
                answerJson
            }))
            var response =
                apiInterface().tongueInquiry(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(questionInfo, nowData)
        }
    }

    //填写舌诊信息或者修改
    fun updateTongueOrder(orderNo:String,sex:String,age:String,realName:String,imageUrl:String,backImageUrl:String) {
        startHttp {
            var json = JSONObject()
            json.put("orderNo",orderNo)
            json.put("sex", sex)
            json.put("age", age)
            json.put("realName", realName)
            json.put("imageUrl", imageUrl)
            json.put("backImageUrl", backImageUrl)
            var response =
                apiInterface().updateTongueOrder(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(upDateInfo, nowData)
        }
    }

    fun uploadImagesFiles(path: List<String>) {
        if (path.isEmpty()) {
            return
        }
        var list = mutableListOf<MultipartBody.Part>()
        for (s in path) {
            if (s.contains("http")){
                continue
            }
            var f = File(s)
            var rb =
                MultipartBody.Part.createFormData("files", f.name, retrofits.getMultiPartFileRb(f))
            list.add(rb)
        }
        if (list.isEmpty()){
            return
        }
        startHttp {
            var body = apiInterface().uploadImagesFile(list)
            body.getCodeStatus(imagePath, nowData)
        }
    }

    //获取详情
    fun tongueGetOrderInfo(orderNo: String) {
        startHttp {
            var json = JSONObject()
            json.put("orderNo",orderNo)
            var response =
                apiInterface().tongueGetOrderInfo(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(orderInfo, nowData)
        }
    }

    //获取有无未完成订单
    fun tongueHasNoFinishOrder() {
        startHttp {
            var json = JSONObject()
            var response =
                apiInterface().tongueHasNoFinishOrder(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(haveOrder, nowData)
        }
    }

}