<?php

namespace PHPNative\Ui\Widget;

class ClickableHeaderLabel extends Label
{
    private string $columnKey;
    private Table $table;

    public function __construct(string $columnKey, Table $table, string $title, string $style)
    {
        $this->columnKey = $columnKey;
        $this->table = $table;
        parent::__construct($title, $style);
    }

    public function handleMouseClick(float $mouseX, float $mouseY, int $button): bool
    {
        if (
            $mouseX >= $this->viewport->x &&
            $mouseX <= ($this->viewport->x + $this->viewport->width) &&
            $mouseY >= $this->viewport->y &&
            $mouseY <= ($this->viewport->y + $this->viewport->height)
        ) {
            $this->table->sortByColumn($this->columnKey);
            return true;
        }
        return parent::handleMouseClick($mouseX, $mouseY, $button);
    }
}
