package com.lukouguoji.gjc.viewModel

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.holder.GjcGoodsManifestViewHolder
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GjcGoodsDetailsBean
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.impl.FlowBus
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 GjcGoodsManifestViewModel : BasePageViewModel() {

    var movID = ""

    val itemLayoutId = R.layout.item_goods_manifest
    val itemViewHolder = GjcGoodsManifestViewHolder::class.java

    // 签名
    val signature = MutableLiveData<String>()

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

    // 详情
    val dataBean = MutableLiveData<GjcGoodsDetailsBean>()

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

    override fun getData() {
        showLoading()
        launchCollect({
            NetApply.api
                .getGjcGoodsDetail(movID)
        }) {
            onSuccess = {
                if (it.verifySuccess() && it.data != null) {
                    val data = it.data!!
                    pageModel.handleDataList(data.loadingHandoverList)
                    dataBean.postValue(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("获取数据失败"))
                }
            }
            onComplete = {
                dismissLoading()
            }
        }
    }

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

    /**
     * 提交签名
     */
    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
                .gjcGoodsSubmitSignature(
                    part, mutableMapOf(
                        "movID " to movID.toRequestBody("multipart/form-data".toMediaTypeOrNull()),
                    )
                )
        }) {
            onSuccess = {
                if (it.verifySuccess()) {
                    showToast(it.msg.noNull("提交成功"))
                    FlowBus.with<Any>(ConstantEvent.GJC_HANDOVER_SIGNATURE_SUCCESS)
                        .tryEmit(true)
                    context.finish()
                } else {
                    showToast(it.msg.noNull("提交失败"))
                }
            }
        }
    }

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

}