#!/usr/bin/env python3
# Lancement: cpu-monitor

import tkinter as tk
from tkinter import ttk
import time

import psutil

try:
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    from matplotlib.figure import Figure
    HAVE_MPL = True
except ImportError:
    HAVE_MPL = False


MAX_POINTS = 60
UPDATE_INTERVAL = 1000  # ms


class Tracker:
    def __init__(self):
        self.cpu_data = []
        self.ram_data = []
        self._last_cpu = None

    def cpu_percent(self):
        return psutil.cpu_percent(interval=None)


class App:
    cpu_data = []
    ram_data = []

    def __init__(self, root):
        self.root = root
        self.root.title("CPU / RAM Monitor")
        self.root.geometry("1000x200")

        self.tracker = Tracker()

        # --- Header card ---
        card_bg = "#23272f"
        card_fg = "#ffffff"
        accent = "#4fc3f7"

        header_frame = ttk.Frame(root)
        header_frame.pack(fill=tk.X, padx=10, pady=(10, 0))

        self.cpu_label = ttk.Label(
            header_frame,
            text=f"CPU: --%",
            font=("Segoe UI", 20, "bold"),
            foreground=accent,
        )
        self.cpu_label.pack(side=tk.LEFT, padx=20)

        self.ram_label = ttk.Label(
            header_frame,
            text=f"RAM: --%",
            font=("Segoe UI", 20, "bold"),
            foreground="#81c784",
        )
        self.ram_label.pack(side=tk.LEFT, padx=20)

        self.disk_label = ttk.Label(
            header_frame,
            text=f"Disk: --%",
            font=("Segoe UI", 20, "bold"),
            foreground="#ba68c8",
        )
        self.disk_label.pack(side=tk.LEFT, padx=20)

        self.fig = None

        if HAVE_MPL:
            # --- Plot ---
            self.fig = Figure(figsize=(14, 3), facecolor=card_bg)
            ax_cpu = self.fig.add_subplot(1, 3, 1, facecolor=card_bg)
            ax_ram = self.fig.add_subplot(1, 3, 2, facecolor=card_bg)
            ax_disk = self.fig.add_subplot(1, 3, 3, facecolor=card_bg)

            ax_cpu.set_title("CPU %", color=card_fg, fontsize=11)
            ax_ram.set_title("RAM %", color=card_fg, fontsize=11)
            ax_disk.set_title("Disk %", color=card_fg, fontsize=11)
            ax_cpu.set_ylim(0, 100)
            ax_ram.set_ylim(0, 100)
            ax_disk.set_ylim(0, 100)

            ax_cpu.tick_params(colors="#aaa")
            ax_ram.tick_params(colors="#aaa")
            ax_disk.tick_params(colors="#aaa")
            for label in ax_cpu.get_yticklabels():
                label.set_color("#aaa")
            for label in ax_ram.get_yticklabels():
                label.set_color("#aaa")
            for label in ax_disk.get_yticklabels():
                label.set_color("#aaa")
            for line in ax_cpu.spines.values():
                line.set_color("#3a3f4a")
            for line in ax_ram.spines.values():
                line.set_color("#3a3f4a")
            for line in ax_disk.spines.values():
                line.set_color("#3a3f4a")

            self.cpu_line, = ax_cpu.plot(
                [], [], color="#ef5350", linewidth=1.5,
                marker="", markerfacecolor="#ef5350"
            )
            self.ram_line, = ax_ram.plot(
                [], [], color="#66bb6a", linewidth=1.5,
                marker="", markerfacecolor="#66bb6a"
            )
            self.disk_line, = ax_disk.plot(
                [], [], color="#ba68c8", linewidth=1.5,
                marker="", markerfacecolor="#ba68c8"
            )

            ax_cpu.axhline(y=80, color="#ef5350", linestyle="--", alpha=0.4)

            canvas = FigureCanvasTkAgg(self.fig, master=root)
            canvas.draw()
            canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

        # --- Footer status ---
        status_frame = ttk.Frame(root)
        status_frame.pack(fill=tk.X, padx=10, pady=(0, 10))

        self.status_label = ttk.Label(
            status_frame,
            text="Monitoring...",
            font=("Segoe UI", 9),
            foreground="#888",
        )
        self.status_label.pack(side=tk.LEFT)

        stop_btn = ttk.Button(
            status_frame,
            text="Stop",
            command=self._stop,
            width=10,
        )
        stop_btn.pack(side=tk.RIGHT, padx=(10, 0))

        self._running = True
        self._refresh()

    def _refresh(self):
        if not self._running:
            return
        cpu = self.tracker.cpu_percent()
        ram = psutil.virtual_memory().percent
        disk = psutil.disk_usage('/').percent
        App._last_cpu = cpu
        self.cpu_data.append(cpu)
        self.ram_data.append(ram)

        if len(App.cpu_data) > MAX_POINTS:
            App.cpu_data = App.cpu_data[-MAX_POINTS:]
        if len(App.ram_data) > MAX_POINTS:
            App.ram_data = App.ram_data[-MAX_POINTS:]

        self.cpu_label.config(text=f"CPU: {cpu:5.1f}%")
        self.ram_label.config(text=f"RAM: {ram:5.1f}%")
        self.disk_label.config(text=f"Disk: {disk:5.1f}%")

        if HAVE_MPL and self.fig:
            ax_cpu = self.fig.axes[0]
            ax_ram = self.fig.axes[1]
            ax_disk = self.fig.axes[2]

            self.cpu_line.set_data(range(len(App.cpu_data)), App.cpu_data)
            self.ram_line.set_data(range(len(App.ram_data)), App.ram_data)

            ax_cpu.set_xlim(0, MAX_POINTS)
            ax_ram.set_xlim(0, MAX_POINTS)
            ax_disk.set_xlim(0, MAX_POINTS)

            self.fig.canvas.draw_idle()

        self.root.after(UPDATE_INTERVAL, self._refresh)

    def _stop(self):
        self._running = False
        self.status_label.config(text="Stopped.")
        self.root.destroy()


def main():
    root = tk.Tk()
    app = App(root)
    root.mainloop()


if __name__ == "__main__":
    main()
