import threading
import time
import sys
from .core.voice_recognizer import VoiceRecognizer
from .core.command_processor import CommandProcessor
from .core.system_controller import SystemController
from .core.text_to_speech import TextToSpeech

class Dikshant:
    def __init__(self):
        self.is_active = True
        self.recognizer = VoiceRecognizer()
        self.processor = CommandProcessor()
        self.controller = SystemController()
        self.tts = TextToSpeech()
        self.wake_phrase = "hey friend"
        self.sleep_phrase = "take rest"
        self.listening_timeout = 5
        
    def start(self):
        """Start the assistant in background"""
        self.tts.speak("I'm ready to help")
        thread = threading.Thread(target=self._run)
        thread.daemon = True  # Changed to daemon thread
        thread.start()
        
    def _run(self):
        """Main listening loop with proper shutdown"""
        while self.is_active:
            try:
                text = self.recognizer.listen(timeout=self.listening_timeout)
                if not text:
                    continue
                    
                if self.wake_phrase in text.lower():
                    command = text.lower().split(self.wake_phrase)[1].strip()
                    self._process_command(command)
                elif self.sleep_phrase in text.lower():
                    self.is_active = False
                    self.tts.speak("Going to rest now")
                    sys.exit(0)  # Clean exit
                    
                time.sleep(0.3)  # Faster response interval
                
            except Exception as e:
                print(f"Error: {e}")
                time.sleep(1)

    def _process_command(self, command_text):
        """Process commands with quick return"""
        command = self.processor.process_command(command_text)
        if command['action'] == 'unknown':
            self.tts.speak("I didn't understand that")
            return
            
        result = self.controller.execute(command)
        self.tts.speak(command.get('response', result))