'''
Created on 12-Jul-2021

@author: rishi_priyansh
'''
import inspect
import traceback
import random
from xv.util import convertToUtf8
import re
from bs4 import BeautifulSoup as BS
import requests
import urllib.parse

# _overview_helper
# OverviewHelper

from xv.util import UtilClass

class OverviewHelper(UtilClass):
    '''
    '''
    # url containing index of Physics formulae
    _PHYSICS_FORMULAE_URL = "https://johanw.home.xs4all.nl/physics_html/"
    _PHYSICS_FORMULAE_NODES = None
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._physics_formulae_nodes = None
        pass
    
    def get_physics_formulae_nodes(self, url = None): 
        if self._physics_formulae_nodes is None:
            self._physics_formulae_nodes = self._get_physics_formulae_nodes(url = url)
        return self._physics_formulae_nodes
        
    def _get_physics_formulae_nodes(self, url = None): 
        url = self._parse_url(url)
        a_nodes = []
        
        r = requests.get(url)
        if not r:
            return a_nodes
        
        soup = BS(r.content, 'lxml') 
        if not soup:
            return a_nodes
    
        ol_nodes = soup.find_all("ol")
        #print(f"ol_nodes = {len(ol_nodes)}")
    
        for ol_node in ol_nodes:
            temp_nodes = ol_node.find_all("a", href=True)
            if temp_nodes:
                a_nodes.extend(temp_nodes)
            pass
        return a_nodes
        pass
    
    def _parse_url(self, url = None):
        if url is None:
            url = self.__class__._PHYSICS_FORMULAE_URL
        return url
        pass

    def search_links(self, search, url = None):
        search_lower = search.lower()
        
        url = self._parse_url(url)
        a_nodes = self.get_physics_formulae_nodes(url = url)
        if not a_nodes:
            return None
        
        search_result = set()
        sep = "###"
        for a_node in a_nodes:
            qText = convertToUtf8(a_node.get_text().strip())
            if search_lower in qText.lower():
                href = urllib.parse.urljoin(url, a_node['href'])
                search_result.add(f"{qText}{sep}{href}")
            pass
        
        search_result = [x.split(sep) for x in search_result]
        return search_result
        pass
    
    pass
#class OverviewHelper:

    