<?php
namespace Misuzu\Perms;

use Index\Data\IDbResult;

class PermissionInfo implements IPermissionResult {
    use PermissionResultShared;

    private ?string $userId;
    private ?string $roleId;
    private ?string $forumCategoryId;
    private string $category;
    private int $allow;
    private int $deny;
    private int $calculated;

    public function __construct(IDbResult $result) {
        $this->userId = $result->isNull(0) ? null : $result->getString(0);
        $this->roleId = $result->isNull(1) ? null : $result->getString(1);
        $this->forumCategoryId = $result->isNull(2) ? null : $result->getString(2);
        $this->category = $result->getString(3);
        $this->allow = $result->getInteger(4);
        $this->deny = $result->getInteger(5);
        $this->calculated = $this->allow & ~$this->deny;
    }

    public function hasUserId(): bool {
        return $this->userId !== null;
    }

    public function getUserId(): ?string {
        return $this->userId;
    }

    public function hasRoleId(): bool {
        return $this->roleId !== null;
    }

    public function getRoleId(): ?string {
        return $this->roleId;
    }

    public function hasForumCategoryId(): bool {
        return $this->forumCategoryId !== null;
    }

    public function getForumCategoryId(): ?string {
        return $this->forumCategoryId;
    }

    public function getCategory(): string {
        return $this->category;
    }

    public function getAllow(): int {
        return $this->allow;
    }

    public function getDeny(): int {
        return $this->deny;
    }

    public function getCalculated(): int {
        return $this->calculated;
    }

    public function check(int $perm): bool {
        return ($this->calculated & $perm) > 0;
    }

    public function checkAllow(int $perm): bool {
        return ($this->allow & $perm) > 0;
    }

    public function checkDeny(int $perm): bool {
        return ($this->deny & $perm) > 0;
    }

    public function checkNeutral(int $perm): bool {
        return ($this->allow & $perm) === 0
            && ($this->deny & $perm) === 0;
    }
}
