package com.zhangxin.magicbox.wxapi;



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

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zxhl.cms.common.Constant;
import com.zxhl.cms.utils.EventUtils;
import com.zxhl.cms.utils.RxBus;
import com.zxhl.cms.utils.Utils;
import com.zxhl.novel.utils.server.PayResultEntity;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private String TAG = "PAY_GET";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//		setContentView(R.layout.pay_result);
		System.out.println("WXPayEntryActivity:$Constant.WX_APPID");
    	api = WXAPIFactory.createWXAPI(this, Constant.WX_APPID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		System.out.println("WXPayEntryActivity:$onNewIntent");

		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
		System.out.println("WXPayEntryActivity onReq:"+req.openId);
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle(resp.openId);
//			builder.setMessage(String.valueOf(resp.errCode));
//			builder.show();
			try{
				Log.d(TAG,"onPayFinish, resp.openId = " + Utils.obj2Str(resp));
//				JSONObject json = new JSONObject(Utils.obj2Str(resp));
//				String extData = json.getString("extData");
//				String prepayId =json.getString("prepayId");
				if(resp.errCode== BaseResp.ErrCode.ERR_OK){
					result(true, Utils.obj2Str(resp));
				}else{
					EventUtils.INSTANCE.onEvent("member_page_pay_fail","code:"+resp.errCode+" str:"+resp.errStr);
					result(false, Utils.obj2Str(resp));
				}

			}catch (Exception e){
				EventUtils.INSTANCE.onEvent("member_page_pay_fail","code:"+resp.errCode+" str:"+resp.errStr);
				result(false,resp.errStr);
			}

		}else if(resp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM){
			WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) resp;
			String extraData =launchMiniProResp.extMsg;
			Log.e("MXL","data"+extraData);
			result(true, Utils.obj2Str(extraData));

		} else{
			EventUtils.INSTANCE.onEvent("member_page_pay_fail","code:"+resp.errCode+" str:"+resp.errStr);
			result(false,resp.errStr);
		}
	}
	public void  result(Boolean isPaysuc, String txt){
//        if(!TextUtils.isEmpty(order)) {
////            var mPresenter = PayPresenter(this)
////            mPresenter?.verifyPay(order)
////        }
		PayResultEntity entity = new PayResultEntity();
		entity.setPaySuc(isPaysuc);
		entity.setTxt(txt);
		entity.setPayType(11);//微信支付类型
		RxBus.get().post(Constant.PAY_RESULT,entity);
		finish();
	}
}