package ai.z.chat;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.getcapacitor.annotation.Permission;

import android.content.Intent;
import android.provider.Settings;

@CapacitorPlugin(
    name = "AutoGLM",
    permissions = {}
)
public class AutoGLMPlugin extends Plugin {

    @PluginMethod
    public void isEnabled(PluginCall call) {
        call.resolve(new JSObject().put("enabled", AutoGLMService.isEnabled()));
    }

    @PluginMethod
    public void openSettings(PluginCall call) {
        try {
            Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getContext().startActivity(intent);
            call.resolve(new JSObject().put("opened", true));
        } catch (Exception e) {
            call.reject("Cannot open accessibility settings: " + e.getMessage());
        }
    }

    @PluginMethod
    public void tap(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        int x = call.getInt("x", 0);
        int y = call.getInt("y", 0);
        svc.tap(x, y);
        call.resolve(new JSObject().put("ok", true));
    }

    @PluginMethod
    public void longPress(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        int x = call.getInt("x", 0);
        int y = call.getInt("y", 0);
        svc.longPress(x, y);
        call.resolve(new JSObject().put("ok", true));
    }

    @PluginMethod
    public void swipe(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        int startX = call.getInt("startX", 0);
        int startY = call.getInt("startY", 0);
        int endX = call.getInt("endX", 0);
        int endY = call.getInt("endY", 0);
        int duration = call.getInt("duration", 300);
        svc.swipe(startX, startY, endX, endY, duration);
        call.resolve(new JSObject().put("ok", true));
    }

    @PluginMethod
    public void typeText(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        String text = call.getString("text", "");
        if (text.isEmpty()) { call.reject("text required"); return; }
        svc.typeText(text);
        call.resolve(new JSObject().put("ok", true));
    }

    @PluginMethod
    public void pressBack(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        svc.pressBack();
        call.resolve(new JSObject().put("ok", true));
    }

    @PluginMethod
    public void pressHome(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        svc.pressHome();
        call.resolve(new JSObject().put("ok", true));
    }

    @PluginMethod
    public void pressRecents(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        svc.pressRecents();
        call.resolve(new JSObject().put("ok", true));
    }

    @PluginMethod
    public void pressNotifications(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        svc.pressNotifications();
        call.resolve(new JSObject().put("ok", true));
    }

    @PluginMethod
    public void pressQuickSettings(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        svc.pressQuickSettings();
        call.resolve(new JSObject().put("ok", true));
    }

    @PluginMethod
    public void takeScreenshot(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.R) {
            call.reject("Screenshot requires Android 11+");
            return;
        }
        String destPath = call.getString("dest", "");
        if (destPath.isEmpty()) {
            destPath = getContext().getCacheDir() + "/autoglm_screenshot.png";
        }
        svc.takeScreenshot(destPath);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        call.resolve(new JSObject().put("path", destPath).put("ok", true));
    }

    @PluginMethod
    public void getUITree(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        String tree = svc.getUITree();
        call.resolve(new JSObject().put("tree", tree).put("ok", true));
    }

    @PluginMethod
    public void getFocusedNode(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        String info = svc.getFocusedNodeInfo();
        call.resolve(new JSObject().put("node", info).put("ok", true));
    }

    @PluginMethod
    public void clickNode(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        String viewId = call.getString("viewId", "");
        if (viewId.isEmpty()) { call.reject("viewId required"); return; }
        boolean result = svc.clickNode(viewId);
        call.resolve(new JSObject().put("ok", result));
    }

    @PluginMethod
    public void clickByText(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        String text = call.getString("text", "");
        if (text.isEmpty()) { call.reject("text required"); return; }
        boolean result = svc.clickNodeByText(text);
        call.resolve(new JSObject().put("ok", result));
    }

    @PluginMethod
    public void scrollNode(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        String viewId = call.getString("viewId", "");
        String direction = call.getString("direction", "forward");
        if (viewId.isEmpty()) { call.reject("viewId required"); return; }
        int action = direction.equals("forward")
            ? android.view.accessibility.AccessibilityNodeInfo.ACTION_SCROLL_FORWARD
            : android.view.accessibility.AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
        boolean result = svc.scrollNode(viewId, action);
        call.resolve(new JSObject().put("ok", result));
    }

    @PluginMethod
    public void getCurrentApp(PluginCall call) {
        AutoGLMService svc = AutoGLMService.getInstance();
        if (svc == null) { call.reject("AutoGLM service not enabled"); return; }
        String pkg = svc.getCurrentApp();
        call.resolve(new JSObject().put("package", pkg).put("ok", true));
    }

    @PluginMethod
    public void launchApp(PluginCall call) {
        String pkg = call.getString("package", "");
        if (pkg.isEmpty()) { call.reject("package required"); return; }
        try {
            Intent intent = getContext().getPackageManager().getLaunchIntentForPackage(pkg);
            if (intent != null) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getContext().startActivity(intent);
                call.resolve(new JSObject().put("ok", true));
            } else {
                call.reject("Package not found: " + pkg);
            }
        } catch (Exception e) {
            call.reject("Launch failed: " + e.getMessage());
        }
    }
}
