package com.ym.cydyj.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.ym.unityandroid.UnityWxLoginUtil;
import com.unity3d.player.UnityPlayer;
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {


    ///通知Unity哪个物体GameObjectName  哪个方法CallBackFuncName
    public static String GameObjectName = "WxLoginTool";//GameObjectName 的 WXPayCallback方法
    public static String CallBackFuncName = "BingWxLogin";//
    //监听支付回调的对象
    private IWXAPI api;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建微信API对象
        api = WXAPIFactory.createWXAPI(this, UnityWxLoginUtil.WxAPP_ID);
        //把接受到的Intent给wxapi这个对象，它会解析回调结果，通过我们实现的IWXAPIEventHandler接口回调给我们
        api.handleIntent(getIntent(), this);
    }

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


    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                break;
            case ConstantsAPI.COMMAND_LAUNCH_BY_WX:
                break;
            default:
                break;
        }
    }

    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp resp) {
        //UnityPlayer.UnitySendMessage(GameObjectName,CallBackFuncName, "回调响应");
        switch (resp.getType()) {
            case ConstantsAPI.COMMAND_LAUNCH_BY_WX:
                break;
            case ConstantsAPI.COMMAND_SENDAUTH:
                onSendAuthResp(resp);
                break;
            default:
                break;
        }
        finish();
    }

    /**
     * 授权回调,获取code后发往服务端，获取access_token
     * @param resp
     */
    public void onSendAuthResp(BaseResp resp) {
        int errorCode = resp.errCode;
        switch (errorCode)
        {
            case BaseResp.ErrCode.ERR_OK:
                //用户同意
                String code = ((SendAuth.Resp) resp).code;
                UnityPlayer.UnitySendMessage(GameObjectName,CallBackFuncName, code);
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                //用户拒绝
                UnityPlayer.UnitySendMessage("WxLoginTool","OnWxAuthFail", "用户拒绝");
                Toast.makeText(this, "用户拒绝", Toast.LENGTH_SHORT).show();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                //用户取消
                UnityPlayer.UnitySendMessage("WxLoginTool","OnWxAuthFail", "用户取消");
                Toast.makeText(this, "用户取消", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
