import pygame
import speech_recognition as sr
from openai import OpenAI
from pathlib import Path
import time
import os
import io
import soundfile as sf
import sounddevice as sd
import random

# Initialize OpenAI API key
api_key = 'sk-proj-435frgerrwwWaxim1Qt13uqzSS0xjT3BlbkFJK0rZvx78AJiWG3Ot7d3S'
client = OpenAI(api_key=api_key)

def create_messages(question, file_content):
    return [
        {"role": "system", "content": "Your name is Futurebot. You were created by Sooraj and team who develops innovative projects in IoT future lab at vodafone. You work with Tim, Sooraj and Priya along with other team members Laura, Sven, Thomas, Stephie. You are from T-E-T-I team. Your manager is Teja. You are a lab tour guide who explains and answers about IoT use cases in Vodafone. You have to create sentences and answer in few lines. Do not say greetings"},
        {"role": "user", "content": f"{file_content}\n\nQ: {question}\nA:"}
    ]

def play_audio(num):
    audio_files = {
        1: "speech1.mp3",
        2: "hydrosense.mp3",
        3: "Pushtotalk.mp3",
    }
    
    audio_file = audio_files.get(num)
    
    if audio_file:
        pygame.mixer.init()
        pygame.mixer.music.load(audio_file)
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy():
            time.sleep(1)

def recognize_speech():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        
        audio = recognizer.listen(source)
        try:
            print("Recognizing...")
            
            
            text = recognizer.recognize_google(audio, language='en-US')
            
            audio_files = ["ty.mp3", "th.mp3", "good.mp3", "hmm.mp3", "haha.mp3"]
            # Select a random audio file
            random_audio = random.choice(audio_files)
            
            # Load and play the selected random audio
            pygame.mixer.music.load(random_audio)
            pygame.mixer.music.play()
            
            print(f"You said: {text}")
            return text
        except sr.UnknownValueError:
            print("Sorry, I did not understand that.")
            return None
        except sr.RequestError:
            print("Sorry, there was an error with the speech recognition service.")
            return None

def get_response_from_openai(messages):
    stream = client.chat.completions.create(
        model="gpt-4o",
        max_tokens=150,
        temperature=0.5,
        messages=messages,
        stream=True,
    )
    for chunk in stream:
        if chunk.choices[0].delta.content is not None:
            yield chunk.choices[0].delta.content

def read_text_file(file_path):
    with open(file_path, 'r') as file:
        return file.read()

def generate_speech(text):
    if text.strip():  # Only generate speech if the text is not empty
        spoken_response = client.audio.speech.create(
            model="tts-1",
            voice="alloy",
            input=text
        )

        buffer = io.BytesIO()
        for chunk in spoken_response.iter_bytes(chunk_size=4096):
            buffer.write(chunk)
        buffer.seek(0)

        with sf.SoundFile(buffer, 'r') as sound_file:
            data = sound_file.read(dtype='int16')
            sd.play(data, sound_file.samplerate)
            sd.wait()

def start_qa_mode(file_content):
    while True:
        
        question = recognize_speech()

        if question and question.lower() in ["no", "go to the next showcase", "exit", "i don't have any questions", "i have no questions", "i don't have any other questions", "I don't have any other questions", "thats it"]:
            pygame.mixer.music.load("give.mp3")
            pygame.mixer.music.play()
            break

        if question:
            messages = create_messages(question, file_content)
            response_generator = get_response_from_openai(messages)
            print("Answer: ", end="")
            accumulated_response = ""
            for response_chunk in response_generator:
                accumulated_response += response_chunk
                if '.' in response_chunk or len(accumulated_response) > 500:  # Check for sentence end or length
                    print(accumulated_response, end="", flush=True)
                    generate_speech(accumulated_response)
                    accumulated_response = ""  # Reset accumulated response for the next chunk
            if accumulated_response:  # Generate speech for any remaining text
                print(accumulated_response, end="", flush=True)
                generate_speech(accumulated_response)
        else:
            print("Sorry, I didn't get that. Please ask again.")

def user_input():
    while True:
        try:
            num = int(input("Enter a number from 1 to 3: "))
            if 1 <= num <= 3:
                return num
            else:
                print("Invalid input. Please enter a number between 1 and 3.")
        except ValueError:
            print("Invalid input. Please enter a valid integer.")

def main():
    text_files = {
        1: "Gigabeeprotect.txt",
        2: "Hydrosense.txt",
        3: "Pushtotalk.txt",
    }

    while True:
        num = user_input()
        play_audio(num)
        file_content = read_text_file(text_files[num])
        start_qa_mode(file_content)

if __name__ == "__main__":
    main()
