package com.gh.gamecenter.common.utils;

import android.content.Context;
import android.content.Intent;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;

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.tencent.mm.opensdk.utils.ILog;

import java.util.Collections;
import java.util.List;

/**
 * 通用微信SDK代理工厂类
 * 1. 构造通用微信SDK代理类实例
 * 2. 处理WXEntryActivity的结果回调
 */
public final class WXAPIProxyFactory {
    private WXAPIProxyFactory() {
    }

    private static final MutableLiveData<BaseResp> mRespLiveData = new MutableLiveData<>();

    public static <T extends BaseReq, R extends BaseResp> WXAPIProxy<T, R> createWXAPI(Context context, String appid) {

        final String transaction = String.valueOf(System.currentTimeMillis());

        return createWXAPI(context, appid, new WXHandler<T, R>() {

            @Override
            public void handleOnReq(T req) {
                req.transaction = transaction;
            }

            @Override
            public R handleOnResp(BaseResp resp) {
                if (transaction.equals(resp.transaction)) {
                    return (R) resp;
                }
                return null;
            }
        });
    }

    public static <T extends BaseReq, R extends BaseResp> WXAPIProxy<T, R> createWXAPI(Context context, String appid, WXHandler<T, R> handler) {
        return new WXAPIProxy<>(context, appid, handler);
    }

    public static MutableLiveData<BaseResp> getLiveData() {
        return mRespLiveData;
    }

    public final static class WXAPIProxy<T extends BaseReq, R extends BaseResp> {

        private final IWXAPI mWxAPI;

        private WXHandler<T, R> mHandler;

        private final MediatorLiveData<R> mLiveData = new MediatorLiveData<R>() {{
            addSource(mRespLiveData, input -> {
                R resp = mHandler.handleOnResp(input);
                if (resp != null) {
                    mLiveData.setValue(resp);
                }
            });
        }};

        private WXAPIProxy(Context context, String appid, WXHandler<T, R> handler) {
            mHandler = handler;
            mWxAPI = WXAPIFactory.createWXAPI(context, appid);
        }

        public boolean registerApp(String s) {
            return mWxAPI.registerApp(s);
        }

        public boolean registerApp(String s, long l) {
            return mWxAPI.registerApp(s, l);
        }

        public void unregisterApp() {
            mWxAPI.unregisterApp();
        }

        public boolean handleIntent(Intent var1, IWXAPIEventHandler var2) {
            return mWxAPI.handleIntent(var1, var2);
        }

        public boolean isWXAppInstalled() {
            return mWxAPI.isWXAppInstalled();
        }

        public int getWXAppSupportAPI() {
            return mWxAPI.getWXAppSupportAPI();
        }

        public boolean openWXApp() {
            return mWxAPI.openWXApp();
        }

        public boolean sendReq(T req) {
            mHandler.handleOnReq(req);
            return mWxAPI.sendReq(req);
        }

        public boolean sendResp(R resp) {
            return mWxAPI.sendResp(resp);
        }

        public void detach() {
            mWxAPI.detach();
        }

        public void setLogImpl(ILog var1) {
            mWxAPI.setLogImpl(var1);
        }

        public LiveData<R> getLiveData() {
            return mLiveData;
        }

    }

    public interface WXHandler<T extends BaseReq, R extends BaseResp> {
        void handleOnReq(T req);

        R handleOnResp(BaseResp resp);
    }
}
