from .imports import *
import json,re,urllib,webbrowser
from urllib.parse import urlparse, parse_qs, parse_qsl
from .seleneum import *
from abstract_utilities import get_home_dir
def determine_matching_dict(dict_og,dict_comp):
    for key,value in dict_og.items():
        if key not in dict_comp or dict_comp.get(key) != value:
            return False
    return True
def is_header_matching(headers_list,headers_comp):
    for headers in headers_list:
        if determine_matching_dict(headers,headers_comp):
            return True
    return False
def get_unique_headers_list(existing_headers, new_headers):
    result = list(existing_headers)  # copy
    for candidate in new_headers:
        if not is_header_matching(result, candidate):
            result.append(candidate)
    return result

def get_approved_headers_file_path(directory=None,file_path=None,basename=None):
    basename = basename or 'approved_headers.json'
    if file_path:
        return file_path
    if directory and os.path.isdir(directory):
        file_path = os.path.join(directory,basename)
    else:
        file_path = os.path.join(get_home_dir(),basename)
    return file_path
def save_approved_headers_dict(data,directory=None,file_path=None,basename=None):
    approved_headers_file_path = get_approved_headers_file_path(directory=directory,file_path=file_path,basename=basename)
    safe_dump_to_json(data=data,file_path=approved_headers_file_path)
def update_approved_headers(obj, url=None, directory=None, file_path=None, basename=None):
    approved_headers_dict = get_approved_headers_dict(
        directory=directory,
        file_path=file_path,
        basename=basename
    )
    if isinstance(obj, list):
        if not url:
            raise ValueError("url is required when obj is a list")
        domain = urlManager(url).parsed.get("full_domain")
        obj = {domain: obj}

    for domain, values in obj.items():
        current = approved_headers_dict.get(domain, [])
        approved_headers_dict[domain] = get_unique_headers_list(current, values)

    save_approved_headers_dict(
        approved_headers_dict,
        directory=directory,
        file_path=file_path,
        basename=basename
    )
def get_approved_headers_dict(directory=None,file_path=None,basename=None):
    approved_headers_file_path = get_approved_headers_file_path(directory=directory,file_path=file_path,basename=basename)
    if not os.path.isfile(approved_headers_file_path):
        safe_dump_to_json(data={},file_path=approved_headers_file_path)
    return safe_load_from_json(approved_headers_file_path)
def get_approved_headers_for_domain(url):
    domain = urlManager(url).parsed.get("domain")
    approved_headers_dict = get_approved_headers_dict()
    return approved_headers_dict.get(domain, [])
def get_approved_header_for_domain(url,restricted_list=None):
    restricted_list = restricted_list or []
    approved_headers = get_approved_headers_for_domain(url)
    for approved_header in approved_headers:
        if not is_header_matching(approved_header,restricted_list):
            return approved_header
    return derive_approved_headers_for_url(url,return_first_found=True,restricted_list=restricted_list)
def get_check_approved_header(url):
    if not is_header_matching(user_agent_list,headers) and is_header_valid_for_url(url,user_agent,headers):
        user_agent_list.append(headers)
        update_approved_headers(user_agent_list,url=url)
def is_valid_request_response(source_code):
    text = str(source_code)
    txts = [
        "not a bot",
        'update your browser',
        'Bitte aktualisiere deinen BrowserDein Browser',
        'browser is no longer supported',
        "Dein Browser"
        ]
    for txt in txts:
        if txt not in text:
            return False
    return True
def is_header_valid_for_url(url,user_agent,headers,session):
    source_code = fetch_html(
                url=url,
                session=session,
                headers=headers,
                user_agent=user_agent
            )
    if is_valid_request_response(source_code):
        return source_code
def generate_ua_and_headers(url):
    ua_mgr = UserAgentManager(randomAll=True)
    user_agent = ua_mgr.get_user_agent()
    headers = ua_mgr.generate_headers()
    return user_agent,headers 
def generate_ua_and_headers_session(url):
    user_agent,headers = generate_ua_and_headers(url)
    session = requests.Session()
    return user_agent,headers ,session
def derive_approved_headers_for_url(url,return_first_found=True,restricted_list=None):
    restricted_list=restricted_list or []
    user_agent_list = get_approved_headers_for_domain(url)
    while True:
        user_agent,headers,session = generate_ua_and_headers_session(url)
        header_matching = is_header_matching(user_agent_list+restricted_list,headers)
        if not header_matching:
            source_code = is_header_valid_for_url(url,user_agent,headers,session)
            if source_code:
                user_agent_list.append(headers)
                update_approved_headers(user_agent_list,url=url)
                if return_first_found:
                    return headers
def derive_approved_headers_user_agent_session_for_url(url,return_first_found=True,restricted_list=None):
    restricted_list=restricted_list or []
    user_agent_list = get_approved_headers_for_domain(url)
    while True:
        user_agent,headers,session = generate_ua_and_headers_session(url)
        header_matching = is_header_matching(user_agent_list+restricted_list,headers)
        if not header_matching:
            source_code = is_header_valid_for_url(url,user_agent,headers,session)
            if source_code:
                user_agent_list.append(headers)
                update_approved_headers(user_agent_list,url=url)
                if return_first_found:
                    return user_agent,headers,session,source_code
