import pytest
from helper.util import get_driver, sleep, stop_browser_app, get_all_headers
from fp_browser_sdk.ext.performance import Performance, PerformanceMatch
from fp_browser_sdk.fp_browser_settings import FPBrowserSettings
from fp_browser_sdk.ext.basic_match import BasicMatch
from fp_browser_sdk.ext.basic import Basic
from fp_browser_sdk.ext.navigator import Navigator
from fp_browser_sdk.ext.browser_enum import BaseEnum, TLSVersion, Platform, PrefersColor, \
    CompatMode, MatchType, SpeechSynthesisVoiceAppendMode, PerformanceNavigationType, DoNotTrackType, MaxTouchPoint, \
    ScreenColorDepth, ScreenOrientationType, MediaKind, WebEffectiveConnectionType, WebConnectionType
import random
import re


class TestBasicMatch(object):
    def setup_method(self):
        stop_browser_app()

    # @pytest.mark.skip()
    @pytest.mark.parametrize('value', [
        (
                "https://ly.so.com/",
                0,
                "Mozilla/5.0 (Linux; Android 10; Redmi Note 9 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.9999.0 Mobile Safari/537.36",
        )
    ])
    def test_user_agent(self, value):
        """
        测试全局匹配
        """
        custom_url, index, user_agent = value

        settings = FPBrowserSettings()

        basic = Basic() \
            .set_info_number("42.0.2311.60") \
            .append_match_list(BasicMatch()
                               .set_target_url("https://1024tools.com/header") \
                               .set_match_break(True) \
                               .set_user_agent(user_agent) \
                               .set_match_type(MatchType.PATH)
                               )

        settings.add_module(basic)

        navigator = Navigator()

        settings.add_module(navigator)

        driver, config = get_driver(custom_url="https://1024tools.com/header", custom_config=settings.parse(),
                                    only_custom_config=True)
        self.driver = driver
        self.config = config
        self.current = self.config.get("basic.match-json")[index]

        script = '''
              function func(el)
              {
                 return navigator.userAgent
              }
              return func();
        '''
        value = self.driver.execute_script(script)
        setting_value = self.merge_ua(self.current['user_agent'], self.config.get("version-info.number"))

        print("Navigator User-Agent:", value, setting_value)
        assert value == setting_value

        # 获得全部 header
        result = get_all_headers(driver=self.driver)
        value = result.get('user-agent')
        setting_value = self.merge_ua(self.current['user_agent'], self.config.get("version-info.number"))

        print("Header 设置 User-Agent:", value, setting_value)
        assert value == setting_value

        self.driver.close()
        self.driver.quit()

    def merge_ua(self, ua, version):
        """
        替换 ua
        """
        result = re.search(r'Chrome/([\d|\.]+)', ua)

        return ua.replace(result.group(1), version)
