/*
 * Copyright (C) 2020 The OpenFlutter Organization
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jarvan.fluwx.handlers

import com.jarvan.fluwx.FluwxPlugin
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelbiz.*
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelpay.PayResp
import io.flutter.plugin.common.MethodChannel

object FluwxResponseHandler {

    private const val errStr = "errStr"
    private const val errCode = "errCode"
    private const val openId = "openId"
    private const val type = "type"

    fun handleResponse(response: BaseResp) {
        when (response) {
            is SendAuth.Resp -> handleAuthResponse(response)
            is SendMessageToWX.Resp -> handleSendMessageResp(response)
            is PayResp -> handlePayResp(response)
            is WXLaunchMiniProgram.Resp -> handleLaunchMiniProgramResponse(response)
            is SubscribeMessage.Resp -> handleSubscribeMessage(response)
            is WXOpenBusinessWebview.Resp -> handlerWXOpenBusinessWebviewResponse(response)
            is WXOpenCustomerServiceChat.Resp -> handlerWXOpenCustomerServiceChatResponse(response)
            is WXOpenBusinessView.Resp -> handleWXOpenBusinessView(response)
            is ChooseCardFromWXCardPackage.Resp -> handleWXOpenInvoiceResponse(response)
        }
    }
    private fun handleWXOpenInvoiceResponse(response: ChooseCardFromWXCardPackage.Resp) {
        val result = mapOf(
                "cardItemList" to response.cardItemList,
                "transaction" to response.transaction,
                "openid" to response.openId,
                errStr to response.errStr,
                type to response.type,
                errCode to response.errCode)

        FluwxPlugin.callingChannel?.invokeMethod("onOpenWechatInvoiceResponse", result)
    }

    private fun handleWXOpenBusinessView(response: WXOpenBusinessView.Resp) {
        val result = mapOf(
            "openid" to response.openId,
            "extMsg" to response.extMsg,
            "businessType" to response.businessType,
            errStr to response.errStr,
            type to response.type,
            errCode to response.errCode)

        FluwxPlugin.callingChannel?.invokeMethod("onOpenBusinessViewResponse", result)
    }

    private fun handleSubscribeMessage(response: SubscribeMessage.Resp) {
        val result = mapOf(
                "openid" to response.openId,
                "templateId" to response.templateID,
                "action" to response.action,
                "reserved" to response.reserved,
                "scene" to response.scene,
                type to response.type)

        FluwxPlugin.callingChannel?.invokeMethod("onSubscribeMsgResp", result)
    }

    private fun handleLaunchMiniProgramResponse(response: WXLaunchMiniProgram.Resp) {
        val result = mutableMapOf(
                errStr to response.errStr,
                type to response.type,
                errCode to response.errCode,
                openId to response.openId
        )

        response.extMsg?.let {
            result["extMsg"] = response.extMsg
        }

        FluwxPlugin.callingChannel?.invokeMethod("onLaunchMiniProgramResponse", result)
    }

    private fun handlePayResp(response: PayResp) {
        val result = mapOf(
                "prepayId" to response.prepayId,
                "returnKey" to response.returnKey,
                "extData" to response.extData,
                errStr to response.errStr,
                type to response.type,
                errCode to response.errCode
        )
        FluwxPlugin.callingChannel?.invokeMethod("onPayResponse", result)
    }

    private fun handleSendMessageResp(response: SendMessageToWX.Resp) {
        val result = mapOf(
                errStr to response.errStr,
                type to response.type,
                errCode to response.errCode,
                openId to response.openId)

        FluwxPlugin.callingChannel?.invokeMethod("onShareResponse", result)
    }

    private fun handleAuthResponse(response: SendAuth.Resp) {
        val result = mapOf(
                errCode to response.errCode,
                "code" to response.code,
                "state" to response.state,
                "lang" to response.lang,
                "country" to response.country,
                errStr to response.errStr,
                openId to response.openId,
                "url" to response.url,
                type to response.type)

        FluwxPlugin.callingChannel?.invokeMethod("onAuthResponse", result)
    }


    private fun handlerWXOpenBusinessWebviewResponse(response: WXOpenBusinessWebview.Resp) {
        val result = mapOf(
                errCode to response.errCode,
                "businessType" to response.businessType,
                "resultInfo" to response.resultInfo,
                errStr to response.errStr,
                openId to response.openId,
                type to response.type)

        FluwxPlugin.callingChannel?.invokeMethod("onWXOpenBusinessWebviewResponse", result)
    }

    private fun handlerWXOpenCustomerServiceChatResponse(response: WXOpenCustomerServiceChat.Resp) {
        val result = mapOf(
            errCode to response.errCode,
            errStr to response.errStr,
            openId to response.openId,
            type to response.type)

        FluwxPlugin.callingChannel?.invokeMethod("onWXOpenCustomerServiceChatResponse", result)
    }
}