package com.jx.jyhd.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

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.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.jx.jyhd.R;
import com.jx.jyhd.simcpux.Constants;
import com.jx.jyhd.simcpux.Util;
import com.tsgame.tsgame_niuniu.util.Settingutil;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    public static Handler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
        Util.verifyStoragePermissions(this);
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

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

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        int what = 0;

        Log.d(TAG, "onPayFinish, errCode = " + baseResp.errCode);
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            // Toast.makeText(this, "进入", 1).show();
            switch (baseResp.errCode) {

                case BaseResp.ErrCode.ERR_OK:
                    Toast.makeText(this, "您支付成功", Toast.LENGTH_SHORT).show();
                    what = 1;
                    System.out.println("-----------------------");
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    Toast.makeText(this, "您取消支付", Toast.LENGTH_SHORT).show();

                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:
                    Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();

                    break;
                default:

                    break;
            }

            if (Settingutil.iswxpay) {
                Settingutil.iswxpay = false;
                handler.sendEmptyMessage(what);
            }
            finish();
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
