package com.lukouguoji.gjj.viewModel

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.holder.GjjGoodsManifestViewHolder
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GjjGoodsDetailsBean
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.finish
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.ktx.toBoolean
import com.lukouguoji.module_base.ui.signature.SignatureActivity
import dev.utils.common.cipher.Base64
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File

class GjjGoodsManifestViewModel : BasePageViewModel() {

    var id: String = ""

    val itemLayoutId = R.layout.item_gjj_goods_manifest
    val itemViewHolder = GjjGoodsManifestViewHolder::class.java

    // 签名
    val signature = MutableLiveData("")

    val dataBean = MutableLiveData<GjjGoodsDetailsBean>()

    // 返回签名
    val resultSign = MutableLiveData<Bitmap>()

    ///////////////////////////////////////////////////////////////////////////
    // 方法区
    ///////////////////////////////////////////////////////////////////////////

    override fun getData() {
        launchCollect({
            NetApply.api.getGjjGoodsDetail(id)
        }) {
            onSuccess = {
                if (it.verifySuccess() && it.data != null) {
                    dataBean.value = it.data!!
                    pageModel.handleDataList(it.data!!.gjjWarehouseList)
                    val data = it.data!!
                    // 已交接 & 有签名
                    if (data.isCheck.toBoolean() && !data.sign.isNullOrEmpty()) {
                        val bytes = Base64.decode(
                            data.sign!!,
                            Base64.DEFAULT
                        )
                        val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
                        resultSign.postValue(bitmap)
                    }
                } else {
                    showToast(it.msg.noNull("获取数据失败"))
                }
            }
        }
    }

    /**
     * 打印点击
     */
    fun printClick(view: View) {
        view.context.finish()
    }

    /**
     * 签字点击
     */
    fun signClick(view: View) {
        if (dataBean.value?.isCheck?.toBoolean() == false) {
            SignatureActivity.start(view.context)
        }
    }

    /**
     * 提交签名
     */
    fun submitSignature(context: Context, signature: String) {
        launchCollect({
            val file = File(signature)
            val body = file.asRequestBody("multipart/form-data".toMediaTypeOrNull())
            val part = MultipartBody.Part.createFormData("file", file.name, body)
            NetApply.api
                .gjjGoodsSubmitSignature(
                    part, mutableMapOf(
                        "movID " to id.toRequestBody("multipart/form-data".toMediaTypeOrNull()),
                    )
                )
        }) {
            onSuccess = {
                if (it.verifySuccess()) {
                    showToast(it.msg.noNull("提交成功"))
                    context.finish()
                } else {
                    showToast(it.msg.noNull("提交失败"))
                }
            }
        }
    }
}