<?php
namespace Misuzu\Users;

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

class RoleInfo implements Stringable {
    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 function __construct(IDbResult $result) {
        $this->id = (string)$result->getInteger(0);
        $this->rank = $result->getInteger(1);
        $this->name = $result->getString(2);
        $this->title = $result->isNull(3) ? null : $result->getString(3);
        $this->description = $result->isNull(4) ? null : $result->getString(4);
        $this->hidden = $result->getInteger(5) !== 0;
        $this->leavable = $result->getInteger(6) !== 0;
        $this->colour = $result->isNull(7) ? null : $result->getInteger(7);
        $this->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;
    }
}
