package com.miraclegarden.smsmessage.xposed;

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

import de.robv.android.xposed.XposedBridge;

public final class HookForwarder {

    private static final String TAG = "notiMessageHook";

    private HookForwarder() {
    }

    public static void forward(Context context, String packageName, String title,
                               String content, String source) {
        if (context == null || TextUtils.isEmpty(packageName) || TextUtils.isEmpty(content)) {
            return;
        }

        try {
            Intent intent = new Intent(HookBridge.ACTION_HOOK_MESSAGE);
            intent.setPackage(HookBridge.TARGET_APP_PACKAGE);
            intent.putExtra(HookBridge.EXTRA_PACKAGE_NAME, packageName);
            intent.putExtra(HookBridge.EXTRA_TITLE,
                    TextUtils.isEmpty(title) ? packageName : title);
            intent.putExtra(HookBridge.EXTRA_CONTENT, content);
            intent.putExtra(HookBridge.EXTRA_TIMESTAMP, System.currentTimeMillis());
            intent.putExtra(HookBridge.EXTRA_SOURCE, source);
            context.sendBroadcast(intent);
        } catch (Throwable t) {
            XposedBridge.log(TAG + " sendBroadcast failed: " + t.getMessage());
        }
    }

    /** 向主 App 汇报当前进程内 Hook 模块版本（用于检测是否最新） */
    public static void reportStatus(Context context, String hostPackage, String source) {
        if (context == null) {
            return;
        }
        try {
            Intent intent = new Intent(HookBridge.ACTION_HOOK_STATUS);
            intent.setPackage(HookBridge.TARGET_APP_PACKAGE);
            intent.putExtra(HookBridge.EXTRA_HOST_PACKAGE, hostPackage);
            intent.putExtra(HookBridge.EXTRA_SOURCE, source);
            intent.putExtra(HookBridge.EXTRA_MODULE_VERSION_CODE, HookBridge.MODULE_VERSION_CODE);
            intent.putExtra(HookBridge.EXTRA_MODULE_VERSION_NAME, HookBridge.MODULE_VERSION_NAME);
            intent.putExtra(HookBridge.EXTRA_TIMESTAMP, System.currentTimeMillis());
            context.sendBroadcast(intent);
        } catch (Throwable t) {
            XposedBridge.log(TAG + " reportStatus failed: " + t.getMessage());
        }
    }
}
