import tkinter as tk
import threading
import subprocess
import os

class ApplicationLauncher:
    def __init__(self, parent):
        """Crea botones para abrir aplicaciones como VS Code, Eclipse y PyCharm."""
        self.parent = parent

        # Detectar rutas de instalación
        self.vscode_path = self.detect_path(["C:\\Program Files\\Microsoft VS Code\\Code.exe"])
        self.eclipse_path = self.detect_path(["C:\\eclipse\\eclipse.exe"])
        self.pycharm_path = self.detect_path(["C:\\Program Files\\JetBrains\\PyCharm\\bin\\pycharm64.exe"])

        # Crear botones de lanzamiento
        self.create_button("Visual Code", self.launch_vscode)
        self.create_button("Eclipse", self.launch_eclipse)
        self.create_button("PyCharm", self.launch_pycharm)

    def detect_path(self, paths):
        """Detecta la primera ruta válida de una lista de rutas posibles."""
        for path in paths:
            if os.path.exists(path):
                return path
        return None

    def create_button(self, name, command):
        """Crea un botón con la función de lanzar una aplicación."""
        button = tk.Button(self.parent, text=name, command=command, bg="lightgreen")
        button.pack(fill="x", pady=2)

    def launch_vscode(self):
        """Lanza Visual Studio Code."""
        self.launch_application(self.vscode_path, "Visual Studio Code")

    def launch_application(self, path, name):
        """Ejecuta la aplicación si la ruta es válida."""
        if path:
            threading.Thread(target=subprocess.run, args=([path],), daemon=True).start()
        else:
            print(f"{name} no está instalado.")
