import pyttsx3
import speech_recognition as sr 
import datetime
import wikipedia
import webbrowser
import os
import random
 

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[1].id)

randNo = random.randint(0 , 7)
i = randNo

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

def wishme():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak("Good Morning !")
    
    elif hour>=12 and hour<18:
        speak("Good After Noon !")
    
    else:
        speak("Good Evening !")

    speak(" I am Jarvis. How May I help you?")

def takeCommand():

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        # r.adjust_for_ambient_noise(source,duration=1)
        r.pause_threshold = 1 
        audio = r.listen(source)

    try:
        print("Recogninzing..")
        query = r.recognize_google(audio, language='en-in')
        print(f"User Said : {query} \n")

    except Exception as e:
        # print(e)
        print("Say that aagain please")
        speak("Sorry! Say that again please.")
        return "None"
    return query



if __name__ == '__main__':
    wishme()
    # while True :
    if 1:
        query = takeCommand().lower()

        if 'wikipedia' in query:
            speak('Searching Wikipedia...')
            query = query.replace("wikipedia", "")
            results = wikipedia.summary(query, sentences=2)
            speak("According to Wikipedia")
            print(results)
            speak(results)

        elif 'open youtube ' in query:
            webbrowser.open("youtube.com")
            speak(" Okay , Opening Youtube")
            
        elif 'open google' in query:
            webbrowser.open('google.com')
            speak(" Okay , Opening Google")

        elif 'open whatsapp' in query:
            webbrowser.open('https://web.whatsapp.com/')
            speak(" Okay , Opening Whatsapp")

        elif 'play music' in query:
            music_dir = 'D:\\adobe\\PYTHON\\JARVIS\\songs'
            songs = os.listdir(music_dir)
            # print(songs)
            os.startfile(os.path.join(music_dir,songs[i]))

        elif 'the time ' in query:
            strTime = datetime.datetime.now().strftime("%H:%M:%S")
            speak(f"The time now is {strTime}")
        
        elif 'open visual studio code' in query:
            codePath = "C:\\Users\\NIKHIL PC\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
            os.startfile(codePath)
            speak(" Okay , Opening VS code")

        elif 'open zoom' in query:
            zoomPath = "C:\\Users\\NIKHIL PC\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe"
            os.startfile(zoomPath)
            speak(" Okay , Opening zoom")

        elif 'open microsoft word' in query:
            msWord = "C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE"
            os.startfile(msWord)
            speak(" Okay , Opening MS word")
        
        
            

