<?php

namespace Cobblestone\CcLogin\Entity;

/**
 * Represents an Asset from the User Rights Management database
 *
 * Class Asset
 * @package Cobblestone\CcLogin\Entity
 */
class Asset
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $title;

    /**
     * @var Lease[]|array
     */
    private $leases;

    /**
     * @var string|null
     */
    private $cvr;

    /**
     * @var string|null
     */
    private $unik;

    /**
     * @var string|null
     */
    private $microbiz;

    /**
     * @var string|null
     */
    private $timelog;

    /**
     * @var string|null
     */
    private $pipedrive;

    /**
     * Asset constructor.
     * @param int $id
     * @param string $title
     * @param Lease[]|array $leases
     * @param null|string $cvr
     * @param null|string $unik
     * @param null|string $microbiz
     * @param null|string $timelog
     * @param null|string $pipedrive
     */
    public function __construct
    (
        int $id,
        string $title,
        array $leases = [],
        ?string $cvr = null,
        ?string $unik = null,
        ?string $microbiz = null,
        ?string $timelog = null,
        ?string $pipedrive = null
    )
    {
        $this->id = $id;
        $this->title = $title;
        $this->leases = $leases;
        $this->cvr = $cvr;
        $this->unik = $unik;
        $this->microbiz = $microbiz;
        $this->timelog = $timelog;
        $this->pipedrive = $pipedrive;
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId(int $id): void
    {
        $this->id = $id;
    }

    /**
     * Returns title if Asset unik is null
     * Returns Asset title prefixed with unik is unik is not null
     *
     * @return string
     */
    public function getTitle(): string
    {
        $title = $this->title;
        $unik = $this->unik;

        return empty($unik) ? $title : "$unik - $title";
    }

    /**
     * @param string $title
     */
    public function setTitle(string $title): void
    {
        $this->title = $title;
    }

    /**
     * @return Lease[]|array
     */
    public function getLeases()
    {
        return $this->leases;
    }

    /**
     * @param Lease[]|array $leases
     */
    public function setLeases($leases): void
    {
        $this->leases = $leases;
    }

    /**
     * @return null|string
     */
    public function getCvr(): ?string
    {
        return $this->cvr;
    }

    /**
     * @param null|string $cvr
     */
    public function setCvr(?string $cvr): void
    {
        $this->cvr = $cvr;
    }

    /**
     * @return null|string
     */
    public function getUnik(): ?string
    {
        return $this->unik;
    }

    /**
     * @param null|string $unik
     */
    public function setUnik(?string $unik): void
    {
        $this->unik = $unik;
    }

    /**
     * @return null|string
     */
    public function getMicrobiz(): ?string
    {
        return $this->microbiz;
    }

    /**
     * @param null|string $microbiz
     */
    public function setMicrobiz(?string $microbiz): void
    {
        $this->microbiz = $microbiz;
    }

    /**
     * @return null|string
     */
    public function getTimelog(): ?string
    {
        return $this->timelog;
    }

    /**
     * @param null|string $timelog
     */
    public function setTimelog(?string $timelog): void
    {
        $this->timelog = $timelog;
    }

    /**
     * @return null|string
     */
    public function getPipedrive(): ?string
    {
        return $this->pipedrive;
    }

    /**
     * @param null|string $pipedrive
     */
    public function setPipedrive(?string $pipedrive): void
    {
        $this->pipedrive = $pipedrive;
    }

    /**
     * Returns the Lease corresponding to the leaseId parameter, if it exists in this Asset's list of Leases
     *
     * @param int|null $leaseId
     * @return Lease|null
     */
    public function getLease(?int $leaseId): ?Lease
    {
        $leases = $this->getLeases();

        foreach ($leases as $lease) {
            if ($lease->getId() === $leaseId) {
                return $lease;
            }
        }

        return null;
    }
}