import webbrowser
import re
import os
import tempfile
from flox import Flox
from osrswiki import OSRSSearch, BASE_URL

WIKI_URL = 'https://oldschool.runescape.wiki/w/'

class OSRSSearcher(Flox):
    def __init__(self):
        self.OSRSWIKI = OSRSSearch()
        super().__init__()

    def query(self, query):
        results = self.OSRSWIKI.search(query)["query"]["search"]
        for result in results:
            openurl = f"{WIKI_URL}{result['title'].replace(' ', '_')}"
            self.add_item(
                title=result['title'].replace('"', ''),
                subtitle="Open OSRS Wiki",
                method='open_url',
                parameters=[openurl]
            )
        return
    
    def context_menu(self, data):
        pass

    def open_url(self, url):
        webbrowser.open(url)

if __name__ == "__main__":
    OSRSSearcher()