import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton, QLabel, QVBoxLayout, QWidget, QGraphicsOpacityEffect, QGraphicsScene, QGraphicsView
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtCore import QUrl, Qt
import speech_recognition as sr
from PyQt5.QtGui import QPainter



class HospitalChatbot:
    def __init__(self):


        self.intents_and_questions = {
            
            "greet":  [
                "hey",
                "hello",
                "Hello?",
                "hello?",
                "hi",
                "hello there",
                "good morning",
                "good evening",
                "moin",
                "hey there",
                "let's go",
                "hey dude",
                "goodmorning",
                "goodevening",
                "good afternoon"],
    

            "goodbye": [

                "cu",
                "good by",
                "cee you later",
                "good night",
                "bye",
                "goodbye",
                "have a nice day",
                "see you around",
                "bye bye",
                "see you later"

            ],


            "affirm": [

                "yes",
                "y",
                "indeed",
                "of course",
                "that sounds good",
                "correct"

            ],


            "bot_challenge": [

                "are you a bot",
                "are you a human",
                "am I talking to a bot",
                "am I talking to a human"

            ],

            "appointment_booking": [

                "How can I schedule an appointment",
                "I need to book an appointment with a doctor",
                "what's the procedure for booking an appointment",
                "hello, I'd like to schedule an appointment.",
                "book an appointment",
                "appointment",
                "need an appointment"
            ],


            "doctor_information": [

                "tell me more about dr. abc",
                "what's the specialty of dr. abc",
                "give me the contact details for dr. abc"
                "doctor",
                "contact doctor",
                "doctor contact",
                "doctor detail",
                "detail doctor"

            ],


            "department_information": [

                "which departments are available in the hospital",
                "can you provide details about the cardiology department",
                "tell me more about the pediatric department",
                "department",
                "which department",
                "department info"

            ],


            "visiting_hours":[

                "what are the visiting hours for patients",
                "when can I visit a patient in the hospital",
                "is there a specific time for visiting hours",
                "visiting time",
                "visit",
                "time",
                "time to visit",
                "visit time",
                "hospital visit",
                "visit hospital"
            ],


            "location_and_directions":[

                "how do I get to the hospital",
                "can you provide me with directions to your location",
                "where is the hospital situated",
                "direction",
                "direction hospital",
                "hospital direction",
                "location",
                "hospital location"
            ],


            "billing_and_insurance": [

                "how do I pay my medical bills",
                "do you accept my insurance",
                "what's the billing process for a hospital stay",
                "bills",
                "insurance",
                "pay bill",
            ],


            "medical_records": [
                "how can I access my medical records",
                "request my medical history",
                "I need a copy of my lab results",
                "records",
                "medical records",
                "record medical",
                "results",
                "lab results",
                "result copy",
                "copy result"
            ],


            "emergency_services": [
                "what should I do in case of a medical emergency",
                "how do I contact the hospital in an emergency",
                "tell me about your emergency services",
                "emergency",
                "emergency service"
            ],


            "covid19_information": [
                "what safety measures are in place due to COVID-19",
                "is it safe to visit the hospital during the pandemic",
                "do you offer COVID-19 testing or vaccinations",
                "covid19"

            ],


            "feedback_and_complaints": [
                "I want to provide feedback about my experience",
                "how can I file a complaint about a staff member",
                "share my thoughts on my recent visit",
                "feedback",
                "complaints",
                "complain",
                "share thoughts",
                "experiance"
            ],


            "thank_you": [
                "thank you for your help",
                "I appreciate your assistance",
                "you've been very helpful"
            ],


            "general_information": [
                "tell me more about the hospital",
                "what services do you offer",
                "is there a cafeteria in the hospital",
                "information",
                "cafeteria",
                "services",
                "services offered",
                "offered services"
            ]
        }

        self.intent_responses = {

            "greet": "Hello! How can I assist you today?",
            "appointment_booking": "To book an appointment, please call our scheduling department at 1234567897.",
            "doctor_information": "Dr. ABC is a specialist in Neuro. You can contact them at 9874563214.",
            "department_information": "Our hospital has various departments, including cardiology and pediatrics. How can I help you with a specific department?",
            "visiting_hours": "Visiting hours for patients are from 9:30 AM to 8:00 PM. Please feel free to visit during that time.",
            "location_and_directions": "Our hospital is located at Ameerpet Hyderabad. Here are directions to our hospital: [Directions].",
            "billing_and_insurance": "You can pay your medical bills at the billing department. We accept a variety of insurance plans. If you have any specific questions about billing, please call 789654125.",
            "medical_records": "To access your medical records, please visit the medical records department. They can provide you with a copy of your records or lab results.",
            "emergency_services": "In case of a medical emergency, please call 911 or go to the nearest emergency room. We also have an emergency department at our hospital.",
            "covid19_information": "For information on our COVID-19 safety measures and testing, please visit our website or contact our COVID-19 hotline at [COVID-19 Hotline Number].",
            "feedback_and_complaints": "Your feedback is valuable to us. You can provide feedback or file a complaint on our website or by contacting our patient services department.",
            "thank_you": "You're welcome! If you have any more questions, feel free to ask.",
            "general_information": "Our hospital provides a range of services, including medical, surgical, and emergency care. We also have a cafeteria for your convenience."
        
            
        }
        
        


    def match_intent(self, sentence):
        sentence = sentence.lower()  # Convert to lowercase for case-insensitive matching
        for intent, questions in self.intents_and_questions.items():
            for question in questions:
                if question in sentence:
                    return intent
        return None

    def get_response(self, sentence):
        matched_intent = self.match_intent(sentence)
        if matched_intent:
            response = self.intent_responses.get(matched_intent, "I'm not sure how to answer that.")
        else:
            response = "I'm not sure how to answer that."
        return response

    def get_video_response(self, intent):
        # Your video URLs here

        '''video_urls = {

            "greet": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260372/Output/greet_a1ngxv.mp4",
            "appointment_booking": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260367/Output/appointment_booking_hl5uvy.mp4",
            "doctor_information": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260369/Output/doctor_information_dq34py.mp4",
            "department_information": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260373/Output/department_information_d7fksd.mp4",
            "visiting_hours": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260376/Output/visiting_hours_dzvh3d.mp4",
            "location_and_directions": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260375/Output/location_and_directions_ktkluz.mp4",
            "billing_and_insurance": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260370/Output/billing_and_insurance_ba77n4.mp4",
            "medical_records": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260377/Output/medical_records_akgmlx.mp4",
            "emergency_services": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260369/Output/emergency_services_et3eyl.mp4",
            "covid19_information": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260371/Output/covid19_information_orlff0.mp4",
            "feedback_and_complaints": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260371/Output/feedback_and_complaints_i3ftal.mp4",
            "thank_you": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260374/Output/thank_you_sgx29d.mp4",
            "general_information": "https://res.cloudinary.com/dam12ojlp/video/upload/v1698260375/Output/general_information_nz2zx1.mp4"
        
            
        }'''

        video_urls = {

            "greet": "/home/knight/ChatBot_UI/video/greet.mp4",
            "appointment_booking": "/home/knight/ChatBot_UI/video/appointment_booking.mp4",
            "doctor_information": "/home/knight/ChatBot_UI/video/doctor_information.mp4",
            "department_information": "/home/knight/Python_Prog/UI/video/department_information.mp4",
            "visiting_hours": "/home/knight/ChatBot_UI/video/visiting_hours.mp4",
            "location_and_directions": "/home/knight/ChatBot_UI/video/location_and_directions.mp4",
            "billing_and_insurance": "/home/knight/ChatBot_UI/video/billing_and_insurance.mp4",
            "medical_records": "/home/knight/ChatBot_UI/video/medical_records.mp4",
            "emergency_services": "/home/knight/ChatBot_UI/video/emergency_services.mp4",
            "covid19_information": "/home/knight/ChatBot_UI/video/covid19_information.mp4",
            "feedback_and_complaints": "/home/knight/ChatBot_UI/video/feedback_and_complaints.mp4",
            "thank_you": "/home/knight/ChatBot_UI/video/thank_you.mp4",
            "general_information": "/home/knight/ChatBot_UI/video/general_information.mp4"
        
        
        }
        
        return video_urls.get(intent, "Video not available for this intent")
    


class HospitalChatbotGUI(QMainWindow):
    def __init__(self):
        super().__init__()

        
        self.chatbot = HospitalChatbot()

        self.setWindowTitle("Hospital Chatbot")
        self.setGeometry(100, 100, 800, 600)
        self.central_widget = QWidget(self)
        
        self.setCentralWidget(self.central_widget)
        self.layout = QVBoxLayout()
        
       
       
        self.chatbox = QTextEdit(self)
        self.layout.addWidget(self.chatbox)
        self.chatbox.setFixedWidth(800)
        self.chatbox.setFixedHeight(40)

        #self.submit_button = QPushButton("Submit", self)
        #self.submit_button.clicked.connect(self.get_response)
        #self.layout.addWidget(self.submit_button)

        self.speak_button = QPushButton("Speak", self)
        self.speak_button.clicked.connect(self.audio_to_text)
        self.layout.addWidget(self.speak_button)

    
        self.video_label = QLabel("Video Output:", self)
        self.layout.addWidget(self.video_label)

        self.video_url_label = QLabel("Video URL: ", self)
        self.layout.addWidget(self.video_url_label)

        self.video_widget = QVideoWidget(self)
        
        self.layout.addWidget(self.video_widget)
        #self.video_widget.setFixedWidth(459)

        

        self.central_widget.setLayout(self.layout)

        self.media_player = QMediaPlayer()
        self.media_player.setVideoOutput(self.video_widget)
        self.media_player.mediaStatusChanged.connect(self.handle_media_status_changed)

        
        self.sample_video_url = "/home/knight/ChatBot_UI/Friendly_new.mp4"
        
        self.answer_video_url = None  # URL for the answer video
        self.play_video(self.sample_video_url)



    

    def audio_to_text(self):

        self.chatbox.setPlainText("Listening ...")
      
        init_rec = sr.Recognizer()
        print("Let's speak!!")
        with sr.Microphone(3) as source:
            audio_data = init_rec.record(source, duration=2)
            print("Recognizing your text.............")
            text = init_rec.recognize_google(audio_data)
            self.chatbox.setPlainText(text.strip())
            self.get_response(text.strip())
            print(text)


        
    def get_response(self,text):
        print("Response Text: ",text)
        #user_input = self.chatbox.toPlainText().strip()
        user_input = text.strip()
        intent = self.chatbot.match_intent(user_input)
        if intent:
            video_url = self.chatbot.get_video_response(intent)
            if video_url:
                self.video_url_label.setText(f"Video URL: {video_url}")
                
                self.play_video(video_url)
                return

        #response = self.chatbot.get_response(user_input)
       
        self.chatbox.clear()
        #self.display_response(response)
        self.play_video("/home/knight/ChatBot_UI/Friendly_new.mp4")

    def play_video(self, video_url):

        media_content = QMediaContent(QUrl.fromLocalFile(video_url))
        self.media_player.setMedia(media_content)
        self.media_player.play()
    
    
    def handle_media_status_changed(self, status):
        if status == QMediaPlayer.EndOfMedia:
            print("Video finished playing.")
            self.play_video("/home/knight/ChatBot_UI/Friendly_new.mp4")
     


    def display_response(self, response):
        self.webview.setHtml(response)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = HospitalChatbotGUI()
    window.show()
    
    sys.exit(app.exec_())