<?php

namespace Cobblestone\CcLogin\Service;

use Cobblestone\CcLogin\Entity\Lease;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class LeaseService extends AbstractCCService
{
    /**
     * @var TokenStorageInterface
     */
    protected $tokenStorage;

    /**
     * @var Container
     */
    private $container;

    /**
     * AssetService constructor.
     * @param TokenStorageInterface $tokenStorage
     * @param Container $container
     * @param string $apiUrl
     */
    public function __construct
    (
        TokenStorageInterface $tokenStorage,
        Container $container,
        string $apiUrl
    )
    {
        $this->tokenStorage = $tokenStorage;
        $this->container = $container;
        parent::__construct($apiUrl);
    }

    /**
     * Turns an API response into an array of Lease objects
     *
     * @param array $results
     * @param int $assetId
     * @return Lease[]|array
     */
    public function resultsToObjects(array $results, int $assetId): array
    {
        $leases = [];

        foreach ($results as $leaseArr) {
            $id = intval($leaseArr['id']);
            $address = $leaseArr['address'];
            $zip = $leaseArr['zip'];

            $lease = new Lease($id, $assetId, $address, $zip);

            $leases[] = $lease;
        }

        return $leases;
    }
}