package com.miya.fastcashier.repository

import android.text.TextUtils
import androidx.annotation.WorkerThread
import com.miya.fastcashier.service.AccountService
import com.sdy.miya.moblie.component.pay.core.constance.AppTagConstance
import com.sdy.miya.moblie.component.pay.core.result.MiYaMobilePayResult
import com.sdy.miya.moblie.component.pay.core.result.ResultBuilder
import com.sdy.miya.moblie.component.pay.platform.MiYaPlatformPayService
import com.sdy.miya.moblie.component.pay.platform.bean.PayServiceResponse
import java.lang.Exception
import java.util.HashMap

/**
 * 提供支付、退款的功能
 */
object PayRepository {

    /**
     * 支付功能
     */
    @WorkerThread
    fun pay(params: Map<String, String>): PayServiceResponse {
        if (params == null) {
            throw RuntimeException("MiyaPayServiceComponent startPay params is null")
        }
        val baseUrl = params["baseUrl"]
        val payPrice = params["payPrice"]

        val payType = params["payType"]
        val token = params["token"]
        val goodsDetail = params["goodsDetail"]
        val signKey = params["signKey"]
        val saasid = params["saasid"]
        val marketid = params["marketid"]
        val posId = params["posId"]
        val cashierCode = params["cashierCode"]
        val version = params["version"]
        val orderNo = params["orderNo"]
        val memberPhone = params["memberPhone"]

        var terminalParams = params["terminalParams"]
        val paymentInfoShowMessage = params["paymentInfoShowMessage"]

        checkNotNull(baseUrl) { "MiyaPayServiceComponent startPay baseUrl is null" }
        checkNotNull(payPrice) { "MiyaPayServiceComponent startPay payPrice is null" }
        checkNotNull(payType) { "MiyaPayServiceComponent startPay payType is null" }
        checkNotNull(goodsDetail) { "MiyaPayServiceComponent startPay goodsDetail is null" }
        checkNotNull(signKey) { "MiyaPayServiceComponent startPay signKey is null" }
        checkNotNull(saasid) { "MiyaPayServiceComponent startPay saasid is null" }
        checkNotNull(marketid) { "MiyaPayServiceComponent startPay marketid is null" }
        checkNotNull(posId) { "MiyaPayServiceComponent startPay posId is null" }
        checkNotNull(cashierCode) { "MiyaPayServiceComponent startPay cashierCode is null" }
        checkNotNull(version) { "MiyaPayServiceComponent startPay version is null" }
        checkNotNull(orderNo) { "MiyaPayServiceComponent startPay oderNo is null" }

        val paramsMap: MutableMap<String, Any?> = HashMap(14, 1.0f)
        paramsMap["baseUrl"] = baseUrl
        paramsMap["signKey"] = signKey
        paramsMap["saasId"] = saasid
        paramsMap["marketId"] = marketid
        paramsMap["posId"] = posId
        paramsMap["cashierCode"] = cashierCode
        paramsMap["version"] = version
        paramsMap["token"] = token
        paramsMap["goodsDetail"] = goodsDetail
        paramsMap["payPrice"] = payPrice
        paramsMap["payType"] = payType
        paramsMap["orderNo"] = orderNo
        paramsMap["appTag"] = AppTagConstance.QINGTING_TYPE
        paramsMap["needRetryWhenNetworkError"] = true

        try {
            //发起付款
            val miYaMobilePayResult =
                MiYaPlatformPayService.getService().syncStartMiYaPay(paramsMap)
            if (miYaMobilePayResult != null && miYaMobilePayResult.resultCode == ResultBuilder.SUCCESS) {
                return miYaMobilePayResult.payServiceResponse
            } else {
                throw RuntimeException(miYaMobilePayResult!!.errorMsg)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            throw RuntimeException(e.message)
        }
    }

    /**
     * 退款功能
     */
    fun refundByOrderNo(params: Map<String, String>): PayServiceResponse {

        val shopInfo = AccountService.getAccountInfo()?.shopInfo

        val baseUrl = shopInfo?.miyaPayPlatformUrl;
        val signKey = shopInfo?.miyaPayPlatformSignKey
        val saasid = shopInfo?.saasid
        val marketid = shopInfo?.storeId
        val posId = shopInfo?.posId
        val cashierCode = shopInfo?.operatorId
        val version = "1.5"
        val oriOrderNo = params["oriOrderNo"]
        val refundOrderNo = params["refundOrderNo"]
        val refundPrice = params["refundPrice"]

        checkNotNull(baseUrl) { "refundByOrderNo baseUrl is null" }
        checkNotNull(signKey) { "refundByOrderNo signKey is null" }
        checkNotNull(saasid) { "refundByOrderNo saasid is null" }
        checkNotNull(marketid) { "refundByOrderNo marketid is null" }
        checkNotNull(version) { "refundByOrderNo version is null" }
        checkNotNull(oriOrderNo) { "refundByOrderNo oriOrderNo is null" }
        checkNotNull(refundOrderNo) { "refundByOrderNo refundOrderNo is null" }
        checkNotNull(refundPrice) { "refundByOrderNo refundPrice is null" }


        val paramsMap: MutableMap<String, Any> = HashMap(14, 1.0f)
        paramsMap["baseUrl"] = baseUrl
        paramsMap["signKey"] = signKey
        paramsMap["saasId"] = saasid
        paramsMap["marketId"] = marketid
        posId?.let {
            paramsMap["posId"] = it
        }
        cashierCode?.let {
            paramsMap["cashierCode"] = it
        }
        paramsMap["version"] = version
        paramsMap["oriOrderNo"] = oriOrderNo
        paramsMap["refundOrderNo"] = refundOrderNo
        paramsMap["refundPrice"] = refundPrice
        try {
            //发起付款
            val miYaMobilePayResult = MiYaPlatformPayService.getService()
                .syncRequestRefundOrderByOrderId(paramsMap)
            if (miYaMobilePayResult != null && miYaMobilePayResult.resultCode == ResultBuilder.SUCCESS) {
                return miYaMobilePayResult.payServiceResponse
            } else {
                throw RuntimeException(miYaMobilePayResult!!.errorMsg)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            throw RuntimeException(e.message)
        }
    }

    /**
     * 只支持微信或者支付宝
     * payType 2是微信 1是支付宝
     */
    fun generatePayCode(orderNo:String,payPrice:String,payType:String): PayServiceResponse {

        val shopInfo = AccountService.getAccountInfo()?.shopInfo

        val baseUrl = shopInfo?.miyaPayPlatformUrl;
        val signKey = shopInfo?.miyaPayPlatformSignKey
        val saasid = shopInfo?.saasid
        val marketid = shopInfo?.storeId
        val posId = shopInfo?.posId
        val cashierCode = shopInfo?.operatorId
        val version = "1.5"

        checkNotNull(baseUrl) { "baseUrl is null" }
        checkNotNull(payPrice) { "payPrice is null" }
        checkNotNull(payType) { "payType is null" }
        checkNotNull(signKey) { "signKey is null" }
        checkNotNull(saasid) { "saasid is null" }
        checkNotNull(marketid) { "marketid is null" }
//        checkNotNull(posId) { "posId is null" }
//        checkNotNull(cashierCode) { "cashierCode is null" }
        checkNotNull(orderNo) { "orderNo is null" }

        val paramsMap: MutableMap<String, Any?> = HashMap(14, 1.0f)
        paramsMap["baseUrl"] = baseUrl
        paramsMap["signKey"] = signKey
        paramsMap["saasId"] = saasid
        paramsMap["marketId"] = marketid
        paramsMap["posId"] = posId
        paramsMap["cashierCode"] = cashierCode
        paramsMap["version"] = version
        paramsMap["payPrice"] = payPrice
        paramsMap["payType"] = payType
        paramsMap["orderNo"] = orderNo

        try {
            val miYaMobilePayResult =
                MiYaPlatformPayService.getService().syncGenMiYaPayCode(paramsMap)
            if (miYaMobilePayResult != null && miYaMobilePayResult.resultCode == ResultBuilder.SUCCESS) {
                return miYaMobilePayResult.payServiceResponse
            } else {
                throw RuntimeException(miYaMobilePayResult!!.errorMsg)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            throw RuntimeException(e.message)
        }
    }

    fun payResultQuery(orderNo:String): PayServiceResponse{
        val shopInfo = AccountService.getAccountInfo()?.shopInfo

        val baseUrl = shopInfo?.miyaPayPlatformUrl;
        val signKey = shopInfo?.miyaPayPlatformSignKey
        val saasid = shopInfo?.saasid
        val marketid = shopInfo?.storeId
        val posId = shopInfo?.posId
        val cashierCode = shopInfo?.operatorId
        val version = "1.5"

        checkNotNull(baseUrl) { "baseUrl is null" }
        checkNotNull(signKey) { "signKey is null" }
        checkNotNull(saasid) { "saasid is null" }
        checkNotNull(marketid) { "marketid is null" }
//        checkNotNull(posId) { "posId is null" }
//        checkNotNull(cashierCode) { "cashierCode is null" }

        val paramsMap: MutableMap<String, Any?> = HashMap(14, 1.0f)
        paramsMap["baseUrl"] = baseUrl
        paramsMap["signKey"] = signKey
        paramsMap["saasId"] = saasid
        paramsMap["marketId"] = marketid
        paramsMap["posId"] = posId
        paramsMap["cashierCode"] = cashierCode
        paramsMap["version"] = version
        paramsMap["orderNo"] = orderNo

        try {
            val miYaMobilePayResult =
                MiYaPlatformPayService.getService().syncRequestOrderStateByOrderId(paramsMap)
            if (miYaMobilePayResult != null && miYaMobilePayResult.resultCode == ResultBuilder.SUCCESS) {
                return miYaMobilePayResult.payServiceResponse
            } else if(!TextUtils.isEmpty(miYaMobilePayResult.errorMsg)){
                throw RuntimeException(miYaMobilePayResult.errorMsg)
            }else if(miYaMobilePayResult.payServiceResponse != null && !TextUtils.isEmpty(miYaMobilePayResult.payServiceResponse.chanelErrorMsg)){
                throw RuntimeException(miYaMobilePayResult.payServiceResponse.chanelErrorMsg)
            }else{
                throw RuntimeException("支付异常！")
            }
        } catch (e: Exception) {
            e.printStackTrace()
            throw RuntimeException(e.message)
        }
    }


}