package com.zswc.creativity.wxapi

import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.hwangjr.rxbus.RxBus
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.zswc.creativity.config.ConfigRx
import com.zswc.creativity.utils.WeChatUtil

/**
 *Created by shihuiyun
 *on 2021/1/8
 */
class WXPayEntryActivity : Activity(), IWXAPIEventHandler {
    override fun onReq(baseResp: BaseReq?) {
//        if (baseResp?.type === ConstantsAPI.COMMAND_PAY_BY_WX) {
//            when (baseResp.hashCode()) {
//                0 -> {
//                    RxBus.get().post(ConfigRx.PAY_COMPLETE, "")
//                    Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show()
//                    //  Log.d(TAG, "onResp: resp.errCode = 0   支付成功")
//                }
//                -1 -> {
//                    Toast.makeText(this, "支付错误" + baseResp.hashCode(), Toast.LENGTH_SHORT).show()
//                    // Log.d(TAG, "onResp: resp.errCode = -1  支付错误")
//                }
//                -2 -> {
//                    // Log.d(TAG, "onResp: resp.errCode = -2  用户取消")
//                    Toast.makeText(this, "用户取消" + baseResp.hashCode(), Toast.LENGTH_SHORT).show()
//                }
//            }
//            finish() //这里需要关闭该页面
//        }
    }

    override fun onResp(resp: BaseResp?) {


        /*  if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.app_tip);
            builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
            builder.show();
        }*/
        val errCode = resp!!.errCode
        if (errCode == 0) {    //显示充值成功的页面和需要的操作
            RxBus.get().post(ConfigRx.PAY_COMPLETE, "")
            Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show()
        } else if (errCode == -1) {  //错误
            Toast.makeText(this, "支付错误$errCode", Toast.LENGTH_SHORT).show()
        } else if (errCode == -2) { //用户取消
            Log.e("-pay--", "pay=ff: ")

        }

        finish() //这里需要关闭该页面


    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        RxBus.get().register(this)
        val mWXApi = WXAPIFactory.createWXAPI(this, WeChatUtil.WECHAT_APP_ID, true)
        mWXApi.handleIntent(intent, this)

    }

    override fun onDestroy() {
        super.onDestroy()
        RxBus.get().unregister(this)
    }
}
