package com.adealink.weparty.share.qrcode.viewmodel

import android.graphics.Bitmap
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.adealink.frame.base.CommonDataNullError
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.App
import com.adealink.weparty.module.order.data.QRCodeOrder
import com.adealink.weparty.module.playmate.data.UserPlaymateCategoryData
import com.adealink.weparty.share.data.QRCodeOrderData
import com.adealink.weparty.share.data.QRCodeOrderReq
import com.adealink.weparty.share.datasource.remote.ShareHttpService
import com.adealink.weparty.util.suspendGenerateQRCode
import kotlinx.coroutines.launch

data class QRCodeOrderDetail(
    val category: UserPlaymateCategoryData,
    val order: QRCodeOrderData
)

class QRCodeViewModel : BaseViewModel() {

    private val shareHttpService by lazy {
        App.instance.networkService.getHttpService(ShareHttpService::class.java)
    }

    val currentCategoryLD = MutableLiveData<UserPlaymateCategoryData?>()

    fun selectCategory(category: UserPlaymateCategoryData?) {
        currentCategoryLD.send(category)
    }

    //playmateId -> QRCodeOrderData
    private val commonQRCodeMap = mutableMapOf<String, QRCodeOrderDetail>()

    fun getCommonQRCodeOrder(
        category: UserPlaymateCategoryData,
    ): LiveData<Rlt<QRCodeOrderDetail>> {
        val liveData = OnceMutableLiveData<Rlt<QRCodeOrderDetail>>()
        viewModelScope.launch {
            val data = commonQRCodeMap[category.id]
            if (data != null) {
                liveData.send(Rlt.Success(data))
                return@launch
            }
            val rlt = shareHttpService.createQRCodeOrder(
                QRCodeOrderReq(
                    category.id,
                    QRCodeOrder.COMMON_QRCODE.type,
                    null
                )
            )
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

                is Rlt.Success -> {
                    val data = rlt.data.data
                    if (data == null) {
                        liveData.send(Rlt.Failed(CommonDataNullError()))
                    } else {
                        val detail = QRCodeOrderDetail(
                            category,
                            data
                        )
                        commonQRCodeMap[category.id] = detail
                        liveData.send(Rlt.Success(detail))
                    }
                }
            }
        }
        return liveData
    }

    fun getSpecialQRCodeOrder(
        category: UserPlaymateCategoryData,
        purchaseQty: Int
    ): LiveData<Rlt<QRCodeOrderDetail>> {
        val liveData = OnceMutableLiveData<Rlt<QRCodeOrderDetail>>()
        viewModelScope.launch {
            val rlt = shareHttpService.createQRCodeOrder(
                QRCodeOrderReq(
                    category.id,
                    QRCodeOrder.SPECIAL_QRCODE.type,
                    purchaseQty
                )
            )
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

                is Rlt.Success -> {
                    val data = rlt.data.data
                    if (data == null) {
                        liveData.send(Rlt.Failed(CommonDataNullError()))
                    } else {
                        val detail = QRCodeOrderDetail(
                            category,
                            data
                        )
                        liveData.send(Rlt.Success(detail))
                    }
                }
            }
        }
        return liveData
    }

    fun generateQRCode(qrCodeText: String): LiveData<Bitmap> {
        val liveData = OnceMutableLiveData<Bitmap>()
        viewModelScope.launch {
            liveData.send(suspendGenerateQRCode(qrCodeText))
        }
        return liveData
    }


}