<?php
namespace Misuzu\Users;

use Stringable;
use Index\DateTime;
use Index\Colour\Colour;
use Index\Data\IDbResult;

class RoleInfo implements Stringable {
    public function __construct(
        private string $id,
        private int $rank,
        private string $name,
        private ?string $title,
        private ?string $description,
        private bool $hidden,
        private bool $leavable,
        private ?int $colour,
        private int $created,
    ) {}

    public static function fromResult(IDbResult $result): RoleInfo {
        return new RoleInfo(
            id: $result->getString(0),
            rank: $result->getInteger(1),
            name: $result->getString(2),
            title: $result->getStringOrNull(3),
            description: $result->getStringOrNull(4),
            hidden: $result->getBoolean(5),
            leavable: $result->getBoolean(6),
            colour: $result->getIntegerOrNull(7),
            created: $result->getInteger(8),
        );
    }

    public function getId(): string {
        return $this->id;
    }

    public function isDefault(): bool {
        return $this->id === Roles::DEFAULT_ROLE;
    }

    public function getRank(): int {
        return $this->rank;
    }

    public function getName(): string {
        return $this->name;
    }

    public function hasTitle(): bool {
        return $this->title !== null && $this->title !== '';
    }

    public function getTitle(): ?string {
        return $this->title;
    }

    public function hasDescription(): bool {
        return $this->description !== null && $this->description !== '';
    }

    public function getDescription(): ?string {
        return $this->description;
    }

    public function isHidden(): bool {
        return $this->hidden;
    }

    public function isLeavable(): bool {
        return $this->leavable;
    }

    public function hasColour(): bool {
        return $this->colour !== null && ($this->colour & 0x40000000) === 0;
    }

    public function getColourRaw(): ?int {
        return $this->colour;
    }

    public function getColour(): Colour {
        return $this->colour === null ? Colour::none() : Colour::fromMisuzu($this->colour);
    }

    public function getCreatedTime(): int {
        return $this->created;
    }

    public function getCreatedAt(): DateTime {
        return DateTime::fromUnixTimeSeconds($this->created);
    }

    public function __toString(): string {
        return 'r' . $this->id;
    }
}
