package cn.waterpolo.network;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import android.util.Log;
@CapacitorPlugin(name = "NetWork")
public class NetWorkPlugin extends Plugin implements NetWork.NetWorkListener {
    private NetWork implementation;
    private String currentType = null;
    @Override
    public void load() {
        super.load();
        // [修复] 在插件加载时获取单例并注册自己为监听器
        this.implementation = NetWorkManager.getInstance(getContext());
        this.implementation.setListener(this);
        Log.d("NetWorkPlugin", "Plugin loaded and listener registered.");
    }
    @Override
    protected void handleOnResume() {
        super.handleOnResume();
        // [修复] APP恢复时，再次确保监听器是自己
        if (this.implementation != null) {
            this.implementation.setListener(this);
            Log.d("NetWorkPlugin", "Plugin resumed and listener re-registered.");
        }
    }
    @Override
    protected void handleOnDestroy() {
        super.handleOnDestroy();
        // [修复] 插件销毁时，可以考虑解绑监听器，但不是必须，因为用了弱引用
        // 如果 implementation 是非空，说明它还活着
        if (this.implementation != null) {
            // this.implementation.setListener(null);
        }
        Log.d("NetWorkPlugin", "Plugin destroyed.");
    }
    // [修复] 实现接口方法，将事件转发给前端
    @Override
    public void onEvent(String event, JSObject data) {
        notifyListeners(event, data, true);
    }
    @PluginMethod
    public void connect(PluginCall call) {
        String type = call.getString("type");
        String ip = call.getString("ip");
        int port = call.getInt("port", -1);

        // [修复] 不再传递 bridge 和 this
        currentType = type;
        if ("tcp".equals(type)) {
            implementation.createTcpConnection(ip, port);
        } else if ("udp".equals(type)) {
            implementation.createUdpConnection(port);
        } else {
            call.reject("Unknown type: " + type);
            return;
        }
        call.resolve();
    }

    @PluginMethod
    public void send(PluginCall call) {
        if (currentType == null) {
            call.reject("Not connected");
            return;
        }
        String data = call.getString("data");
        if ("tcp".equals(currentType)) {
            implementation.sendTcpData(data);
        } else if ("udp".equals(currentType)) {
            String ip = call.getString("ip", "255.255.255.255");
            String type = call.getString("udpType", "broadcast");
            implementation.sendUdpData(data, ip, type);
        }
        call.resolve();
    }

    @PluginMethod
    public void disconnect(PluginCall call) {
        if ("tcp".equals(currentType)) {
            implementation.closeTcp();
        } else if ("udp".equals(currentType)) {
            implementation.closeUdp();
        }
        currentType = null;
        call.resolve();
    }


    @PluginMethod
    public void getIpInfo(PluginCall call) {
        try {
            // [修复] 不再传入Context
            String ipAddress = implementation.getIpInfo();

            JSObject ret = new JSObject();
            ret.put("ipv4", ipAddress);
            call.resolve(ret);
        } catch (Exception e) {
            call.reject("获取IP地址失败", e);
        }
    }

    // 事件推送由NetWork调用notifyEvent
    public void notifyEvent(String event, JSObject data) {
        notifyListeners(event, data, true);
    }
}
