package com.ydl.component.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.medical.ydl.medical.pay.bean.MedicalPayStatus;
import com.medical.ydl.medical.pay.bean.MedicalWXPayEvent;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yidianling.ydl_pay.common.bean.WeiXinPayStatusEvent;

import de.greenrobot.event.EventBus;

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/04/18
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, null);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        finish();
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.e("WXPayEntryActivity","onPayFinish, errCode = " + baseResp.errCode);
        EventBus.getDefault().post(new WeiXinPayStatusEvent(baseResp.errCode == 0, baseResp.errStr));
        EventBus.getDefault().post(new MedicalWXPayEvent(new MedicalPayStatus(baseResp.errCode == 0)));
        finish();
    }
}
