package com.gamedog.vididin.features.withdraw

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ama.core.architecture.util.AndroidUtil
import com.ama.core.architecture.util.DeviceUtil
import com.ama.core.architecture.util.MD5Util
import com.ama.core.architecture.util.NetUtil
import com.gamedog.vididin.VidiConst
import com.gamedog.vididin.beans.req.PayInitReq
import com.gamedog.vididin.beans.req.PayoutCheckReq
import com.gamedog.vididin.beans.req.PayoutReq
import com.gamedog.vididin.beans.resp.PayInit
import com.gamedog.vididin.beans.resp.PayoutCheckData
import com.gamedog.vididin.beans.resp.PayoutData
import com.gamedog.vididin.core.login.login.AccountManager
import com.gamedog.vididin.manager.WithdrawManager
import com.gamedog.vididin.netbase.NetworkUtil
import com.gamedog.vididin.netbase.Result
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class WithdrawViewModel : ViewModel() {
    private val _InitData = MutableStateFlow<Result<PayInit>>(Result.Loading)
    val InitData: StateFlow<Result<PayInit>> = _InitData.asStateFlow()

    private val _PayoutResult = MutableStateFlow<Result<PayoutData?>>(Result.Loading)
    val PayoutResult: StateFlow<Result<PayoutData?>> = _PayoutResult.asStateFlow()

    private val _CheckResult = MutableStateFlow<Result<PayoutCheckData?>>(Result.Loading)
    val CheckResult: StateFlow<Result<PayoutCheckData?>> = _CheckResult.asStateFlow()


    fun withdrawInit() {
        viewModelScope.launch {
            val requestParam = PayInitReq().applyInitFields()

            _InitData.value = Result.Loading
            _InitData.value = NetworkUtil.callApi {
                NetworkUtil.apiservice().withdrawInit(requestParam)
            }
        }
    }


    fun withdrawPayout(initUUID: String, payItemId: Int, payCashNum: Double) {
        viewModelScope.launch {
            val requestParam = PayoutReq().applyInitFields().apply {
                val bankAccount = AccountManager.getAccount().bankInfo?.bankAccount
                val accountType = "CPF"

                account = bankAccount
                item_id = payItemId
                amount = payCashNum.toString()
                additional_remark = "communnyboneycashmoneyrewardfastrealgame"
                uuid = initUUID
                account_type = accountType
                document_type = accountType
                document_id = bankAccount
                name = "CapyBucks"
                clientName = AndroidUtil.getPackageId()


                dataAdjust.gps_adid = "gaid"
                dataAdjust.android_id = "androidid"
                dataAdjust.adid = "adid"
                dataAdjust.user_agent = "GetUerAgent"
                dataAdjust.price = amount
                dataAdjust.currency = "USD"

                dataShuShu.gps_gaid = "gaid"
                dataShuShu.android_id = "androidid"
                dataShuShu.adid = "adid"
                dataShuShu.user_agent = "GetUerAgent"
                dataShuShu.price = amount
                dataShuShu.currency = "USD"
                dataShuShu.payment_method = "Pix"
                dataShuShu.payment_type = accountType
                dataShuShu.payment_number = account
                dataShuShu.iap_name = "100br"
                dataShuShu.gamecoin_number = "100"
                dataShuShu.gamecoin_type = "gold"
                dataShuShu.ss_account_id = "GetSSAccountId"
                dataShuShu.ss_distinct_id = "GetSSDistinctId"
                dataShuShu.ss_super_properties =  "GetSSSuper Properties"
            }

            _PayoutResult.value = Result.Loading
            _PayoutResult.value = NetworkUtil.callApi {
                NetworkUtil.apiservice().withdrawPayout(requestParam)
            }
        }
    }

    fun withdrawCheck(recordNo: String) {
        viewModelScope.launch {
            val requestParam = PayoutCheckReq().applyInitFields().apply {
                record_no = recordNo
            }

            _CheckResult.value = Result.Loading
            _CheckResult.value = NetworkUtil.callApi {
                NetworkUtil.apiservice().withdrawCheck(requestParam)
            }
        }
    }


    private fun <T : PayInitReq> T.applyInitFields(): T {
        return WithdrawManager.instance().applyInitFields(this)
    }

}