package com.tagmae.tsgame_erwang;

import java.io.IOException;

/**
 * 新的HTTP服务器实例管理类，使用NanoHTTPD替换AndServer
 * 作者：GitHub Copilot
 * 日期：2025/6/18
 */
public class NewWebServerInstance {
    private OkHttpPhotoServer server;
    private static NewWebServerInstance instance;
    private int port;
    
    public static NewWebServerInstance getInstance() {
        if (null == instance) {
            instance = new NewWebServerInstance();
        }
        return instance;
    }
    
    public void create(int port) {
        this.port = port;
        if (server != null) {
            stop();
        }
        
        server = new OkHttpPhotoServer(port);
        try {
            server.start();
            System.out.println("HTTP服务器启动成功，端口：" + port);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("HTTP服务器启动失败：" + e.getMessage());
        }
    }
    
    public boolean isRunning() {
        return server != null && server.isAlive();
    }
    
    public void stop() {
        if (server != null) {
            server.stop();
            server = null;
            System.out.println("HTTP服务器已停止");
        }
    }
    
    public String getServerUrl(String ip) {
        if (isRunning()) {
            return "http://" + ip + ":" + port + "/testurl";
        }
        return null;
    }
}
