package com.ytpm.hydtw.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
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;

public class WechatLoginHelper implements IWXAPIEventHandler {
    private static final String TAG = "WechatLoginHelper";
    private static WechatLoginHelper instance;

    private static final String APP_ID = "";
    private static final String APP_SECRET = "";
    private final Activity activity;
    private final IWXAPI api;
    private WechatLoginListener listener;

    public WechatLoginHelper(Activity activity) {
        String appid = SPUtils.getInstance().getString("WxAppId");
        this.activity = activity;
        api = WXAPIFactory.createWXAPI(activity, appid, true);
        api.registerApp(appid);
    }

    // 静态方法，用于获取实例
    public static synchronized WechatLoginHelper getInstance(Activity context) {
        if (instance == null) {
            instance = new WechatLoginHelper(context);
        }
        return instance;
    }

    // 发起微信登录请求
    public void login(WechatLoginListener listener) {
        this.listener = listener;

        if (!api.isWXAppInstalled()) {
            Toast.makeText(activity, "未安装微信客户端", Toast.LENGTH_SHORT).show();
            if (listener != null) {
                listener.onLoginFailed("未安装微信客户端");
            }
            return;
        }

        // 发送授权登录请求，获取code
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "wechat_login_state";
        api.sendReq(req);
    }

    // 处理微信回调
    public void handleIntent(Intent intent) {
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        // 一般不会处理，这里可以留空
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.d(TAG, "onResp: errCode = " + baseResp.errCode);
        Log.d(TAG, "onResp: errStr = " + baseResp.errStr);

        if (baseResp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
            if (baseResp.errCode == BaseResp.ErrCode.ERR_OK) {
                // 获取到授权码code，接下来获取access_token和用户信息
                String code = ((SendAuth.Resp) baseResp).code;
                Log.d(TAG, "onResp: code = " + code);
                if (listener != null) {
                    listener.onLoginSuccess(code);
                }
            } else {
                // 登录失败
                String errorMsg = "登录失败，错误码：" + baseResp.errCode + "，错误信息：" + baseResp.errStr;
                Log.e(TAG, errorMsg);
                if (listener != null) {
                    listener.onLoginFailed(errorMsg);
                }
            }
        }
    }


    // 微信登录回调接口
    public interface WechatLoginListener {
        void onLoginSuccess(String code);

        void onLoginFailed(String errorMsg);
    }
}