from .speech import speak, recognize
from .vision import capture_frame
from .llm import openai_chat, local_generate
from .utils import load_prompts
import cv2

class Jarvis:
    def __init__(self):
        self.prompts = load_prompts()
        self.use_local = False   # переключаемся автоматически, если нет OpenAI‑key

    def _choose_llm(self):
        return local_generate if self.use_local else openai_chat

    def process(self, text: str) -> str:
        llm = self._choose_llm()
        # простейший роутинг команд
        if text.lower().startswith("show me"):
            frame = capture_frame()
            if frame is not None:
                cv2.imshow("Jarvis – camera", frame)
                cv2.waitKey(2000)
                cv2.destroyAllWindows()
            return "Displayed camera frame."
        elif text.lower().startswith("joke"):
            return llm("Tell me a short joke.")
        elif text.lower().startswith("weather"):
            # здесь можно добавить запрос к внешнему API
            return "I don't have internet access right now."
        else:
            # generic answer
            return llm(text)

    def run(self):
        speak("Jarvis ready. Say something!")
        while True:
            try:
                cmd = recognize()
                if cmd.lower() in {"exit", "quit", "stop"}:
                    speak("Good bye!")
                    break
                answer = self.process(cmd)
                speak(answer)
            except KeyboardInterrupt:
                speak("Interrupted, shutting down.")
                break

if __name__ == "__main__":
    Jarvis().run()
