# neurotron/dashboard/renderer.py

import sys
import time

from neurotron.dashboard.panes.status import StatusPane
from neurotron.dashboard.panes.kernel import KernelPane
from neurotron.dashboard.panes.memory import MemoryPane
from neurotron.dashboard.panes.trm import TRMPane
from neurotron.dashboard.panes.chat import ChatPane


class Renderer:
    WIDTH = 80
    HEIGHT = 45

    def __init__(self):
        self.status = StatusPane()
        self.kernel = KernelPane()
        self.memory = MemoryPane()
        self.trm    = TRMPane()
        self.chat   = ChatPane()

        # limpar ecrã e esconder cursor
        sys.stdout.write("\033[2J\033[H\033[?25l")
        sys.stdout.flush()

    def render(self, ctx):
        # HEADER
        self.status.render(ctx, 1, 1, 80, 3)

        self._hline(4)

        # KERNEL
        self.kernel.render(ctx, 1, 5, 80, 8)
        self._hline(13)

        # NEUROTRON / MEMORY
        self.memory.render(ctx, 1, 14, 80, 8)
        self._hline(22)

        # TRM
        self.trm.render(ctx, 1, 23, 80, 8)
        self._hline(31)

        # CHAT
        self.chat.render(ctx, 1, 32, 80, 12)

        sys.stdout.flush()

    def _hline(self, y):
        sys.stdout.write(f"\033[{y};1H" + "─" * self.WIDTH)
