package com.miraclegarden.smsmessage.xposed.hook;

import android.content.ContentValues;
import android.content.Context;
import android.text.TextUtils;

import com.miraclegarden.smsmessage.xposed.HookBridge;
import com.miraclegarden.smsmessage.xposed.HookForwarder;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public final class WeChatMessageHook {

    private static final String TAG = "notiMessageHook/WeChat";
    private static final String WCDB_CLASS = "com.tencent.wcdb.database.SQLiteDatabase";

    private WeChatMessageHook() {
    }

    public static void install(XC_LoadPackage.LoadPackageParam lpparam) {
        try {
            hookWcdbInsert(lpparam, "insert");
            hookWcdbInsert(lpparam, "insertWithOnConflict");
            XposedBridge.log(TAG + " installed for " + lpparam.packageName);
        } catch (Throwable t) {
            XposedBridge.log(TAG + " install failed: " + t.getMessage());
        }
    }

    private static void hookWcdbInsert(XC_LoadPackage.LoadPackageParam lpparam, String methodName) {
        XposedHelpers.findAndHookMethod(
                WCDB_CLASS,
                lpparam.classLoader,
                methodName,
                String.class,
                String.class,
                ContentValues.class,
                new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) {
                        handleWeChatInsert(param);
                    }
                }
        );
    }

    private static void handleWeChatInsert(XC_MethodHook.MethodHookParam param) {
        if (param.args == null || param.args.length < 3) {
            return;
        }

        Object tableArg = param.args[0];
        Object valuesArg = param.args[2];
        if (!(tableArg instanceof String) || !(valuesArg instanceof ContentValues)) {
            return;
        }

        String table = (String) tableArg;
        if (!"message".equalsIgnoreCase(table)) {
            return;
        }

        ContentValues values = (ContentValues) valuesArg;
        String talker = values.getAsString("talker");
        String content = values.getAsString("content");
        Integer type = values.getAsInteger("type");

        if (TextUtils.isEmpty(content)) {
            return;
        }
        if (type != null && (type == 10000 || type == 10002)) {
            return;
        }

        Context context = getContext();
        if (context == null) {
            return;
        }

        HookForwarder.forward(context, "com.tencent.mm", talker, content,
                HookBridge.SOURCE_XPOSED_WECHAT);
    }

    private static Context getContext() {
        try {
            Class<?> activityThread = XposedHelpers.findClass("android.app.ActivityThread", null);
            Object app = XposedHelpers.callStaticMethod(activityThread, "currentApplication");
            if (app instanceof Context) {
                return (Context) app;
            }
        } catch (Throwable ignored) {
        }
        return null;
    }
}
