/*
package cn.sikey.selenium.util;

import cn.hutool.http.HttpStatus;
import cn.sikey.framework.common.exception.ServiceException;
import com.microsoft.playwright.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

*/
/**
 * @Author: nelson
 * @Date: 2025/6/28
 * @Description: 基于 Playwright 的会话管理
 *//*

public class PlaywrightContextManagerUtil {

    static {
        // 在类加载时第一时间设置（优先级最高）
        System.setProperty("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD", "1");
        System.setProperty("PLAYWRIGHT_DISABLE_INSTALLATION_METRICS", "1");
    }

    private static final Map<String, PlaywrightSession> sessionMap = new ConcurrentHashMap<>();
    private static final long DEFAULT_TIMEOUT = 30;

    // Playwright 实例（单例）
    private static  Playwright playwright;

    // 创建新的 Playwright 会话
    public static String createSession(String wsEndpoint) {
        return createSession(wsEndpoint, DEFAULT_TIMEOUT);
    }

    public static String createSession(String wsEndpoint, long timeoutSeconds) {
        String sessionId = generateSessionId();

        // 初始化 Playwright（单例模式）
        if (playwright == null) {
            Playwright.CreateOptions options = new Playwright.CreateOptions()
                    .setEnv(Map.of("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD", "1","PLAYWRIGHT_DISABLE_INSTALLATION_METRICS", "1")); // 跳过浏览器下载
            playwright = Playwright.create(options);
        }

        // 连接远程浏览器实例
        BrowserType browserType = playwright.chromium().connect(wsEndpoint).browserType();

        // 创建浏览器实例
        Browser browser = browserType.launch(new BrowserType.LaunchOptions()
                .setHeadless(true)
                .setTimeout(timeoutSeconds * 1000));

        // 创建浏览器上下文（会话隔离）
        BrowserContext context = browser.newContext();

        // 创建页面
        Page page = context.newPage();

        sessionMap.put(sessionId, new PlaywrightSession(browser, context, page));
        return sessionId;
    }

    // 获取现有会话
    public static PlaywrightSession getSession(String sessionId) {
        PlaywrightSession session = sessionMap.get(sessionId);
        if (session == null) {
            throw new ServiceException(HttpStatus.HTTP_INTERNAL_ERROR, "无效的会话ID");
        }
        return session;
    }

    // 销毁会话
    public static void destroySession(String sessionId) {
        PlaywrightSession session = sessionMap.remove(sessionId);
        if (session != null) {
            session.close();
        }
    }

    // 销毁所有会话和Playwright实例
    public static void shutdown() {
        sessionMap.values().forEach(PlaywrightSession::close);
        sessionMap.clear();

        if (playwright != null) {
            playwright.close();
            playwright = null;
        }
    }

    // 生成唯一会话ID
    private static String generateSessionId() {
        return "SID_" + System.currentTimeMillis() + "_" + (int) (Math.random() * 1000);
    }

    // 会话数据封装类
    public static class PlaywrightSession {
        private final Browser browser;
        private final BrowserContext context;
        private final Page page;

        public PlaywrightSession(Browser browser, BrowserContext context, Page page) {
            this.browser = browser;
            this.context = context;
            this.page = page;
        }

        public Browser getBrowser() {
            return browser;
        }

        public BrowserContext getContext() {
            return context;
        }

        public Page getPage() {
            return page;
        }

        public void close() {
            if (page != null && !page.isClosed()) {
                page.close();
            }
            if (context != null) {
                context.close();
            }
            if (browser != null) {
                browser.close();
            }
        }
    }
}*/
