import webbrowser
import os
import speech_recognition as sr
import pyttsx3


class Doraemon:
    """This is a simple speech command bot. It is made with few functions and elif ladder. It can take speech input.
    It will perform two categories of tasks:
        1. searching through the web.
        2. opening some apps. Both on voice command. It will also reply to user by talking.
    It has four major methods:
        1. __init__: which is an empty constructor
        2. speak: that speaks the text input and also prints it on the console.
        3. takeCommand: It listens with the default microphone of the system and returns the spoken text
        4. run_bot: This is the interface user will get in the instance of this class. It has an infinite while loop to take commands and execute them until the user asks to stop.
    Libraries used:
        webbrowser: to browse through web
        os: to open applications
        speech_recognition: to take voice commands
        pyttsx3: to give voice outputs
    """
    def __init__(self):
        pass

    def speak(self, text:str) -> None:
        engine = pyttsx3.init()
        print(text)
        engine.say(text)
        engine.runAndWait()
        return None
    
    def takeCommand(self) -> str:
        r = sr.Recognizer()
        with sr.Microphone() as source:
            print("Listening...")
            r.pause_threshold = 1
            audio = r.listen(source)
        try:
            print("Recognizing...")    
            query = r.recognize_google(audio, language='en-in')
            return query

        except Exception:
            print("Say that again please...")
            return ""
    
    def open_youtube(self) -> None:
        self.speak("What should I search for? ")
        cm = input()
        self.speak(f"Searching in youtube {cm}")
        webbrowser.open(f"https://www.youtube.com/results?search_query={cm}")

    def open_google(self) -> None:
        self.speak("What should I search for? ")
        cm = input()
        self.speak(f"Searching in google {cm}")
        webbrowser.open(f"https://www.google.com/search?q={cm}")

    def open_notepad(self) -> None:
        self.speak("Opening Notepad")
        os.system("C:\\Windows\\notepad.exe")

    def open_vs_code(self) -> None:
        self.speak("Opening VS Code")
        os.startfile("C:\\Microsoft VS Code2\\Code.exe")

    def run_bot(self) -> None:
        self.speak("How can i help you buddy?")
        while True:
            instruction = self.takeCommand().lower()
            if instruction == "":
                continue
            elif "youtube" in instruction:
                self.open_youtube()

            elif "google" in instruction:
                self.open_google()

            elif "notepad" in instruction:
                self.open_notepad()

            elif "vs code" in instruction:
                self.open_vs_code()

            elif "quit" or "exit" in instruction:
                self.speak("Ok bye bye have a great time")
                break

            elif "thanks" in instruction:
                self.speak("NO thanks in friendship")
                
            else:
                self.speak("Invalid request")

my_buddy = Doraemon()
my_buddy.run_bot()