<?php

declare(strict_types=1);

namespace App\Framework\Console\Screen;

use App\Framework\Console\ConsoleOutput;

/**
 * Verantwortlich für Cursor-Positionierung.
 */
final readonly class Cursor
{
    public function __construct(
        private ConsoleOutput $output
    ) {
    }

    /**
     * Bewegt den Cursor zu einer bestimmten Position.
     */
    public function moveTo(int $row, int $col): self
    {
        if ($this->output->isTerminal()) {
            $this->output->writeRaw(CursorControlCode::POSITION->format($row, $col));
        }

        return $this;
    }

    /**
     * Bewegt den Cursor zum Anfang (Home-Position).
     */
    public function home(): self
    {
        if ($this->output->isTerminal()) {
            // Die Home-Position ist 1,1 (obere linke Ecke)
            $this->output->writeRaw(CursorControlCode::POSITION->format(1, 1));
        }

        return $this;
    }

    /**
     * Bewegt den Cursor um X Zeilen nach oben.
     */
    public function up(int $lines = 1): self
    {
        if ($this->output->isTerminal() && $lines > 0) {
            $this->output->writeRaw(CursorControlCode::UP->format($lines));
        }

        return $this;
    }

    /**
     * Bewegt den Cursor um X Zeilen nach unten.
     */
    public function down(int $lines = 1): self
    {
        if ($this->output->isTerminal() && $lines > 0) {
            $this->output->writeRaw(CursorControlCode::DOWN->format($lines));
        }

        return $this;
    }

    /**
     * Bewegt den Cursor um X Spalten nach links.
     */
    public function left(int $columns = 1): self
    {
        if ($this->output->isTerminal() && $columns > 0) {
            $this->output->writeRaw(CursorControlCode::LEFT->format($columns));
        }

        return $this;
    }

    /**
     * Bewegt den Cursor um X Spalten nach rechts.
     */
    public function right(int $columns = 1): self
    {
        if ($this->output->isTerminal() && $columns > 0) {
            $this->output->writeRaw(CursorControlCode::RIGHT->format($columns));
        }

        return $this;
    }

    /**
     * Versteckt den Cursor.
     */
    public function hide(): self
    {
        if ($this->output->isTerminal()) {
            $this->output->writeRaw(CursorControlCode::HIDE->format());
        }

        return $this;
    }

    /**
     * Zeigt den Cursor wieder an.
     */
    public function show(): self
    {
        if ($this->output->isTerminal()) {
            $this->output->writeRaw(CursorControlCode::SHOW->format());
        }

        return $this;
    }

    /**
     * Speichert die aktuelle Cursorposition.
     */
    public function save(): self
    {
        if ($this->output->isTerminal()) {
            $this->output->writeRaw(CursorControlCode::SAVE->format());
        }

        return $this;
    }

    /**
     * Stellt die zuvor gespeicherte Cursorposition wieder her.
     */
    public function restore(): self
    {
        if ($this->output->isTerminal()) {
            $this->output->writeRaw(CursorControlCode::RESTORE->format());
        }

        return $this;
    }
}
