"""

"""
import platform
from abc import abstractmethod

import api
from playwright.sync_api import Playwright, sync_playwright

SPLIT_CHAR = '\\' if platform.system() == 'Windows' else '/'


class BaseBrowser:
    def __init__(self, account: str, playwright=None):
        api.assert_not_none(account, "账号不能为空")
        self.account = account
        self.browser = None
        self.page = None
        self.result = None
        self.list_result = []
        self.map_result = {}
        self.has_more = False
        self.playwright = playwright

    def __init_browser__(self, playwright):
        if playwright:
            self.playwright = playwright
        else:
            self.playwright = sync_playwright().start()
        self.browser = self.playwright.chromium.launch_persistent_context(
            proxy=None,
            user_data_dir=f'.{SPLIT_CHAR}.data{SPLIT_CHAR}{self.__get_name__()}{SPLIT_CHAR}{self.account}',
            headless=False,
            slow_mo=1000,
            channel="chrome",
            ignore_https_errors=True,
            user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                       'Chrome/126.0.0.0 Safari/537.36',
            args=[
                '--disable-blink-features=AutomationControlled',
                # '--incognito',
                '--ignore-certificate-errors-spki-list',
                '--disable-web-security',  # 禁用 Web 安全性,类似于 ChromeOptions 中的 --ignore-certificate-errors-spki-list
                '--no-sandbox',  # 禁用沙盒模式
                '--disable-dev-shm-usage',  # 禁用/dev/shm使用
                '--disable-features=site-per-process',  # 禁用每个站点的进程,类似于 ChromeOptions 中的 --no-sandbox
                '--ignore-certificate-errors',  # 忽略证书错误
                '--disable-features=AutomationControlled'  # 禁用与自动化相关的特性
            ])
        self.browser.add_init_script(path="./stealth.min.js")
        self.page = self.browser.pages[0]

    def close(self):
        self.browser.close()
        self.playwright.stop()

    @abstractmethod
    def __get_name__(self):
        pass